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; |
jovanak | 2a9131f | 2018-10-15 17:50:19 -0700 | [diff] [blame] | 27 | import android.os.UserHandle; |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 28 | import android.util.Log; |
| 29 | |
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; |
Ivan Podogov | 191ce9c | 2018-02-27 17:58:16 +0000 | [diff] [blame] | 32 | import java.util.concurrent.Executor; |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 33 | |
| 34 | /** |
Hansong Zhang | 53f5412 | 2017-12-04 10:31:30 -0800 | [diff] [blame] | 35 | * Provides the public APIs to control the Bluetooth HID Device profile. |
Hansong Zhang | fa377b4 | 2017-09-27 14:17:20 -0700 | [diff] [blame] | 36 | * |
Hansong Zhang | 53f5412 | 2017-12-04 10:31:30 -0800 | [diff] [blame] | 37 | * <p>BluetoothHidDevice is a proxy object for controlling the Bluetooth HID Device Service via IPC. |
| 38 | * Use {@link BluetoothAdapter#getProfileProxy} to get the BluetoothHidDevice proxy object. |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 39 | */ |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame] | 40 | public final class BluetoothHidDevice implements BluetoothProfile { |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame] | 41 | private static final String TAG = BluetoothHidDevice.class.getSimpleName(); |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 42 | |
Ivan Podogov | 0afe190 | 2016-12-23 11:52:21 +0000 | [diff] [blame] | 43 | /** |
Hansong Zhang | 53f5412 | 2017-12-04 10:31:30 -0800 | [diff] [blame] | 44 | * 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] | 45 | * |
| 46 | * <p>This intent will have 3 extras: |
Hansong Zhang | 53f5412 | 2017-12-04 10:31:30 -0800 | [diff] [blame] | 47 | * |
Ivan Podogov | 0afe190 | 2016-12-23 11:52:21 +0000 | [diff] [blame] | 48 | * <ul> |
Hansong Zhang | 53f5412 | 2017-12-04 10:31:30 -0800 | [diff] [blame] | 49 | * <li>{@link #EXTRA_STATE} - The current state of the profile. |
| 50 | * <li>{@link #EXTRA_PREVIOUS_STATE}- The previous state of the profile. |
| 51 | * <li>{@link BluetoothDevice#EXTRA_DEVICE} - The remote device. |
Ivan Podogov | 0afe190 | 2016-12-23 11:52:21 +0000 | [diff] [blame] | 52 | * </ul> |
| 53 | * |
Hansong Zhang | 53f5412 | 2017-12-04 10:31:30 -0800 | [diff] [blame] | 54 | * <p>{@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} can be any of {@link |
| 55 | * #STATE_DISCONNECTED}, {@link #STATE_CONNECTING}, {@link #STATE_CONNECTED}, {@link |
| 56 | * #STATE_DISCONNECTING}. |
Ivan Podogov | 0afe190 | 2016-12-23 11:52:21 +0000 | [diff] [blame] | 57 | * |
Hansong Zhang | 53f5412 | 2017-12-04 10:31:30 -0800 | [diff] [blame] | 58 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission to receive. |
Ivan Podogov | 0afe190 | 2016-12-23 11:52:21 +0000 | [diff] [blame] | 59 | */ |
| 60 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 61 | public static final String ACTION_CONNECTION_STATE_CHANGED = |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame] | 62 | "android.bluetooth.hiddevice.profile.action.CONNECTION_STATE_CHANGED"; |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 63 | |
| 64 | /** |
Ivan Podogov | 191ce9c | 2018-02-27 17:58:16 +0000 | [diff] [blame] | 65 | * Constant representing unspecified HID device subclass. |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 66 | * |
Hansong Zhang | 53f5412 | 2017-12-04 10:31:30 -0800 | [diff] [blame] | 67 | * @see #registerApp (BluetoothHidDeviceAppQosSettings, BluetoothHidDeviceAppQosSettings, |
Ivan Podogov | 191ce9c | 2018-02-27 17:58:16 +0000 | [diff] [blame] | 68 | * BluetoothHidDeviceAppQosSettings, Executor, Callback) |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 69 | */ |
| 70 | public static final byte SUBCLASS1_NONE = (byte) 0x00; |
Ivan Podogov | 191ce9c | 2018-02-27 17:58:16 +0000 | [diff] [blame] | 71 | /** |
| 72 | * Constant representing keyboard subclass. |
| 73 | * |
| 74 | * @see #registerApp (BluetoothHidDeviceAppQosSettings, BluetoothHidDeviceAppQosSettings, |
| 75 | * BluetoothHidDeviceAppQosSettings, Executor, Callback) |
| 76 | */ |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 77 | public static final byte SUBCLASS1_KEYBOARD = (byte) 0x40; |
Ivan Podogov | 191ce9c | 2018-02-27 17:58:16 +0000 | [diff] [blame] | 78 | /** |
| 79 | * Constant representing mouse subclass. |
| 80 | * |
| 81 | * @see #registerApp (BluetoothHidDeviceAppQosSettings, BluetoothHidDeviceAppQosSettings, |
| 82 | * BluetoothHidDeviceAppQosSettings, Executor, Callback) |
| 83 | */ |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 84 | public static final byte SUBCLASS1_MOUSE = (byte) 0x80; |
Ivan Podogov | 191ce9c | 2018-02-27 17:58:16 +0000 | [diff] [blame] | 85 | /** |
| 86 | * Constant representing combo keyboard and mouse subclass. |
| 87 | * |
| 88 | * @see #registerApp (BluetoothHidDeviceAppQosSettings, BluetoothHidDeviceAppQosSettings, |
| 89 | * BluetoothHidDeviceAppQosSettings, Executor, Callback) |
| 90 | */ |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 91 | public static final byte SUBCLASS1_COMBO = (byte) 0xC0; |
| 92 | |
Ivan Podogov | 191ce9c | 2018-02-27 17:58:16 +0000 | [diff] [blame] | 93 | /** |
| 94 | * Constant representing uncategorized HID device subclass. |
| 95 | * |
| 96 | * @see #registerApp (BluetoothHidDeviceAppQosSettings, BluetoothHidDeviceAppQosSettings, |
| 97 | * BluetoothHidDeviceAppQosSettings, Executor, Callback) |
| 98 | */ |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 99 | public static final byte SUBCLASS2_UNCATEGORIZED = (byte) 0x00; |
Ivan Podogov | 191ce9c | 2018-02-27 17:58:16 +0000 | [diff] [blame] | 100 | /** |
| 101 | * Constant representing joystick subclass. |
| 102 | * |
| 103 | * @see #registerApp (BluetoothHidDeviceAppQosSettings, BluetoothHidDeviceAppQosSettings, |
| 104 | * BluetoothHidDeviceAppQosSettings, Executor, Callback) |
| 105 | */ |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 106 | public static final byte SUBCLASS2_JOYSTICK = (byte) 0x01; |
Ivan Podogov | 191ce9c | 2018-02-27 17:58:16 +0000 | [diff] [blame] | 107 | /** |
| 108 | * Constant representing gamepad subclass. |
| 109 | * |
| 110 | * @see #registerApp (BluetoothHidDeviceAppQosSettings, BluetoothHidDeviceAppQosSettings, |
| 111 | * BluetoothHidDeviceAppQosSettings, Executor, Callback) |
| 112 | */ |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 113 | public static final byte SUBCLASS2_GAMEPAD = (byte) 0x02; |
Ivan Podogov | 191ce9c | 2018-02-27 17:58:16 +0000 | [diff] [blame] | 114 | /** |
| 115 | * Constant representing remote control subclass. |
| 116 | * |
| 117 | * @see #registerApp (BluetoothHidDeviceAppQosSettings, BluetoothHidDeviceAppQosSettings, |
| 118 | * BluetoothHidDeviceAppQosSettings, Executor, Callback) |
| 119 | */ |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 120 | public static final byte SUBCLASS2_REMOTE_CONTROL = (byte) 0x03; |
Ivan Podogov | 191ce9c | 2018-02-27 17:58:16 +0000 | [diff] [blame] | 121 | /** |
| 122 | * Constant representing sensing device subclass. |
| 123 | * |
| 124 | * @see #registerApp (BluetoothHidDeviceAppQosSettings, BluetoothHidDeviceAppQosSettings, |
| 125 | * BluetoothHidDeviceAppQosSettings, Executor, Callback) |
| 126 | */ |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 127 | public static final byte SUBCLASS2_SENSING_DEVICE = (byte) 0x04; |
Ivan Podogov | 191ce9c | 2018-02-27 17:58:16 +0000 | [diff] [blame] | 128 | /** |
| 129 | * Constant representing digitizer tablet subclass. |
| 130 | * |
| 131 | * @see #registerApp (BluetoothHidDeviceAppQosSettings, BluetoothHidDeviceAppQosSettings, |
| 132 | * BluetoothHidDeviceAppQosSettings, Executor, Callback) |
| 133 | */ |
Hansong Zhang | 9eb138b | 2017-09-29 09:26:09 -0700 | [diff] [blame] | 134 | public static final byte SUBCLASS2_DIGITIZER_TABLET = (byte) 0x05; |
Ivan Podogov | 191ce9c | 2018-02-27 17:58:16 +0000 | [diff] [blame] | 135 | /** |
| 136 | * Constant representing card reader subclass. |
| 137 | * |
| 138 | * @see #registerApp (BluetoothHidDeviceAppQosSettings, BluetoothHidDeviceAppQosSettings, |
| 139 | * BluetoothHidDeviceAppQosSettings, Executor, Callback) |
| 140 | */ |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 141 | public static final byte SUBCLASS2_CARD_READER = (byte) 0x06; |
| 142 | |
| 143 | /** |
Ivan Podogov | 191ce9c | 2018-02-27 17:58:16 +0000 | [diff] [blame] | 144 | * Constant representing HID Input Report type. |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 145 | * |
Ivan Podogov | 191ce9c | 2018-02-27 17:58:16 +0000 | [diff] [blame] | 146 | * @see Callback#onGetReport(BluetoothDevice, byte, byte, int) |
| 147 | * @see Callback#onSetReport(BluetoothDevice, byte, byte, byte[]) |
| 148 | * @see Callback#onInterruptData(BluetoothDevice, byte, byte[]) |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 149 | */ |
| 150 | public static final byte REPORT_TYPE_INPUT = (byte) 1; |
Ivan Podogov | 191ce9c | 2018-02-27 17:58:16 +0000 | [diff] [blame] | 151 | /** |
| 152 | * Constant representing HID Output Report type. |
| 153 | * |
| 154 | * @see Callback#onGetReport(BluetoothDevice, byte, byte, int) |
| 155 | * @see Callback#onSetReport(BluetoothDevice, byte, byte, byte[]) |
| 156 | * @see Callback#onInterruptData(BluetoothDevice, byte, byte[]) |
| 157 | */ |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 158 | public static final byte REPORT_TYPE_OUTPUT = (byte) 2; |
Ivan Podogov | 191ce9c | 2018-02-27 17:58:16 +0000 | [diff] [blame] | 159 | /** |
| 160 | * Constant representing HID Feature Report type. |
| 161 | * |
| 162 | * @see Callback#onGetReport(BluetoothDevice, byte, byte, int) |
| 163 | * @see Callback#onSetReport(BluetoothDevice, byte, byte, byte[]) |
| 164 | * @see Callback#onInterruptData(BluetoothDevice, byte, byte[]) |
| 165 | */ |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 166 | public static final byte REPORT_TYPE_FEATURE = (byte) 3; |
| 167 | |
| 168 | /** |
Ivan Podogov | 191ce9c | 2018-02-27 17:58:16 +0000 | [diff] [blame] | 169 | * Constant representing success response for Set Report. |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 170 | * |
Ivan Podogov | 191ce9c | 2018-02-27 17:58:16 +0000 | [diff] [blame] | 171 | * @see Callback#onSetReport(BluetoothDevice, byte, byte, byte[]) |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 172 | */ |
| 173 | public static final byte ERROR_RSP_SUCCESS = (byte) 0; |
Ivan Podogov | 191ce9c | 2018-02-27 17:58:16 +0000 | [diff] [blame] | 174 | /** |
| 175 | * Constant representing error response for Set Report due to "not ready". |
| 176 | * |
| 177 | * @see Callback#onSetReport(BluetoothDevice, byte, byte, byte[]) |
| 178 | */ |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 179 | public static final byte ERROR_RSP_NOT_READY = (byte) 1; |
Ivan Podogov | 191ce9c | 2018-02-27 17:58:16 +0000 | [diff] [blame] | 180 | /** |
| 181 | * Constant representing error response for Set Report due to "invalid report ID". |
| 182 | * |
| 183 | * @see Callback#onSetReport(BluetoothDevice, byte, byte, byte[]) |
| 184 | */ |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 185 | public static final byte ERROR_RSP_INVALID_RPT_ID = (byte) 2; |
Ivan Podogov | 191ce9c | 2018-02-27 17:58:16 +0000 | [diff] [blame] | 186 | /** |
| 187 | * Constant representing error response for Set Report due to "unsupported request". |
| 188 | * |
| 189 | * @see Callback#onSetReport(BluetoothDevice, byte, byte, byte[]) |
| 190 | */ |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 191 | public static final byte ERROR_RSP_UNSUPPORTED_REQ = (byte) 3; |
Ivan Podogov | 191ce9c | 2018-02-27 17:58:16 +0000 | [diff] [blame] | 192 | /** |
| 193 | * Constant representing error response for Set Report due to "invalid parameter". |
| 194 | * |
| 195 | * @see Callback#onSetReport(BluetoothDevice, byte, byte, byte[]) |
| 196 | */ |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 197 | public static final byte ERROR_RSP_INVALID_PARAM = (byte) 4; |
Ivan Podogov | 191ce9c | 2018-02-27 17:58:16 +0000 | [diff] [blame] | 198 | /** |
| 199 | * Constant representing error response for Set Report with unknown reason. |
| 200 | * |
| 201 | * @see Callback#onSetReport(BluetoothDevice, byte, byte, byte[]) |
| 202 | */ |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 203 | public static final byte ERROR_RSP_UNKNOWN = (byte) 14; |
| 204 | |
| 205 | /** |
Ivan Podogov | 191ce9c | 2018-02-27 17:58:16 +0000 | [diff] [blame] | 206 | * Constant representing boot protocol mode used set by host. Default is always {@link |
Hansong Zhang | 53f5412 | 2017-12-04 10:31:30 -0800 | [diff] [blame] | 207 | * #PROTOCOL_REPORT_MODE} unless notified otherwise. |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 208 | * |
Ivan Podogov | 191ce9c | 2018-02-27 17:58:16 +0000 | [diff] [blame] | 209 | * @see Callback#onSetProtocol(BluetoothDevice, byte) |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 210 | */ |
| 211 | public static final byte PROTOCOL_BOOT_MODE = (byte) 0; |
Ivan Podogov | 191ce9c | 2018-02-27 17:58:16 +0000 | [diff] [blame] | 212 | /** |
| 213 | * Constant representing report protocol mode used set by host. Default is always {@link |
| 214 | * #PROTOCOL_REPORT_MODE} unless notified otherwise. |
| 215 | * |
| 216 | * @see Callback#onSetProtocol(BluetoothDevice, byte) |
| 217 | */ |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 218 | public static final byte PROTOCOL_REPORT_MODE = (byte) 1; |
| 219 | |
Ivan Podogov | 191ce9c | 2018-02-27 17:58:16 +0000 | [diff] [blame] | 220 | /** |
| 221 | * The template class that applications use to call callback functions on events from the HID |
| 222 | * host. Callback functions are wrapped in this class and registered to the Android system |
| 223 | * during app registration. |
| 224 | */ |
| 225 | public abstract static class Callback { |
| 226 | |
| 227 | private static final String TAG = "BluetoothHidDevCallback"; |
| 228 | |
| 229 | /** |
| 230 | * Callback called when application registration state changes. Usually it's called due to |
| 231 | * either {@link BluetoothHidDevice#registerApp (String, String, String, byte, byte[], |
| 232 | * Executor, Callback)} or {@link BluetoothHidDevice#unregisterApp()} , but can be also |
| 233 | * unsolicited in case e.g. Bluetooth was turned off in which case application is |
| 234 | * unregistered automatically. |
| 235 | * |
| 236 | * @param pluggedDevice {@link BluetoothDevice} object which represents host that currently |
| 237 | * has Virtual Cable established with device. Only valid when application is registered, |
| 238 | * can be <code>null</code>. |
| 239 | * @param registered <code>true</code> if application is registered, <code>false</code> |
| 240 | * otherwise. |
| 241 | */ |
| 242 | public void onAppStatusChanged(BluetoothDevice pluggedDevice, boolean registered) { |
| 243 | Log.d( |
| 244 | TAG, |
| 245 | "onAppStatusChanged: pluggedDevice=" |
| 246 | + pluggedDevice |
| 247 | + " registered=" |
| 248 | + registered); |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Callback called when connection state with remote host was changed. Application can |
| 253 | * assume than Virtual Cable is established when called with {@link |
| 254 | * BluetoothProfile#STATE_CONNECTED} <code>state</code>. |
| 255 | * |
| 256 | * @param device {@link BluetoothDevice} object representing host device which connection |
| 257 | * state was changed. |
| 258 | * @param state Connection state as defined in {@link BluetoothProfile}. |
| 259 | */ |
| 260 | public void onConnectionStateChanged(BluetoothDevice device, int state) { |
| 261 | Log.d(TAG, "onConnectionStateChanged: device=" + device + " state=" + state); |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Callback called when GET_REPORT is received from remote host. Should be replied by |
| 266 | * application using {@link BluetoothHidDevice#replyReport(BluetoothDevice, byte, byte, |
| 267 | * byte[])}. |
| 268 | * |
| 269 | * @param type Requested Report Type. |
| 270 | * @param id Requested Report Id, can be 0 if no Report Id are defined in descriptor. |
| 271 | * @param bufferSize Requested buffer size, application shall respond with at least given |
| 272 | * number of bytes. |
| 273 | */ |
| 274 | public void onGetReport(BluetoothDevice device, byte type, byte id, int bufferSize) { |
| 275 | Log.d( |
| 276 | TAG, |
| 277 | "onGetReport: device=" |
| 278 | + device |
| 279 | + " type=" |
| 280 | + type |
| 281 | + " id=" |
| 282 | + id |
| 283 | + " bufferSize=" |
| 284 | + bufferSize); |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Callback called when SET_REPORT is received from remote host. In case received data are |
| 289 | * invalid, application shall respond with {@link |
| 290 | * BluetoothHidDevice#reportError(BluetoothDevice, byte)}. |
| 291 | * |
| 292 | * @param type Report Type. |
| 293 | * @param id Report Id. |
| 294 | * @param data Report data. |
| 295 | */ |
| 296 | public void onSetReport(BluetoothDevice device, byte type, byte id, byte[] data) { |
| 297 | Log.d(TAG, "onSetReport: device=" + device + " type=" + type + " id=" + id); |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Callback called when SET_PROTOCOL is received from remote host. Application shall use |
| 302 | * this information to send only reports valid for given protocol mode. By default, {@link |
| 303 | * BluetoothHidDevice#PROTOCOL_REPORT_MODE} shall be assumed. |
| 304 | * |
| 305 | * @param protocol Protocol Mode. |
| 306 | */ |
| 307 | public void onSetProtocol(BluetoothDevice device, byte protocol) { |
| 308 | Log.d(TAG, "onSetProtocol: device=" + device + " protocol=" + protocol); |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * Callback called when report data is received over interrupt channel. Report Type is |
| 313 | * assumed to be {@link BluetoothHidDevice#REPORT_TYPE_OUTPUT}. |
| 314 | * |
| 315 | * @param reportId Report Id. |
| 316 | * @param data Report data. |
| 317 | */ |
| 318 | public void onInterruptData(BluetoothDevice device, byte reportId, byte[] data) { |
| 319 | Log.d(TAG, "onInterruptData: device=" + device + " reportId=" + reportId); |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Callback called when Virtual Cable is removed. After this callback is received connection |
| 324 | * will be disconnected automatically. |
| 325 | */ |
| 326 | public void onVirtualCableUnplug(BluetoothDevice device) { |
| 327 | Log.d(TAG, "onVirtualCableUnplug: device=" + device); |
| 328 | } |
| 329 | } |
| 330 | |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 331 | private Context mContext; |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 332 | private ServiceListener mServiceListener; |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame] | 333 | private volatile IBluetoothHidDevice mService; |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 334 | private BluetoothAdapter mAdapter; |
| 335 | |
Ivan Podogov | 191ce9c | 2018-02-27 17:58:16 +0000 | [diff] [blame] | 336 | private static class CallbackWrapper extends IBluetoothHidDeviceCallback.Stub { |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 337 | |
Ivan Podogov | 191ce9c | 2018-02-27 17:58:16 +0000 | [diff] [blame] | 338 | private final Executor mExecutor; |
| 339 | private final Callback mCallback; |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 340 | |
Ivan Podogov | 191ce9c | 2018-02-27 17:58:16 +0000 | [diff] [blame] | 341 | CallbackWrapper(Executor executor, Callback callback) { |
| 342 | mExecutor = executor; |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 343 | mCallback = callback; |
| 344 | } |
| 345 | |
| 346 | @Override |
Hansong Zhang | 7e527b7 | 2017-11-30 16:37:05 -0800 | [diff] [blame] | 347 | public void onAppStatusChanged(BluetoothDevice pluggedDevice, boolean registered) { |
Ivan Podogov | 191ce9c | 2018-02-27 17:58:16 +0000 | [diff] [blame] | 348 | clearCallingIdentity(); |
| 349 | mExecutor.execute(() -> mCallback.onAppStatusChanged(pluggedDevice, registered)); |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | @Override |
| 353 | public void onConnectionStateChanged(BluetoothDevice device, int state) { |
Ivan Podogov | 191ce9c | 2018-02-27 17:58:16 +0000 | [diff] [blame] | 354 | clearCallingIdentity(); |
| 355 | mExecutor.execute(() -> mCallback.onConnectionStateChanged(device, state)); |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | @Override |
Ivan Podogov | dd87cd3 | 2016-12-30 14:43:29 +0000 | [diff] [blame] | 359 | public void onGetReport(BluetoothDevice device, byte type, byte id, int bufferSize) { |
Ivan Podogov | 191ce9c | 2018-02-27 17:58:16 +0000 | [diff] [blame] | 360 | clearCallingIdentity(); |
| 361 | mExecutor.execute(() -> mCallback.onGetReport(device, type, id, bufferSize)); |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | @Override |
Ivan Podogov | dd87cd3 | 2016-12-30 14:43:29 +0000 | [diff] [blame] | 365 | public void onSetReport(BluetoothDevice device, byte type, byte id, byte[] data) { |
Ivan Podogov | 191ce9c | 2018-02-27 17:58:16 +0000 | [diff] [blame] | 366 | clearCallingIdentity(); |
| 367 | mExecutor.execute(() -> mCallback.onSetReport(device, type, id, data)); |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | @Override |
Ivan Podogov | dd87cd3 | 2016-12-30 14:43:29 +0000 | [diff] [blame] | 371 | public void onSetProtocol(BluetoothDevice device, byte protocol) { |
Ivan Podogov | 191ce9c | 2018-02-27 17:58:16 +0000 | [diff] [blame] | 372 | clearCallingIdentity(); |
| 373 | mExecutor.execute(() -> mCallback.onSetProtocol(device, protocol)); |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | @Override |
Hansong Zhang | 4232427 | 2017-12-18 15:18:39 -0800 | [diff] [blame] | 377 | public void onInterruptData(BluetoothDevice device, byte reportId, byte[] data) { |
Ivan Podogov | 191ce9c | 2018-02-27 17:58:16 +0000 | [diff] [blame] | 378 | clearCallingIdentity(); |
| 379 | mExecutor.execute(() -> mCallback.onInterruptData(device, reportId, data)); |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | @Override |
Ivan Podogov | dd87cd3 | 2016-12-30 14:43:29 +0000 | [diff] [blame] | 383 | public void onVirtualCableUnplug(BluetoothDevice device) { |
Ivan Podogov | 191ce9c | 2018-02-27 17:58:16 +0000 | [diff] [blame] | 384 | clearCallingIdentity(); |
| 385 | mExecutor.execute(() -> mCallback.onVirtualCableUnplug(device)); |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 386 | } |
| 387 | } |
| 388 | |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 389 | private final IBluetoothStateChangeCallback mBluetoothStateChangeCallback = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 390 | new IBluetoothStateChangeCallback.Stub() { |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 391 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 392 | public void onBluetoothStateChange(boolean up) { |
| 393 | Log.d(TAG, "onBluetoothStateChange: up=" + up); |
| 394 | synchronized (mConnection) { |
Hansong Zhang | fa377b4 | 2017-09-27 14:17:20 -0700 | [diff] [blame] | 395 | if (up) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 396 | try { |
| 397 | if (mService == null) { |
| 398 | Log.d(TAG, "Binding HID Device service..."); |
| 399 | doBind(); |
| 400 | } |
| 401 | } catch (IllegalStateException e) { |
Hansong Zhang | 53f5412 | 2017-12-04 10:31:30 -0800 | [diff] [blame] | 402 | Log.e(TAG, "onBluetoothStateChange: could not bind to HID Dev " |
| 403 | + "service: ", e); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 404 | } catch (SecurityException e) { |
Hansong Zhang | 53f5412 | 2017-12-04 10:31:30 -0800 | [diff] [blame] | 405 | Log.e(TAG, "onBluetoothStateChange: could not bind to HID Dev " |
| 406 | + "service: ", e); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 407 | } |
Hansong Zhang | fa377b4 | 2017-09-27 14:17:20 -0700 | [diff] [blame] | 408 | } else { |
| 409 | Log.d(TAG, "Unbinding service..."); |
| 410 | doUnbind(); |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 411 | } |
| 412 | } |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 413 | } |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 414 | }; |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 415 | |
Hansong Zhang | 53f5412 | 2017-12-04 10:31:30 -0800 | [diff] [blame] | 416 | private final ServiceConnection mConnection = |
| 417 | new ServiceConnection() { |
| 418 | public void onServiceConnected(ComponentName className, IBinder service) { |
| 419 | Log.d(TAG, "onServiceConnected()"); |
| 420 | mService = IBluetoothHidDevice.Stub.asInterface(service); |
| 421 | if (mServiceListener != null) { |
| 422 | mServiceListener.onServiceConnected( |
| 423 | BluetoothProfile.HID_DEVICE, BluetoothHidDevice.this); |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | public void onServiceDisconnected(ComponentName className) { |
| 428 | Log.d(TAG, "onServiceDisconnected()"); |
| 429 | mService = null; |
| 430 | if (mServiceListener != null) { |
| 431 | mServiceListener.onServiceDisconnected(BluetoothProfile.HID_DEVICE); |
| 432 | } |
| 433 | } |
| 434 | }; |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 435 | |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame] | 436 | BluetoothHidDevice(Context context, ServiceListener listener) { |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 437 | mContext = context; |
| 438 | mServiceListener = listener; |
| 439 | mAdapter = BluetoothAdapter.getDefaultAdapter(); |
| 440 | |
| 441 | IBluetoothManager mgr = mAdapter.getBluetoothManager(); |
| 442 | if (mgr != null) { |
| 443 | try { |
| 444 | mgr.registerStateChangeCallback(mBluetoothStateChangeCallback); |
| 445 | } catch (RemoteException e) { |
| 446 | e.printStackTrace(); |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | doBind(); |
| 451 | } |
| 452 | |
| 453 | boolean doBind() { |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame] | 454 | Intent intent = new Intent(IBluetoothHidDevice.class.getName()); |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 455 | ComponentName comp = intent.resolveSystemService(mContext.getPackageManager(), 0); |
| 456 | intent.setComponent(comp); |
| 457 | if (comp == null || !mContext.bindServiceAsUser(intent, mConnection, 0, |
jovanak | 2a9131f | 2018-10-15 17:50:19 -0700 | [diff] [blame] | 458 | UserHandle.CURRENT_OR_SELF)) { |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 459 | Log.e(TAG, "Could not bind to Bluetooth HID Device Service with " + intent); |
| 460 | return false; |
| 461 | } |
| 462 | Log.d(TAG, "Bound to HID Device Service"); |
| 463 | return true; |
| 464 | } |
| 465 | |
Hansong Zhang | fa377b4 | 2017-09-27 14:17:20 -0700 | [diff] [blame] | 466 | void doUnbind() { |
Hansong Zhang | fa377b4 | 2017-09-27 14:17:20 -0700 | [diff] [blame] | 467 | if (mService != null) { |
| 468 | mService = null; |
| 469 | try { |
| 470 | mContext.unbindService(mConnection); |
| 471 | } catch (IllegalArgumentException e) { |
| 472 | Log.e(TAG, "Unable to unbind HidDevService", e); |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 477 | void close() { |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 478 | IBluetoothManager mgr = mAdapter.getBluetoothManager(); |
| 479 | if (mgr != null) { |
| 480 | try { |
| 481 | mgr.unregisterStateChangeCallback(mBluetoothStateChangeCallback); |
| 482 | } catch (RemoteException e) { |
| 483 | e.printStackTrace(); |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | synchronized (mConnection) { |
Hansong Zhang | fa377b4 | 2017-09-27 14:17:20 -0700 | [diff] [blame] | 488 | doUnbind(); |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 489 | } |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 490 | mServiceListener = null; |
| 491 | } |
| 492 | |
Hansong Zhang | 53f5412 | 2017-12-04 10:31:30 -0800 | [diff] [blame] | 493 | /** {@inheritDoc} */ |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 494 | @Override |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 495 | public List<BluetoothDevice> getConnectedDevices() { |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame] | 496 | final IBluetoothHidDevice service = mService; |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 497 | if (service != null) { |
Ivan Podogov | dd87cd3 | 2016-12-30 14:43:29 +0000 | [diff] [blame] | 498 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 499 | return service.getConnectedDevices(); |
Ivan Podogov | dd87cd3 | 2016-12-30 14:43:29 +0000 | [diff] [blame] | 500 | } catch (RemoteException e) { |
| 501 | Log.e(TAG, e.toString()); |
| 502 | } |
| 503 | } else { |
| 504 | Log.w(TAG, "Proxy not attached to service"); |
| 505 | } |
| 506 | |
Ivan Podogov | 191ce9c | 2018-02-27 17:58:16 +0000 | [diff] [blame] | 507 | return new ArrayList<>(); |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 508 | } |
| 509 | |
Hansong Zhang | 53f5412 | 2017-12-04 10:31:30 -0800 | [diff] [blame] | 510 | /** {@inheritDoc} */ |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 511 | @Override |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 512 | public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame] | 513 | final IBluetoothHidDevice service = mService; |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 514 | if (service != null) { |
Ivan Podogov | dd87cd3 | 2016-12-30 14:43:29 +0000 | [diff] [blame] | 515 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 516 | return service.getDevicesMatchingConnectionStates(states); |
Ivan Podogov | dd87cd3 | 2016-12-30 14:43:29 +0000 | [diff] [blame] | 517 | } catch (RemoteException e) { |
| 518 | Log.e(TAG, e.toString()); |
| 519 | } |
| 520 | } else { |
| 521 | Log.w(TAG, "Proxy not attached to service"); |
| 522 | } |
| 523 | |
Ivan Podogov | 191ce9c | 2018-02-27 17:58:16 +0000 | [diff] [blame] | 524 | return new ArrayList<>(); |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 525 | } |
| 526 | |
Hansong Zhang | 53f5412 | 2017-12-04 10:31:30 -0800 | [diff] [blame] | 527 | /** {@inheritDoc} */ |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 528 | @Override |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 529 | public int getConnectionState(BluetoothDevice device) { |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame] | 530 | final IBluetoothHidDevice service = mService; |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 531 | if (service != null) { |
Ivan Podogov | dd87cd3 | 2016-12-30 14:43:29 +0000 | [diff] [blame] | 532 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 533 | return service.getConnectionState(device); |
Ivan Podogov | dd87cd3 | 2016-12-30 14:43:29 +0000 | [diff] [blame] | 534 | } catch (RemoteException e) { |
| 535 | Log.e(TAG, e.toString()); |
| 536 | } |
| 537 | } else { |
| 538 | Log.w(TAG, "Proxy not attached to service"); |
| 539 | } |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 540 | |
| 541 | return STATE_DISCONNECTED; |
| 542 | } |
| 543 | |
| 544 | /** |
Hansong Zhang | 53f5412 | 2017-12-04 10:31:30 -0800 | [diff] [blame] | 545 | * Registers application to be used for HID device. Connections to HID Device are only possible |
| 546 | * when application is registered. Only one application can be registered at one time. When an |
| 547 | * application is registered, the HID Host service will be disabled until it is unregistered. |
| 548 | * When no longer used, application should be unregistered using {@link #unregisterApp()}. The |
Ivan Podogov | 191ce9c | 2018-02-27 17:58:16 +0000 | [diff] [blame] | 549 | * app will be automatically unregistered if it is not foreground. The registration status |
| 550 | * should be tracked by the application by handling callback from Callback#onAppStatusChanged. |
| 551 | * The app registration status is not related to the return value of this method. |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 552 | * |
Hansong Zhang | 53f5412 | 2017-12-04 10:31:30 -0800 | [diff] [blame] | 553 | * @param sdp {@link BluetoothHidDeviceAppSdpSettings} object of HID Device SDP record. The HID |
| 554 | * Device SDP record is required. |
| 555 | * @param inQos {@link BluetoothHidDeviceAppQosSettings} object of Incoming QoS Settings. The |
| 556 | * Incoming QoS Settings is not required. Use null or default |
| 557 | * BluetoothHidDeviceAppQosSettings.Builder for default values. |
| 558 | * @param outQos {@link BluetoothHidDeviceAppQosSettings} object of Outgoing QoS Settings. The |
| 559 | * Outgoing QoS Settings is not required. Use null or default |
| 560 | * BluetoothHidDeviceAppQosSettings.Builder for default values. |
Ivan Podogov | 191ce9c | 2018-02-27 17:58:16 +0000 | [diff] [blame] | 561 | * @param executor {@link Executor} object on which callback will be executed. The Executor |
| 562 | * object is required. |
| 563 | * @param callback {@link Callback} object to which callback messages will be sent. The Callback |
| 564 | * object is required. |
Hansong Zhang | ceb84db | 2017-11-08 09:57:12 -0800 | [diff] [blame] | 565 | * @return true if the command is successfully sent; otherwise false. |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 566 | */ |
Ivan Podogov | 191ce9c | 2018-02-27 17:58:16 +0000 | [diff] [blame] | 567 | public boolean registerApp( |
| 568 | BluetoothHidDeviceAppSdpSettings sdp, |
| 569 | BluetoothHidDeviceAppQosSettings inQos, |
| 570 | BluetoothHidDeviceAppQosSettings outQos, |
| 571 | Executor executor, |
| 572 | Callback callback) { |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 573 | boolean result = false; |
| 574 | |
Ivan Podogov | 191ce9c | 2018-02-27 17:58:16 +0000 | [diff] [blame] | 575 | if (sdp == null) { |
| 576 | throw new IllegalArgumentException("sdp parameter cannot be null"); |
| 577 | } |
| 578 | |
| 579 | if (executor == null) { |
| 580 | throw new IllegalArgumentException("executor parameter cannot be null"); |
| 581 | } |
| 582 | |
| 583 | if (callback == null) { |
| 584 | throw new IllegalArgumentException("callback parameter cannot be null"); |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 585 | } |
| 586 | |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame] | 587 | final IBluetoothHidDevice service = mService; |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 588 | if (service != null) { |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 589 | try { |
Ivan Podogov | 191ce9c | 2018-02-27 17:58:16 +0000 | [diff] [blame] | 590 | CallbackWrapper cbw = new CallbackWrapper(executor, callback); |
Hansong Zhang | 7e527b7 | 2017-11-30 16:37:05 -0800 | [diff] [blame] | 591 | result = service.registerApp(sdp, inQos, outQos, cbw); |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 592 | } catch (RemoteException e) { |
| 593 | Log.e(TAG, e.toString()); |
| 594 | } |
| 595 | } else { |
| 596 | Log.w(TAG, "Proxy not attached to service"); |
| 597 | } |
| 598 | |
| 599 | return result; |
| 600 | } |
| 601 | |
| 602 | /** |
Hansong Zhang | 53f5412 | 2017-12-04 10:31:30 -0800 | [diff] [blame] | 603 | * Unregisters application. Active connection will be disconnected and no new connections will |
| 604 | * be allowed until registered again using {@link #registerApp |
Hansong Zhang | fa377b4 | 2017-09-27 14:17:20 -0700 | [diff] [blame] | 605 | * (BluetoothHidDeviceAppQosSettings, BluetoothHidDeviceAppQosSettings, |
Ivan Podogov | 191ce9c | 2018-02-27 17:58:16 +0000 | [diff] [blame] | 606 | * BluetoothHidDeviceAppQosSettings, Executor, Callback)}. The registration status should be |
| 607 | * tracked by the application by handling callback from Callback#onAppStatusChanged. The app |
| 608 | * registration status is not related to the return value of this method. |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 609 | * |
Hansong Zhang | ceb84db | 2017-11-08 09:57:12 -0800 | [diff] [blame] | 610 | * @return true if the command is successfully sent; otherwise false. |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 611 | */ |
Hansong Zhang | 7e527b7 | 2017-11-30 16:37:05 -0800 | [diff] [blame] | 612 | public boolean unregisterApp() { |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 613 | boolean result = false; |
| 614 | |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame] | 615 | final IBluetoothHidDevice service = mService; |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 616 | if (service != null) { |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 617 | try { |
Hansong Zhang | 7e527b7 | 2017-11-30 16:37:05 -0800 | [diff] [blame] | 618 | result = service.unregisterApp(); |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 619 | } catch (RemoteException e) { |
| 620 | Log.e(TAG, e.toString()); |
| 621 | } |
| 622 | } else { |
| 623 | Log.w(TAG, "Proxy not attached to service"); |
| 624 | } |
| 625 | |
| 626 | return result; |
| 627 | } |
| 628 | |
| 629 | /** |
| 630 | * Sends report to remote host using interrupt channel. |
| 631 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 632 | * @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] | 633 | * descriptor. |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 634 | * @param data Report data, not including Report Id. |
Hansong Zhang | ceb84db | 2017-11-08 09:57:12 -0800 | [diff] [blame] | 635 | * @return true if the command is successfully sent; otherwise false. |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 636 | */ |
Ivan Podogov | dd87cd3 | 2016-12-30 14:43:29 +0000 | [diff] [blame] | 637 | public boolean sendReport(BluetoothDevice device, int id, byte[] data) { |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 638 | boolean result = false; |
| 639 | |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame] | 640 | final IBluetoothHidDevice service = mService; |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 641 | if (service != null) { |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 642 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 643 | result = service.sendReport(device, id, data); |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 644 | } catch (RemoteException e) { |
| 645 | Log.e(TAG, e.toString()); |
| 646 | } |
| 647 | } else { |
| 648 | Log.w(TAG, "Proxy not attached to service"); |
| 649 | } |
| 650 | |
| 651 | return result; |
| 652 | } |
| 653 | |
| 654 | /** |
Hansong Zhang | 53f5412 | 2017-12-04 10:31:30 -0800 | [diff] [blame] | 655 | * Sends report to remote host as reply for GET_REPORT request from {@link |
Ivan Podogov | 191ce9c | 2018-02-27 17:58:16 +0000 | [diff] [blame] | 656 | * Callback#onGetReport(BluetoothDevice, byte, byte, int)}. |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 657 | * |
| 658 | * @param type Report Type, as in request. |
| 659 | * @param id Report Id, as in request. |
| 660 | * @param data Report data, not including Report Id. |
Hansong Zhang | ceb84db | 2017-11-08 09:57:12 -0800 | [diff] [blame] | 661 | * @return true if the command is successfully sent; otherwise false. |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 662 | */ |
Ivan Podogov | dd87cd3 | 2016-12-30 14:43:29 +0000 | [diff] [blame] | 663 | public boolean replyReport(BluetoothDevice device, byte type, byte id, byte[] data) { |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 664 | boolean result = false; |
| 665 | |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame] | 666 | final IBluetoothHidDevice service = mService; |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 667 | if (service != null) { |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 668 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 669 | result = service.replyReport(device, type, id, data); |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 670 | } catch (RemoteException e) { |
| 671 | Log.e(TAG, e.toString()); |
| 672 | } |
| 673 | } else { |
| 674 | Log.w(TAG, "Proxy not attached to service"); |
| 675 | } |
| 676 | |
| 677 | return result; |
| 678 | } |
| 679 | |
| 680 | /** |
Hansong Zhang | 53f5412 | 2017-12-04 10:31:30 -0800 | [diff] [blame] | 681 | * Sends error handshake message as reply for invalid SET_REPORT request from {@link |
Ivan Podogov | 191ce9c | 2018-02-27 17:58:16 +0000 | [diff] [blame] | 682 | * Callback#onSetReport(BluetoothDevice, byte, byte, byte[])}. |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 683 | * |
| 684 | * @param error Error to be sent for SET_REPORT via HANDSHAKE. |
Hansong Zhang | ceb84db | 2017-11-08 09:57:12 -0800 | [diff] [blame] | 685 | * @return true if the command is successfully sent; otherwise false. |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 686 | */ |
Ivan Podogov | dd87cd3 | 2016-12-30 14:43:29 +0000 | [diff] [blame] | 687 | public boolean reportError(BluetoothDevice device, byte error) { |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 688 | boolean result = false; |
| 689 | |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame] | 690 | final IBluetoothHidDevice service = mService; |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 691 | if (service != null) { |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 692 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 693 | result = service.reportError(device, error); |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 694 | } catch (RemoteException e) { |
| 695 | Log.e(TAG, e.toString()); |
| 696 | } |
| 697 | } else { |
| 698 | Log.w(TAG, "Proxy not attached to service"); |
| 699 | } |
| 700 | |
| 701 | return result; |
| 702 | } |
| 703 | |
| 704 | /** |
Hansong Zhang | baca867 | 2018-04-03 12:12:53 -0700 | [diff] [blame] | 705 | * Gets the application name of the current HidDeviceService user. |
| 706 | * |
| 707 | * @return the current user name, or empty string if cannot get the name |
| 708 | * {@hide} |
| 709 | */ |
| 710 | public String getUserAppName() { |
| 711 | final IBluetoothHidDevice service = mService; |
| 712 | |
| 713 | if (service != null) { |
| 714 | try { |
| 715 | return service.getUserAppName(); |
| 716 | } catch (RemoteException e) { |
| 717 | Log.e(TAG, e.toString()); |
| 718 | } |
| 719 | } else { |
| 720 | Log.w(TAG, "Proxy not attached to service"); |
| 721 | } |
| 722 | |
| 723 | return ""; |
| 724 | } |
| 725 | |
| 726 | /** |
Hansong Zhang | 53f5412 | 2017-12-04 10:31:30 -0800 | [diff] [blame] | 727 | * Initiates connection to host which is currently paired with this device. If the application |
| 728 | * is not registered, #connect(BluetoothDevice) will fail. The connection state should be |
Ivan Podogov | 191ce9c | 2018-02-27 17:58:16 +0000 | [diff] [blame] | 729 | * tracked by the application by handling callback from Callback#onConnectionStateChanged. The |
| 730 | * connection state is not related to the return value of this method. |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 731 | * |
Hansong Zhang | ceb84db | 2017-11-08 09:57:12 -0800 | [diff] [blame] | 732 | * @return true if the command is successfully sent; otherwise false. |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 733 | */ |
Ivan Podogov | dd87cd3 | 2016-12-30 14:43:29 +0000 | [diff] [blame] | 734 | public boolean connect(BluetoothDevice device) { |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 735 | boolean result = false; |
| 736 | |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame] | 737 | final IBluetoothHidDevice service = mService; |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 738 | if (service != null) { |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 739 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 740 | result = service.connect(device); |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 741 | } catch (RemoteException e) { |
| 742 | Log.e(TAG, e.toString()); |
| 743 | } |
| 744 | } else { |
| 745 | Log.w(TAG, "Proxy not attached to service"); |
| 746 | } |
| 747 | |
| 748 | return result; |
| 749 | } |
| 750 | |
| 751 | /** |
Hansong Zhang | 53f5412 | 2017-12-04 10:31:30 -0800 | [diff] [blame] | 752 | * Disconnects from currently connected host. The connection state should be tracked by the |
Ivan Podogov | 191ce9c | 2018-02-27 17:58:16 +0000 | [diff] [blame] | 753 | * application by handling callback from Callback#onConnectionStateChanged. The connection state |
| 754 | * is not related to the return value of this method. |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 755 | * |
Hansong Zhang | ceb84db | 2017-11-08 09:57:12 -0800 | [diff] [blame] | 756 | * @return true if the command is successfully sent; otherwise false. |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 757 | */ |
Ivan Podogov | dd87cd3 | 2016-12-30 14:43:29 +0000 | [diff] [blame] | 758 | public boolean disconnect(BluetoothDevice device) { |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 759 | boolean result = false; |
| 760 | |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame] | 761 | final IBluetoothHidDevice service = mService; |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 762 | if (service != null) { |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 763 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 764 | result = service.disconnect(device); |
Hemant Gupta | e88fd4b | 2014-04-18 11:22:45 +0530 | [diff] [blame] | 765 | } catch (RemoteException e) { |
| 766 | Log.e(TAG, e.toString()); |
| 767 | } |
| 768 | } else { |
| 769 | Log.w(TAG, "Proxy not attached to service"); |
| 770 | } |
| 771 | |
| 772 | return result; |
| 773 | } |
| 774 | } |