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