Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.bluetooth; |
| 18 | |
Ivan Podogov | 0afe190 | 2016-12-23 11:52:21 +0000 | [diff] [blame] | 19 | import android.annotation.SdkConstant; |
| 20 | import android.annotation.SdkConstant.SdkConstantType; |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 21 | import android.content.ComponentName; |
| 22 | import android.content.Context; |
| 23 | import android.content.Intent; |
| 24 | import android.content.ServiceConnection; |
| 25 | import android.os.IBinder; |
| 26 | import android.os.RemoteException; |
| 27 | import android.util.Log; |
| 28 | |
Ivan Podogov | dd87cd3 | 2016-12-30 14:43:29 +0000 | [diff] [blame] | 29 | import java.util.ArrayList; |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 30 | import java.util.Arrays; |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 31 | import java.util.List; |
| 32 | |
| 33 | /** |
Hansong Zhang | 53f5412 | 2017-12-04 10:31:30 -0800 | [diff] [blame] | 34 | * Provides the public APIs to control the Bluetooth HID Device profile. |
Hansong Zhang | fa377b4 | 2017-09-27 14:17:20 -0700 | [diff] [blame] | 35 | * |
Hansong Zhang | 53f5412 | 2017-12-04 10:31:30 -0800 | [diff] [blame] | 36 | * <p>BluetoothHidDevice is a proxy object for controlling the Bluetooth HID Device Service via IPC. |
| 37 | * Use {@link BluetoothAdapter#getProfileProxy} to get the BluetoothHidDevice proxy object. |
Hansong Zhang | fa377b4 | 2017-09-27 14:17:20 -0700 | [diff] [blame] | 38 | * |
Hansong Zhang | 53f5412 | 2017-12-04 10:31:30 -0800 | [diff] [blame] | 39 | * <p>{@hide} |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 40 | */ |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame] | 41 | public final class BluetoothHidDevice implements BluetoothProfile { |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 42 | |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame] | 43 | private static final String TAG = BluetoothHidDevice.class.getSimpleName(); |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 44 | |
Ivan Podogov | 0afe190 | 2016-12-23 11:52:21 +0000 | [diff] [blame] | 45 | /** |
Hansong Zhang | 53f5412 | 2017-12-04 10:31:30 -0800 | [diff] [blame] | 46 | * Intent used to broadcast the change in connection state of the Input Host profile. |
Ivan Podogov | 0afe190 | 2016-12-23 11:52:21 +0000 | [diff] [blame] | 47 | * |
| 48 | * <p>This intent will have 3 extras: |
Hansong Zhang | 53f5412 | 2017-12-04 10:31:30 -0800 | [diff] [blame] | 49 | * |
Ivan Podogov | 0afe190 | 2016-12-23 11:52:21 +0000 | [diff] [blame] | 50 | * <ul> |
Hansong Zhang | 53f5412 | 2017-12-04 10:31:30 -0800 | [diff] [blame] | 51 | * <li>{@link #EXTRA_STATE} - The current state of the profile. |
| 52 | * <li>{@link #EXTRA_PREVIOUS_STATE}- The previous state of the profile. |
| 53 | * <li>{@link BluetoothDevice#EXTRA_DEVICE} - The remote device. |
Ivan Podogov | 0afe190 | 2016-12-23 11:52:21 +0000 | [diff] [blame] | 54 | * </ul> |
| 55 | * |
Hansong Zhang | 53f5412 | 2017-12-04 10:31:30 -0800 | [diff] [blame] | 56 | * <p>{@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} can be any of {@link |
| 57 | * #STATE_DISCONNECTED}, {@link #STATE_CONNECTING}, {@link #STATE_CONNECTED}, {@link |
| 58 | * #STATE_DISCONNECTING}. |
Ivan Podogov | 0afe190 | 2016-12-23 11:52:21 +0000 | [diff] [blame] | 59 | * |
Hansong Zhang | 53f5412 | 2017-12-04 10:31:30 -0800 | [diff] [blame] | 60 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission to receive. |
Ivan Podogov | 0afe190 | 2016-12-23 11:52:21 +0000 | [diff] [blame] | 61 | */ |
| 62 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 63 | public static final String ACTION_CONNECTION_STATE_CHANGED = |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame] | 64 | "android.bluetooth.hiddevice.profile.action.CONNECTION_STATE_CHANGED"; |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 65 | |
| 66 | /** |
| 67 | * Constants representing device subclass. |
| 68 | * |
Hansong Zhang | 53f5412 | 2017-12-04 10:31:30 -0800 | [diff] [blame] | 69 | * @see #registerApp (BluetoothHidDeviceAppQosSettings, BluetoothHidDeviceAppQosSettings, |
| 70 | * BluetoothHidDeviceAppQosSettings, BluetoothHidDeviceCallback) |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 71 | */ |
| 72 | public static final byte SUBCLASS1_NONE = (byte) 0x00; |
| 73 | public static final byte SUBCLASS1_KEYBOARD = (byte) 0x40; |
| 74 | public static final byte SUBCLASS1_MOUSE = (byte) 0x80; |
| 75 | public static final byte SUBCLASS1_COMBO = (byte) 0xC0; |
| 76 | |
| 77 | public static final byte SUBCLASS2_UNCATEGORIZED = (byte) 0x00; |
| 78 | public static final byte SUBCLASS2_JOYSTICK = (byte) 0x01; |
| 79 | public static final byte SUBCLASS2_GAMEPAD = (byte) 0x02; |
| 80 | public static final byte SUBCLASS2_REMOTE_CONTROL = (byte) 0x03; |
| 81 | public static final byte SUBCLASS2_SENSING_DEVICE = (byte) 0x04; |
Hansong Zhang | 9eb138b | 2017-09-29 09:26:09 -0700 | [diff] [blame] | 82 | public static final byte SUBCLASS2_DIGITIZER_TABLET = (byte) 0x05; |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 83 | public static final byte SUBCLASS2_CARD_READER = (byte) 0x06; |
| 84 | |
| 85 | /** |
| 86 | * Constants representing report types. |
| 87 | * |
Hansong Zhang | fa377b4 | 2017-09-27 14:17:20 -0700 | [diff] [blame] | 88 | * @see BluetoothHidDeviceCallback#onGetReport(BluetoothDevice, byte, byte, int) |
| 89 | * @see BluetoothHidDeviceCallback#onSetReport(BluetoothDevice, byte, byte, byte[]) |
| 90 | * @see BluetoothHidDeviceCallback#onIntrData(BluetoothDevice, byte, byte[]) |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 91 | */ |
| 92 | public static final byte REPORT_TYPE_INPUT = (byte) 1; |
| 93 | public static final byte REPORT_TYPE_OUTPUT = (byte) 2; |
| 94 | public static final byte REPORT_TYPE_FEATURE = (byte) 3; |
| 95 | |
| 96 | /** |
| 97 | * Constants representing error response for Set Report. |
| 98 | * |
Hansong Zhang | fa377b4 | 2017-09-27 14:17:20 -0700 | [diff] [blame] | 99 | * @see BluetoothHidDeviceCallback#onSetReport(BluetoothDevice, byte, byte, byte[]) |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 100 | */ |
| 101 | public static final byte ERROR_RSP_SUCCESS = (byte) 0; |
| 102 | public static final byte ERROR_RSP_NOT_READY = (byte) 1; |
| 103 | public static final byte ERROR_RSP_INVALID_RPT_ID = (byte) 2; |
| 104 | public static final byte ERROR_RSP_UNSUPPORTED_REQ = (byte) 3; |
| 105 | public static final byte ERROR_RSP_INVALID_PARAM = (byte) 4; |
| 106 | public static final byte ERROR_RSP_UNKNOWN = (byte) 14; |
| 107 | |
| 108 | /** |
Hansong Zhang | 53f5412 | 2017-12-04 10:31:30 -0800 | [diff] [blame] | 109 | * Constants representing protocol mode used set by host. Default is always {@link |
| 110 | * #PROTOCOL_REPORT_MODE} unless notified otherwise. |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 111 | * |
Hansong Zhang | fa377b4 | 2017-09-27 14:17:20 -0700 | [diff] [blame] | 112 | * @see BluetoothHidDeviceCallback#onSetProtocol(BluetoothDevice, byte) |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 113 | */ |
| 114 | public static final byte PROTOCOL_BOOT_MODE = (byte) 0; |
| 115 | public static final byte PROTOCOL_REPORT_MODE = (byte) 1; |
| 116 | |
| 117 | private Context mContext; |
| 118 | |
| 119 | private ServiceListener mServiceListener; |
| 120 | |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame] | 121 | private volatile IBluetoothHidDevice mService; |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 122 | |
| 123 | private BluetoothAdapter mAdapter; |
| 124 | |
Hansong Zhang | 53f5412 | 2017-12-04 10:31:30 -0800 | [diff] [blame] | 125 | private static class BluetoothHidDeviceCallbackWrapper |
| 126 | extends IBluetoothHidDeviceCallback.Stub { |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 127 | |
| 128 | private BluetoothHidDeviceCallback mCallback; |
| 129 | |
| 130 | public BluetoothHidDeviceCallbackWrapper(BluetoothHidDeviceCallback callback) { |
| 131 | mCallback = callback; |
| 132 | } |
| 133 | |
| 134 | @Override |
Hansong Zhang | 7e527b7 | 2017-11-30 16:37:05 -0800 | [diff] [blame] | 135 | public void onAppStatusChanged(BluetoothDevice pluggedDevice, boolean registered) { |
| 136 | mCallback.onAppStatusChanged(pluggedDevice, registered); |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public void onConnectionStateChanged(BluetoothDevice device, int state) { |
| 141 | mCallback.onConnectionStateChanged(device, state); |
| 142 | } |
| 143 | |
| 144 | @Override |
Ivan Podogov | dd87cd3 | 2016-12-30 14:43:29 +0000 | [diff] [blame] | 145 | public void onGetReport(BluetoothDevice device, byte type, byte id, int bufferSize) { |
| 146 | mCallback.onGetReport(device, type, id, bufferSize); |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | @Override |
Ivan Podogov | dd87cd3 | 2016-12-30 14:43:29 +0000 | [diff] [blame] | 150 | public void onSetReport(BluetoothDevice device, byte type, byte id, byte[] data) { |
| 151 | mCallback.onSetReport(device, type, id, data); |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | @Override |
Ivan Podogov | dd87cd3 | 2016-12-30 14:43:29 +0000 | [diff] [blame] | 155 | public void onSetProtocol(BluetoothDevice device, byte protocol) { |
| 156 | mCallback.onSetProtocol(device, protocol); |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | @Override |
Ivan Podogov | dd87cd3 | 2016-12-30 14:43:29 +0000 | [diff] [blame] | 160 | public void onIntrData(BluetoothDevice device, byte reportId, byte[] data) { |
| 161 | mCallback.onIntrData(device, reportId, data); |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | @Override |
Ivan Podogov | dd87cd3 | 2016-12-30 14:43:29 +0000 | [diff] [blame] | 165 | public void onVirtualCableUnplug(BluetoothDevice device) { |
| 166 | mCallback.onVirtualCableUnplug(device); |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 170 | private final IBluetoothStateChangeCallback mBluetoothStateChangeCallback = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 171 | new IBluetoothStateChangeCallback.Stub() { |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 172 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 173 | public void onBluetoothStateChange(boolean up) { |
| 174 | Log.d(TAG, "onBluetoothStateChange: up=" + up); |
| 175 | synchronized (mConnection) { |
Hansong Zhang | fa377b4 | 2017-09-27 14:17:20 -0700 | [diff] [blame] | 176 | if (up) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 177 | try { |
| 178 | if (mService == null) { |
| 179 | Log.d(TAG, "Binding HID Device service..."); |
| 180 | doBind(); |
| 181 | } |
| 182 | } catch (IllegalStateException e) { |
Hansong Zhang | 53f5412 | 2017-12-04 10:31:30 -0800 | [diff] [blame] | 183 | Log.e(TAG, "onBluetoothStateChange: could not bind to HID Dev " |
| 184 | + "service: ", e); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 185 | } catch (SecurityException e) { |
Hansong Zhang | 53f5412 | 2017-12-04 10:31:30 -0800 | [diff] [blame] | 186 | Log.e(TAG, "onBluetoothStateChange: could not bind to HID Dev " |
| 187 | + "service: ", e); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 188 | } |
Hansong Zhang | fa377b4 | 2017-09-27 14:17:20 -0700 | [diff] [blame] | 189 | } else { |
| 190 | Log.d(TAG, "Unbinding service..."); |
| 191 | doUnbind(); |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 192 | } |
| 193 | } |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 194 | } |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 195 | }; |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 196 | |
Hansong Zhang | 53f5412 | 2017-12-04 10:31:30 -0800 | [diff] [blame] | 197 | private final ServiceConnection mConnection = |
| 198 | new ServiceConnection() { |
| 199 | public void onServiceConnected(ComponentName className, IBinder service) { |
| 200 | Log.d(TAG, "onServiceConnected()"); |
| 201 | mService = IBluetoothHidDevice.Stub.asInterface(service); |
| 202 | if (mServiceListener != null) { |
| 203 | mServiceListener.onServiceConnected( |
| 204 | BluetoothProfile.HID_DEVICE, BluetoothHidDevice.this); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | public void onServiceDisconnected(ComponentName className) { |
| 209 | Log.d(TAG, "onServiceDisconnected()"); |
| 210 | mService = null; |
| 211 | if (mServiceListener != null) { |
| 212 | mServiceListener.onServiceDisconnected(BluetoothProfile.HID_DEVICE); |
| 213 | } |
| 214 | } |
| 215 | }; |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 216 | |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame] | 217 | BluetoothHidDevice(Context context, ServiceListener listener) { |
| 218 | Log.v(TAG, "BluetoothHidDevice"); |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 219 | |
| 220 | mContext = context; |
| 221 | mServiceListener = listener; |
| 222 | mAdapter = BluetoothAdapter.getDefaultAdapter(); |
| 223 | |
| 224 | IBluetoothManager mgr = mAdapter.getBluetoothManager(); |
| 225 | if (mgr != null) { |
| 226 | try { |
| 227 | mgr.registerStateChangeCallback(mBluetoothStateChangeCallback); |
| 228 | } catch (RemoteException e) { |
| 229 | e.printStackTrace(); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | doBind(); |
| 234 | } |
| 235 | |
| 236 | boolean doBind() { |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame] | 237 | Intent intent = new Intent(IBluetoothHidDevice.class.getName()); |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 238 | ComponentName comp = intent.resolveSystemService(mContext.getPackageManager(), 0); |
| 239 | intent.setComponent(comp); |
| 240 | if (comp == null || !mContext.bindServiceAsUser(intent, mConnection, 0, |
| 241 | android.os.Process.myUserHandle())) { |
| 242 | Log.e(TAG, "Could not bind to Bluetooth HID Device Service with " + intent); |
| 243 | return false; |
| 244 | } |
| 245 | Log.d(TAG, "Bound to HID Device Service"); |
| 246 | return true; |
| 247 | } |
| 248 | |
Hansong Zhang | fa377b4 | 2017-09-27 14:17:20 -0700 | [diff] [blame] | 249 | void doUnbind() { |
| 250 | Log.d(TAG, "Unbinding HidDevService"); |
| 251 | if (mService != null) { |
| 252 | mService = null; |
| 253 | try { |
| 254 | mContext.unbindService(mConnection); |
| 255 | } catch (IllegalArgumentException e) { |
| 256 | Log.e(TAG, "Unable to unbind HidDevService", e); |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 261 | void close() { |
| 262 | Log.v(TAG, "close()"); |
| 263 | |
| 264 | IBluetoothManager mgr = mAdapter.getBluetoothManager(); |
| 265 | if (mgr != null) { |
| 266 | try { |
| 267 | mgr.unregisterStateChangeCallback(mBluetoothStateChangeCallback); |
| 268 | } catch (RemoteException e) { |
| 269 | e.printStackTrace(); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | synchronized (mConnection) { |
Hansong Zhang | fa377b4 | 2017-09-27 14:17:20 -0700 | [diff] [blame] | 274 | doUnbind(); |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 275 | } |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 276 | mServiceListener = null; |
| 277 | } |
| 278 | |
Hansong Zhang | 53f5412 | 2017-12-04 10:31:30 -0800 | [diff] [blame] | 279 | /** {@inheritDoc} */ |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 280 | @Override |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 281 | public List<BluetoothDevice> getConnectedDevices() { |
| 282 | Log.v(TAG, "getConnectedDevices()"); |
Ivan Podogov | dd87cd3 | 2016-12-30 14:43:29 +0000 | [diff] [blame] | 283 | |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame] | 284 | final IBluetoothHidDevice service = mService; |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 285 | if (service != null) { |
Ivan Podogov | dd87cd3 | 2016-12-30 14:43:29 +0000 | [diff] [blame] | 286 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 287 | return service.getConnectedDevices(); |
Ivan Podogov | dd87cd3 | 2016-12-30 14:43:29 +0000 | [diff] [blame] | 288 | } catch (RemoteException e) { |
| 289 | Log.e(TAG, e.toString()); |
| 290 | } |
| 291 | } else { |
| 292 | Log.w(TAG, "Proxy not attached to service"); |
| 293 | } |
| 294 | |
| 295 | return new ArrayList<BluetoothDevice>(); |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 296 | } |
| 297 | |
Hansong Zhang | 53f5412 | 2017-12-04 10:31:30 -0800 | [diff] [blame] | 298 | /** {@inheritDoc} */ |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 299 | @Override |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 300 | public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { |
| 301 | Log.v(TAG, "getDevicesMatchingConnectionStates(): states=" + Arrays.toString(states)); |
Ivan Podogov | dd87cd3 | 2016-12-30 14:43:29 +0000 | [diff] [blame] | 302 | |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame] | 303 | final IBluetoothHidDevice service = mService; |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 304 | if (service != null) { |
Ivan Podogov | dd87cd3 | 2016-12-30 14:43:29 +0000 | [diff] [blame] | 305 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 306 | return service.getDevicesMatchingConnectionStates(states); |
Ivan Podogov | dd87cd3 | 2016-12-30 14:43:29 +0000 | [diff] [blame] | 307 | } catch (RemoteException e) { |
| 308 | Log.e(TAG, e.toString()); |
| 309 | } |
| 310 | } else { |
| 311 | Log.w(TAG, "Proxy not attached to service"); |
| 312 | } |
| 313 | |
| 314 | return new ArrayList<BluetoothDevice>(); |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 315 | } |
| 316 | |
Hansong Zhang | 53f5412 | 2017-12-04 10:31:30 -0800 | [diff] [blame] | 317 | /** {@inheritDoc} */ |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 318 | @Override |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 319 | public int getConnectionState(BluetoothDevice device) { |
Ivan Podogov | dd87cd3 | 2016-12-30 14:43:29 +0000 | [diff] [blame] | 320 | Log.v(TAG, "getConnectionState(): device=" + device); |
| 321 | |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame] | 322 | final IBluetoothHidDevice service = mService; |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 323 | if (service != null) { |
Ivan Podogov | dd87cd3 | 2016-12-30 14:43:29 +0000 | [diff] [blame] | 324 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 325 | return service.getConnectionState(device); |
Ivan Podogov | dd87cd3 | 2016-12-30 14:43:29 +0000 | [diff] [blame] | 326 | } catch (RemoteException e) { |
| 327 | Log.e(TAG, e.toString()); |
| 328 | } |
| 329 | } else { |
| 330 | Log.w(TAG, "Proxy not attached to service"); |
| 331 | } |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 332 | |
| 333 | return STATE_DISCONNECTED; |
| 334 | } |
| 335 | |
| 336 | /** |
Hansong Zhang | 53f5412 | 2017-12-04 10:31:30 -0800 | [diff] [blame] | 337 | * Registers application to be used for HID device. Connections to HID Device are only possible |
| 338 | * when application is registered. Only one application can be registered at one time. When an |
| 339 | * application is registered, the HID Host service will be disabled until it is unregistered. |
| 340 | * When no longer used, application should be unregistered using {@link #unregisterApp()}. The |
| 341 | * registration status should be tracked by the application by handling callback from |
| 342 | * BluetoothHidDeviceCallback#onAppStatusChanged. The app registration status is not related to |
| 343 | * the return value of this method. |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 344 | * |
Hansong Zhang | 53f5412 | 2017-12-04 10:31:30 -0800 | [diff] [blame] | 345 | * @param sdp {@link BluetoothHidDeviceAppSdpSettings} object of HID Device SDP record. The HID |
| 346 | * Device SDP record is required. |
| 347 | * @param inQos {@link BluetoothHidDeviceAppQosSettings} object of Incoming QoS Settings. The |
| 348 | * Incoming QoS Settings is not required. Use null or default |
| 349 | * BluetoothHidDeviceAppQosSettings.Builder for default values. |
| 350 | * @param outQos {@link BluetoothHidDeviceAppQosSettings} object of Outgoing QoS Settings. The |
| 351 | * Outgoing QoS Settings is not required. Use null or default |
| 352 | * BluetoothHidDeviceAppQosSettings.Builder for default values. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 353 | * @param callback {@link BluetoothHidDeviceCallback} object to which callback messages will be |
Hansong Zhang | 53f5412 | 2017-12-04 10:31:30 -0800 | [diff] [blame] | 354 | * sent. The BluetoothHidDeviceCallback object is required. |
Hansong Zhang | ceb84db | 2017-11-08 09:57:12 -0800 | [diff] [blame] | 355 | * @return true if the command is successfully sent; otherwise false. |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 356 | */ |
| 357 | public boolean registerApp(BluetoothHidDeviceAppSdpSettings sdp, |
| 358 | BluetoothHidDeviceAppQosSettings inQos, BluetoothHidDeviceAppQosSettings outQos, |
| 359 | BluetoothHidDeviceCallback callback) { |
| 360 | Log.v(TAG, "registerApp(): sdp=" + sdp + " inQos=" + inQos + " outQos=" + outQos |
Hansong Zhang | 53f5412 | 2017-12-04 10:31:30 -0800 | [diff] [blame] | 361 | + " callback=" + callback); |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 362 | |
| 363 | boolean result = false; |
| 364 | |
| 365 | if (sdp == null || callback == null) { |
| 366 | return false; |
| 367 | } |
| 368 | |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame] | 369 | final IBluetoothHidDevice service = mService; |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 370 | if (service != null) { |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 371 | try { |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 372 | BluetoothHidDeviceCallbackWrapper cbw = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 373 | new BluetoothHidDeviceCallbackWrapper(callback); |
Hansong Zhang | 7e527b7 | 2017-11-30 16:37:05 -0800 | [diff] [blame] | 374 | result = service.registerApp(sdp, inQos, outQos, cbw); |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 375 | } catch (RemoteException e) { |
| 376 | Log.e(TAG, e.toString()); |
| 377 | } |
| 378 | } else { |
| 379 | Log.w(TAG, "Proxy not attached to service"); |
| 380 | } |
| 381 | |
| 382 | return result; |
| 383 | } |
| 384 | |
| 385 | /** |
Hansong Zhang | 53f5412 | 2017-12-04 10:31:30 -0800 | [diff] [blame] | 386 | * Unregisters application. Active connection will be disconnected and no new connections will |
| 387 | * be allowed until registered again using {@link #registerApp |
Hansong Zhang | fa377b4 | 2017-09-27 14:17:20 -0700 | [diff] [blame] | 388 | * (BluetoothHidDeviceAppQosSettings, BluetoothHidDeviceAppQosSettings, |
Hansong Zhang | 53f5412 | 2017-12-04 10:31:30 -0800 | [diff] [blame] | 389 | * BluetoothHidDeviceAppQosSettings, BluetoothHidDeviceCallback)} The registration status should |
| 390 | * be tracked by the application by handling callback from |
| 391 | * BluetoothHidDeviceCallback#onAppStatusChanged. The app registration status is not related to |
| 392 | * the return value of this method. |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 393 | * |
Hansong Zhang | ceb84db | 2017-11-08 09:57:12 -0800 | [diff] [blame] | 394 | * @return true if the command is successfully sent; otherwise false. |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 395 | */ |
Hansong Zhang | 7e527b7 | 2017-11-30 16:37:05 -0800 | [diff] [blame] | 396 | public boolean unregisterApp() { |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 397 | Log.v(TAG, "unregisterApp()"); |
| 398 | |
| 399 | boolean result = false; |
| 400 | |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame] | 401 | final IBluetoothHidDevice service = mService; |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 402 | if (service != null) { |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 403 | try { |
Hansong Zhang | 7e527b7 | 2017-11-30 16:37:05 -0800 | [diff] [blame] | 404 | result = service.unregisterApp(); |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 405 | } catch (RemoteException e) { |
| 406 | Log.e(TAG, e.toString()); |
| 407 | } |
| 408 | } else { |
| 409 | Log.w(TAG, "Proxy not attached to service"); |
| 410 | } |
| 411 | |
| 412 | return result; |
| 413 | } |
| 414 | |
| 415 | /** |
| 416 | * Sends report to remote host using interrupt channel. |
| 417 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 418 | * @param id Report Id, as defined in descriptor. Can be 0 in case Report Id are not defined in |
Hansong Zhang | 53f5412 | 2017-12-04 10:31:30 -0800 | [diff] [blame] | 419 | * descriptor. |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 420 | * @param data Report data, not including Report Id. |
Hansong Zhang | ceb84db | 2017-11-08 09:57:12 -0800 | [diff] [blame] | 421 | * @return true if the command is successfully sent; otherwise false. |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 422 | */ |
Ivan Podogov | dd87cd3 | 2016-12-30 14:43:29 +0000 | [diff] [blame] | 423 | public boolean sendReport(BluetoothDevice device, int id, byte[] data) { |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 424 | boolean result = false; |
| 425 | |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame] | 426 | final IBluetoothHidDevice service = mService; |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 427 | if (service != null) { |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 428 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 429 | result = service.sendReport(device, id, data); |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 430 | } catch (RemoteException e) { |
| 431 | Log.e(TAG, e.toString()); |
| 432 | } |
| 433 | } else { |
| 434 | Log.w(TAG, "Proxy not attached to service"); |
| 435 | } |
| 436 | |
| 437 | return result; |
| 438 | } |
| 439 | |
| 440 | /** |
Hansong Zhang | 53f5412 | 2017-12-04 10:31:30 -0800 | [diff] [blame] | 441 | * Sends report to remote host as reply for GET_REPORT request from {@link |
| 442 | * BluetoothHidDeviceCallback#onGetReport(BluetoothDevice, byte, byte, int)}. |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 443 | * |
| 444 | * @param type Report Type, as in request. |
| 445 | * @param id Report Id, as in request. |
| 446 | * @param data Report data, not including Report Id. |
Hansong Zhang | ceb84db | 2017-11-08 09:57:12 -0800 | [diff] [blame] | 447 | * @return true if the command is successfully sent; otherwise false. |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 448 | */ |
Ivan Podogov | dd87cd3 | 2016-12-30 14:43:29 +0000 | [diff] [blame] | 449 | public boolean replyReport(BluetoothDevice device, byte type, byte id, byte[] data) { |
| 450 | Log.v(TAG, "replyReport(): device=" + device + " type=" + type + " id=" + id); |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 451 | |
| 452 | boolean result = false; |
| 453 | |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame] | 454 | final IBluetoothHidDevice service = mService; |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 455 | if (service != null) { |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 456 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 457 | result = service.replyReport(device, type, id, data); |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 458 | } catch (RemoteException e) { |
| 459 | Log.e(TAG, e.toString()); |
| 460 | } |
| 461 | } else { |
| 462 | Log.w(TAG, "Proxy not attached to service"); |
| 463 | } |
| 464 | |
| 465 | return result; |
| 466 | } |
| 467 | |
| 468 | /** |
Hansong Zhang | 53f5412 | 2017-12-04 10:31:30 -0800 | [diff] [blame] | 469 | * Sends error handshake message as reply for invalid SET_REPORT request from {@link |
| 470 | * BluetoothHidDeviceCallback#onSetReport(BluetoothDevice, byte, byte, byte[])}. |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 471 | * |
| 472 | * @param error Error to be sent for SET_REPORT via HANDSHAKE. |
Hansong Zhang | ceb84db | 2017-11-08 09:57:12 -0800 | [diff] [blame] | 473 | * @return true if the command is successfully sent; otherwise false. |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 474 | */ |
Ivan Podogov | dd87cd3 | 2016-12-30 14:43:29 +0000 | [diff] [blame] | 475 | public boolean reportError(BluetoothDevice device, byte error) { |
| 476 | Log.v(TAG, "reportError(): device=" + device + " error=" + error); |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 477 | |
| 478 | boolean result = false; |
| 479 | |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame] | 480 | final IBluetoothHidDevice service = mService; |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 481 | if (service != null) { |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 482 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 483 | result = service.reportError(device, error); |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 484 | } catch (RemoteException e) { |
| 485 | Log.e(TAG, e.toString()); |
| 486 | } |
| 487 | } else { |
| 488 | Log.w(TAG, "Proxy not attached to service"); |
| 489 | } |
| 490 | |
| 491 | return result; |
| 492 | } |
| 493 | |
| 494 | /** |
| 495 | * Sends Virtual Cable Unplug to currently connected host. |
| 496 | * |
| 497 | * @return |
Hansong Zhang | 53f5412 | 2017-12-04 10:31:30 -0800 | [diff] [blame] | 498 | * {@hide} |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 499 | */ |
Ivan Podogov | dd87cd3 | 2016-12-30 14:43:29 +0000 | [diff] [blame] | 500 | public boolean unplug(BluetoothDevice device) { |
| 501 | Log.v(TAG, "unplug(): device=" + device); |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 502 | |
| 503 | boolean result = false; |
| 504 | |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame] | 505 | final IBluetoothHidDevice service = mService; |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 506 | if (service != null) { |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 507 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 508 | result = service.unplug(device); |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 509 | } catch (RemoteException e) { |
| 510 | Log.e(TAG, e.toString()); |
| 511 | } |
| 512 | } else { |
| 513 | Log.w(TAG, "Proxy not attached to service"); |
| 514 | } |
| 515 | |
| 516 | return result; |
| 517 | } |
| 518 | |
| 519 | /** |
Hansong Zhang | 53f5412 | 2017-12-04 10:31:30 -0800 | [diff] [blame] | 520 | * Initiates connection to host which is currently paired with this device. If the application |
| 521 | * is not registered, #connect(BluetoothDevice) will fail. The connection state should be |
| 522 | * tracked by the application by handling callback from |
| 523 | * BluetoothHidDeviceCallback#onConnectionStateChanged. The connection state is not related to |
| 524 | * the return value of this method. |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 525 | * |
Hansong Zhang | ceb84db | 2017-11-08 09:57:12 -0800 | [diff] [blame] | 526 | * @return true if the command is successfully sent; otherwise false. |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 527 | */ |
Ivan Podogov | dd87cd3 | 2016-12-30 14:43:29 +0000 | [diff] [blame] | 528 | public boolean connect(BluetoothDevice device) { |
| 529 | Log.v(TAG, "connect(): device=" + device); |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 530 | |
| 531 | boolean result = false; |
| 532 | |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame] | 533 | final IBluetoothHidDevice service = mService; |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 534 | if (service != null) { |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 535 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 536 | result = service.connect(device); |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 537 | } catch (RemoteException e) { |
| 538 | Log.e(TAG, e.toString()); |
| 539 | } |
| 540 | } else { |
| 541 | Log.w(TAG, "Proxy not attached to service"); |
| 542 | } |
| 543 | |
| 544 | return result; |
| 545 | } |
| 546 | |
| 547 | /** |
Hansong Zhang | 53f5412 | 2017-12-04 10:31:30 -0800 | [diff] [blame] | 548 | * Disconnects from currently connected host. The connection state should be tracked by the |
| 549 | * application by handling callback from BluetoothHidDeviceCallback#onConnectionStateChanged. |
| 550 | * The connection state is not related to the return value of this method. |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 551 | * |
Hansong Zhang | ceb84db | 2017-11-08 09:57:12 -0800 | [diff] [blame] | 552 | * @return true if the command is successfully sent; otherwise false. |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 553 | */ |
Ivan Podogov | dd87cd3 | 2016-12-30 14:43:29 +0000 | [diff] [blame] | 554 | public boolean disconnect(BluetoothDevice device) { |
| 555 | Log.v(TAG, "disconnect(): device=" + device); |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 556 | |
| 557 | boolean result = false; |
| 558 | |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame] | 559 | final IBluetoothHidDevice service = mService; |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 560 | if (service != null) { |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 561 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 562 | result = service.disconnect(device); |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 563 | } catch (RemoteException e) { |
| 564 | Log.e(TAG, e.toString()); |
| 565 | } |
| 566 | } else { |
| 567 | Log.w(TAG, "Proxy not attached to service"); |
| 568 | } |
| 569 | |
| 570 | return result; |
| 571 | } |
| 572 | } |