blob: e44f36e90c75aa17a044dff3585bb9fc25771671 [file] [log] [blame]
Hemant Guptae88fd4b2014-04-18 11:22:45 +05301/*
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
17package android.bluetooth;
18
Ivan Podogov0afe1902016-12-23 11:52:21 +000019import android.annotation.SdkConstant;
20import android.annotation.SdkConstant.SdkConstantType;
Hemant Guptae88fd4b2014-04-18 11:22:45 +053021import android.content.ComponentName;
22import android.content.Context;
23import android.content.Intent;
24import android.content.ServiceConnection;
25import android.os.IBinder;
26import android.os.RemoteException;
jovanak2a9131f2018-10-15 17:50:19 -070027import android.os.UserHandle;
Hemant Guptae88fd4b2014-04-18 11:22:45 +053028import android.util.Log;
29
Ivan Podogovdd87cd32016-12-30 14:43:29 +000030import java.util.ArrayList;
Hemant Guptae88fd4b2014-04-18 11:22:45 +053031import java.util.List;
Ivan Podogov191ce9c2018-02-27 17:58:16 +000032import java.util.concurrent.Executor;
Hemant Guptae88fd4b2014-04-18 11:22:45 +053033
34/**
Hansong Zhang53f54122017-12-04 10:31:30 -080035 * Provides the public APIs to control the Bluetooth HID Device profile.
Hansong Zhangfa377b42017-09-27 14:17:20 -070036 *
Hansong Zhang53f54122017-12-04 10:31:30 -080037 * <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 Guptae88fd4b2014-04-18 11:22:45 +053039 */
Hansong Zhangc26c76c2017-10-20 15:55:59 -070040public final class BluetoothHidDevice implements BluetoothProfile {
Hansong Zhangc26c76c2017-10-20 15:55:59 -070041 private static final String TAG = BluetoothHidDevice.class.getSimpleName();
Hemant Guptae88fd4b2014-04-18 11:22:45 +053042
Ivan Podogov0afe1902016-12-23 11:52:21 +000043 /**
Hansong Zhang53f54122017-12-04 10:31:30 -080044 * Intent used to broadcast the change in connection state of the Input Host profile.
Ivan Podogov0afe1902016-12-23 11:52:21 +000045 *
46 * <p>This intent will have 3 extras:
Hansong Zhang53f54122017-12-04 10:31:30 -080047 *
Ivan Podogov0afe1902016-12-23 11:52:21 +000048 * <ul>
Hansong Zhang53f54122017-12-04 10:31:30 -080049 * <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 Podogov0afe1902016-12-23 11:52:21 +000052 * </ul>
53 *
Hansong Zhang53f54122017-12-04 10:31:30 -080054 * <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 Podogov0afe1902016-12-23 11:52:21 +000057 *
Hansong Zhang53f54122017-12-04 10:31:30 -080058 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission to receive.
Ivan Podogov0afe1902016-12-23 11:52:21 +000059 */
60 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Hemant Guptae88fd4b2014-04-18 11:22:45 +053061 public static final String ACTION_CONNECTION_STATE_CHANGED =
Hansong Zhangc26c76c2017-10-20 15:55:59 -070062 "android.bluetooth.hiddevice.profile.action.CONNECTION_STATE_CHANGED";
Hemant Guptae88fd4b2014-04-18 11:22:45 +053063
64 /**
Ivan Podogov191ce9c2018-02-27 17:58:16 +000065 * Constant representing unspecified HID device subclass.
Hemant Guptae88fd4b2014-04-18 11:22:45 +053066 *
Hansong Zhang53f54122017-12-04 10:31:30 -080067 * @see #registerApp (BluetoothHidDeviceAppQosSettings, BluetoothHidDeviceAppQosSettings,
Ivan Podogov191ce9c2018-02-27 17:58:16 +000068 * BluetoothHidDeviceAppQosSettings, Executor, Callback)
Hemant Guptae88fd4b2014-04-18 11:22:45 +053069 */
70 public static final byte SUBCLASS1_NONE = (byte) 0x00;
Ivan Podogov191ce9c2018-02-27 17:58:16 +000071 /**
72 * Constant representing keyboard subclass.
73 *
74 * @see #registerApp (BluetoothHidDeviceAppQosSettings, BluetoothHidDeviceAppQosSettings,
75 * BluetoothHidDeviceAppQosSettings, Executor, Callback)
76 */
Hemant Guptae88fd4b2014-04-18 11:22:45 +053077 public static final byte SUBCLASS1_KEYBOARD = (byte) 0x40;
Ivan Podogov191ce9c2018-02-27 17:58:16 +000078 /**
79 * Constant representing mouse subclass.
80 *
81 * @see #registerApp (BluetoothHidDeviceAppQosSettings, BluetoothHidDeviceAppQosSettings,
82 * BluetoothHidDeviceAppQosSettings, Executor, Callback)
83 */
Hemant Guptae88fd4b2014-04-18 11:22:45 +053084 public static final byte SUBCLASS1_MOUSE = (byte) 0x80;
Ivan Podogov191ce9c2018-02-27 17:58:16 +000085 /**
86 * Constant representing combo keyboard and mouse subclass.
87 *
88 * @see #registerApp (BluetoothHidDeviceAppQosSettings, BluetoothHidDeviceAppQosSettings,
89 * BluetoothHidDeviceAppQosSettings, Executor, Callback)
90 */
Hemant Guptae88fd4b2014-04-18 11:22:45 +053091 public static final byte SUBCLASS1_COMBO = (byte) 0xC0;
92
Ivan Podogov191ce9c2018-02-27 17:58:16 +000093 /**
94 * Constant representing uncategorized HID device subclass.
95 *
96 * @see #registerApp (BluetoothHidDeviceAppQosSettings, BluetoothHidDeviceAppQosSettings,
97 * BluetoothHidDeviceAppQosSettings, Executor, Callback)
98 */
Hemant Guptae88fd4b2014-04-18 11:22:45 +053099 public static final byte SUBCLASS2_UNCATEGORIZED = (byte) 0x00;
Ivan Podogov191ce9c2018-02-27 17:58:16 +0000100 /**
101 * Constant representing joystick subclass.
102 *
103 * @see #registerApp (BluetoothHidDeviceAppQosSettings, BluetoothHidDeviceAppQosSettings,
104 * BluetoothHidDeviceAppQosSettings, Executor, Callback)
105 */
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530106 public static final byte SUBCLASS2_JOYSTICK = (byte) 0x01;
Ivan Podogov191ce9c2018-02-27 17:58:16 +0000107 /**
108 * Constant representing gamepad subclass.
109 *
110 * @see #registerApp (BluetoothHidDeviceAppQosSettings, BluetoothHidDeviceAppQosSettings,
111 * BluetoothHidDeviceAppQosSettings, Executor, Callback)
112 */
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530113 public static final byte SUBCLASS2_GAMEPAD = (byte) 0x02;
Ivan Podogov191ce9c2018-02-27 17:58:16 +0000114 /**
115 * Constant representing remote control subclass.
116 *
117 * @see #registerApp (BluetoothHidDeviceAppQosSettings, BluetoothHidDeviceAppQosSettings,
118 * BluetoothHidDeviceAppQosSettings, Executor, Callback)
119 */
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530120 public static final byte SUBCLASS2_REMOTE_CONTROL = (byte) 0x03;
Ivan Podogov191ce9c2018-02-27 17:58:16 +0000121 /**
122 * Constant representing sensing device subclass.
123 *
124 * @see #registerApp (BluetoothHidDeviceAppQosSettings, BluetoothHidDeviceAppQosSettings,
125 * BluetoothHidDeviceAppQosSettings, Executor, Callback)
126 */
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530127 public static final byte SUBCLASS2_SENSING_DEVICE = (byte) 0x04;
Ivan Podogov191ce9c2018-02-27 17:58:16 +0000128 /**
129 * Constant representing digitizer tablet subclass.
130 *
131 * @see #registerApp (BluetoothHidDeviceAppQosSettings, BluetoothHidDeviceAppQosSettings,
132 * BluetoothHidDeviceAppQosSettings, Executor, Callback)
133 */
Hansong Zhang9eb138b2017-09-29 09:26:09 -0700134 public static final byte SUBCLASS2_DIGITIZER_TABLET = (byte) 0x05;
Ivan Podogov191ce9c2018-02-27 17:58:16 +0000135 /**
136 * Constant representing card reader subclass.
137 *
138 * @see #registerApp (BluetoothHidDeviceAppQosSettings, BluetoothHidDeviceAppQosSettings,
139 * BluetoothHidDeviceAppQosSettings, Executor, Callback)
140 */
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530141 public static final byte SUBCLASS2_CARD_READER = (byte) 0x06;
142
143 /**
Ivan Podogov191ce9c2018-02-27 17:58:16 +0000144 * Constant representing HID Input Report type.
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530145 *
Ivan Podogov191ce9c2018-02-27 17:58:16 +0000146 * @see Callback#onGetReport(BluetoothDevice, byte, byte, int)
147 * @see Callback#onSetReport(BluetoothDevice, byte, byte, byte[])
148 * @see Callback#onInterruptData(BluetoothDevice, byte, byte[])
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530149 */
150 public static final byte REPORT_TYPE_INPUT = (byte) 1;
Ivan Podogov191ce9c2018-02-27 17:58:16 +0000151 /**
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 Guptae88fd4b2014-04-18 11:22:45 +0530158 public static final byte REPORT_TYPE_OUTPUT = (byte) 2;
Ivan Podogov191ce9c2018-02-27 17:58:16 +0000159 /**
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 Guptae88fd4b2014-04-18 11:22:45 +0530166 public static final byte REPORT_TYPE_FEATURE = (byte) 3;
167
168 /**
Ivan Podogov191ce9c2018-02-27 17:58:16 +0000169 * Constant representing success response for Set Report.
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530170 *
Ivan Podogov191ce9c2018-02-27 17:58:16 +0000171 * @see Callback#onSetReport(BluetoothDevice, byte, byte, byte[])
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530172 */
173 public static final byte ERROR_RSP_SUCCESS = (byte) 0;
Ivan Podogov191ce9c2018-02-27 17:58:16 +0000174 /**
175 * Constant representing error response for Set Report due to "not ready".
176 *
177 * @see Callback#onSetReport(BluetoothDevice, byte, byte, byte[])
178 */
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530179 public static final byte ERROR_RSP_NOT_READY = (byte) 1;
Ivan Podogov191ce9c2018-02-27 17:58:16 +0000180 /**
181 * Constant representing error response for Set Report due to "invalid report ID".
182 *
183 * @see Callback#onSetReport(BluetoothDevice, byte, byte, byte[])
184 */
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530185 public static final byte ERROR_RSP_INVALID_RPT_ID = (byte) 2;
Ivan Podogov191ce9c2018-02-27 17:58:16 +0000186 /**
187 * Constant representing error response for Set Report due to "unsupported request".
188 *
189 * @see Callback#onSetReport(BluetoothDevice, byte, byte, byte[])
190 */
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530191 public static final byte ERROR_RSP_UNSUPPORTED_REQ = (byte) 3;
Ivan Podogov191ce9c2018-02-27 17:58:16 +0000192 /**
193 * Constant representing error response for Set Report due to "invalid parameter".
194 *
195 * @see Callback#onSetReport(BluetoothDevice, byte, byte, byte[])
196 */
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530197 public static final byte ERROR_RSP_INVALID_PARAM = (byte) 4;
Ivan Podogov191ce9c2018-02-27 17:58:16 +0000198 /**
199 * Constant representing error response for Set Report with unknown reason.
200 *
201 * @see Callback#onSetReport(BluetoothDevice, byte, byte, byte[])
202 */
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530203 public static final byte ERROR_RSP_UNKNOWN = (byte) 14;
204
205 /**
Ivan Podogov191ce9c2018-02-27 17:58:16 +0000206 * Constant representing boot protocol mode used set by host. Default is always {@link
Hansong Zhang53f54122017-12-04 10:31:30 -0800207 * #PROTOCOL_REPORT_MODE} unless notified otherwise.
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530208 *
Ivan Podogov191ce9c2018-02-27 17:58:16 +0000209 * @see Callback#onSetProtocol(BluetoothDevice, byte)
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530210 */
211 public static final byte PROTOCOL_BOOT_MODE = (byte) 0;
Ivan Podogov191ce9c2018-02-27 17:58:16 +0000212 /**
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 Guptae88fd4b2014-04-18 11:22:45 +0530218 public static final byte PROTOCOL_REPORT_MODE = (byte) 1;
219
Ivan Podogov191ce9c2018-02-27 17:58:16 +0000220 /**
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 Guptae88fd4b2014-04-18 11:22:45 +0530331 private Context mContext;
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530332 private ServiceListener mServiceListener;
Hansong Zhangc26c76c2017-10-20 15:55:59 -0700333 private volatile IBluetoothHidDevice mService;
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530334 private BluetoothAdapter mAdapter;
335
Ivan Podogov191ce9c2018-02-27 17:58:16 +0000336 private static class CallbackWrapper extends IBluetoothHidDeviceCallback.Stub {
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530337
Ivan Podogov191ce9c2018-02-27 17:58:16 +0000338 private final Executor mExecutor;
339 private final Callback mCallback;
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530340
Ivan Podogov191ce9c2018-02-27 17:58:16 +0000341 CallbackWrapper(Executor executor, Callback callback) {
342 mExecutor = executor;
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530343 mCallback = callback;
344 }
345
346 @Override
Hansong Zhang7e527b72017-11-30 16:37:05 -0800347 public void onAppStatusChanged(BluetoothDevice pluggedDevice, boolean registered) {
Ivan Podogov191ce9c2018-02-27 17:58:16 +0000348 clearCallingIdentity();
349 mExecutor.execute(() -> mCallback.onAppStatusChanged(pluggedDevice, registered));
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530350 }
351
352 @Override
353 public void onConnectionStateChanged(BluetoothDevice device, int state) {
Ivan Podogov191ce9c2018-02-27 17:58:16 +0000354 clearCallingIdentity();
355 mExecutor.execute(() -> mCallback.onConnectionStateChanged(device, state));
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530356 }
357
358 @Override
Ivan Podogovdd87cd32016-12-30 14:43:29 +0000359 public void onGetReport(BluetoothDevice device, byte type, byte id, int bufferSize) {
Ivan Podogov191ce9c2018-02-27 17:58:16 +0000360 clearCallingIdentity();
361 mExecutor.execute(() -> mCallback.onGetReport(device, type, id, bufferSize));
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530362 }
363
364 @Override
Ivan Podogovdd87cd32016-12-30 14:43:29 +0000365 public void onSetReport(BluetoothDevice device, byte type, byte id, byte[] data) {
Ivan Podogov191ce9c2018-02-27 17:58:16 +0000366 clearCallingIdentity();
367 mExecutor.execute(() -> mCallback.onSetReport(device, type, id, data));
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530368 }
369
370 @Override
Ivan Podogovdd87cd32016-12-30 14:43:29 +0000371 public void onSetProtocol(BluetoothDevice device, byte protocol) {
Ivan Podogov191ce9c2018-02-27 17:58:16 +0000372 clearCallingIdentity();
373 mExecutor.execute(() -> mCallback.onSetProtocol(device, protocol));
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530374 }
375
376 @Override
Hansong Zhang42324272017-12-18 15:18:39 -0800377 public void onInterruptData(BluetoothDevice device, byte reportId, byte[] data) {
Ivan Podogov191ce9c2018-02-27 17:58:16 +0000378 clearCallingIdentity();
379 mExecutor.execute(() -> mCallback.onInterruptData(device, reportId, data));
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530380 }
381
382 @Override
Ivan Podogovdd87cd32016-12-30 14:43:29 +0000383 public void onVirtualCableUnplug(BluetoothDevice device) {
Ivan Podogov191ce9c2018-02-27 17:58:16 +0000384 clearCallingIdentity();
385 mExecutor.execute(() -> mCallback.onVirtualCableUnplug(device));
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530386 }
387 }
388
Jack He2992cd02017-08-22 21:21:23 -0700389 private final IBluetoothStateChangeCallback mBluetoothStateChangeCallback =
Jack Hea355e5e2017-08-22 16:06:54 -0700390 new IBluetoothStateChangeCallback.Stub() {
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530391
Jack Hea355e5e2017-08-22 16:06:54 -0700392 public void onBluetoothStateChange(boolean up) {
393 Log.d(TAG, "onBluetoothStateChange: up=" + up);
394 synchronized (mConnection) {
Hansong Zhangfa377b42017-09-27 14:17:20 -0700395 if (up) {
Jack Hea355e5e2017-08-22 16:06:54 -0700396 try {
397 if (mService == null) {
398 Log.d(TAG, "Binding HID Device service...");
399 doBind();
400 }
401 } catch (IllegalStateException e) {
Hansong Zhang53f54122017-12-04 10:31:30 -0800402 Log.e(TAG, "onBluetoothStateChange: could not bind to HID Dev "
403 + "service: ", e);
Jack Hea355e5e2017-08-22 16:06:54 -0700404 } catch (SecurityException e) {
Hansong Zhang53f54122017-12-04 10:31:30 -0800405 Log.e(TAG, "onBluetoothStateChange: could not bind to HID Dev "
406 + "service: ", e);
Jack Hea355e5e2017-08-22 16:06:54 -0700407 }
Hansong Zhangfa377b42017-09-27 14:17:20 -0700408 } else {
409 Log.d(TAG, "Unbinding service...");
410 doUnbind();
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530411 }
412 }
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530413 }
Jack Hea355e5e2017-08-22 16:06:54 -0700414 };
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530415
Hansong Zhang53f54122017-12-04 10:31:30 -0800416 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 Guptae88fd4b2014-04-18 11:22:45 +0530435
Hansong Zhangc26c76c2017-10-20 15:55:59 -0700436 BluetoothHidDevice(Context context, ServiceListener listener) {
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530437 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 Zhangc26c76c2017-10-20 15:55:59 -0700454 Intent intent = new Intent(IBluetoothHidDevice.class.getName());
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530455 ComponentName comp = intent.resolveSystemService(mContext.getPackageManager(), 0);
456 intent.setComponent(comp);
457 if (comp == null || !mContext.bindServiceAsUser(intent, mConnection, 0,
jovanak2a9131f2018-10-15 17:50:19 -0700458 UserHandle.CURRENT_OR_SELF)) {
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530459 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 Zhangfa377b42017-09-27 14:17:20 -0700466 void doUnbind() {
Hansong Zhangfa377b42017-09-27 14:17:20 -0700467 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 Guptae88fd4b2014-04-18 11:22:45 +0530477 void close() {
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530478 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 Zhangfa377b42017-09-27 14:17:20 -0700488 doUnbind();
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530489 }
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530490 mServiceListener = null;
491 }
492
Hansong Zhang53f54122017-12-04 10:31:30 -0800493 /** {@inheritDoc} */
Jack He2992cd02017-08-22 21:21:23 -0700494 @Override
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530495 public List<BluetoothDevice> getConnectedDevices() {
Hansong Zhangc26c76c2017-10-20 15:55:59 -0700496 final IBluetoothHidDevice service = mService;
Jack He16eeac32017-08-17 12:11:18 -0700497 if (service != null) {
Ivan Podogovdd87cd32016-12-30 14:43:29 +0000498 try {
Jack He16eeac32017-08-17 12:11:18 -0700499 return service.getConnectedDevices();
Ivan Podogovdd87cd32016-12-30 14:43:29 +0000500 } catch (RemoteException e) {
501 Log.e(TAG, e.toString());
502 }
503 } else {
504 Log.w(TAG, "Proxy not attached to service");
505 }
506
Ivan Podogov191ce9c2018-02-27 17:58:16 +0000507 return new ArrayList<>();
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530508 }
509
Hansong Zhang53f54122017-12-04 10:31:30 -0800510 /** {@inheritDoc} */
Jack He2992cd02017-08-22 21:21:23 -0700511 @Override
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530512 public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) {
Hansong Zhangc26c76c2017-10-20 15:55:59 -0700513 final IBluetoothHidDevice service = mService;
Jack He16eeac32017-08-17 12:11:18 -0700514 if (service != null) {
Ivan Podogovdd87cd32016-12-30 14:43:29 +0000515 try {
Jack He16eeac32017-08-17 12:11:18 -0700516 return service.getDevicesMatchingConnectionStates(states);
Ivan Podogovdd87cd32016-12-30 14:43:29 +0000517 } catch (RemoteException e) {
518 Log.e(TAG, e.toString());
519 }
520 } else {
521 Log.w(TAG, "Proxy not attached to service");
522 }
523
Ivan Podogov191ce9c2018-02-27 17:58:16 +0000524 return new ArrayList<>();
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530525 }
526
Hansong Zhang53f54122017-12-04 10:31:30 -0800527 /** {@inheritDoc} */
Jack He2992cd02017-08-22 21:21:23 -0700528 @Override
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530529 public int getConnectionState(BluetoothDevice device) {
Hansong Zhangc26c76c2017-10-20 15:55:59 -0700530 final IBluetoothHidDevice service = mService;
Jack He16eeac32017-08-17 12:11:18 -0700531 if (service != null) {
Ivan Podogovdd87cd32016-12-30 14:43:29 +0000532 try {
Jack He16eeac32017-08-17 12:11:18 -0700533 return service.getConnectionState(device);
Ivan Podogovdd87cd32016-12-30 14:43:29 +0000534 } catch (RemoteException e) {
535 Log.e(TAG, e.toString());
536 }
537 } else {
538 Log.w(TAG, "Proxy not attached to service");
539 }
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530540
541 return STATE_DISCONNECTED;
542 }
543
544 /**
Hansong Zhang53f54122017-12-04 10:31:30 -0800545 * 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 Podogov191ce9c2018-02-27 17:58:16 +0000549 * 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 Guptae88fd4b2014-04-18 11:22:45 +0530552 *
Hansong Zhang53f54122017-12-04 10:31:30 -0800553 * @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 Podogov191ce9c2018-02-27 17:58:16 +0000561 * @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 Zhangceb84db2017-11-08 09:57:12 -0800565 * @return true if the command is successfully sent; otherwise false.
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530566 */
Ivan Podogov191ce9c2018-02-27 17:58:16 +0000567 public boolean registerApp(
568 BluetoothHidDeviceAppSdpSettings sdp,
569 BluetoothHidDeviceAppQosSettings inQos,
570 BluetoothHidDeviceAppQosSettings outQos,
571 Executor executor,
572 Callback callback) {
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530573 boolean result = false;
574
Ivan Podogov191ce9c2018-02-27 17:58:16 +0000575 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 Guptae88fd4b2014-04-18 11:22:45 +0530585 }
586
Hansong Zhangc26c76c2017-10-20 15:55:59 -0700587 final IBluetoothHidDevice service = mService;
Jack He16eeac32017-08-17 12:11:18 -0700588 if (service != null) {
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530589 try {
Ivan Podogov191ce9c2018-02-27 17:58:16 +0000590 CallbackWrapper cbw = new CallbackWrapper(executor, callback);
Hansong Zhang7e527b72017-11-30 16:37:05 -0800591 result = service.registerApp(sdp, inQos, outQos, cbw);
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530592 } 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 Zhang53f54122017-12-04 10:31:30 -0800603 * Unregisters application. Active connection will be disconnected and no new connections will
604 * be allowed until registered again using {@link #registerApp
Hansong Zhangfa377b42017-09-27 14:17:20 -0700605 * (BluetoothHidDeviceAppQosSettings, BluetoothHidDeviceAppQosSettings,
Ivan Podogov191ce9c2018-02-27 17:58:16 +0000606 * 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 Guptae88fd4b2014-04-18 11:22:45 +0530609 *
Hansong Zhangceb84db2017-11-08 09:57:12 -0800610 * @return true if the command is successfully sent; otherwise false.
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530611 */
Hansong Zhang7e527b72017-11-30 16:37:05 -0800612 public boolean unregisterApp() {
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530613 boolean result = false;
614
Hansong Zhangc26c76c2017-10-20 15:55:59 -0700615 final IBluetoothHidDevice service = mService;
Jack He16eeac32017-08-17 12:11:18 -0700616 if (service != null) {
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530617 try {
Hansong Zhang7e527b72017-11-30 16:37:05 -0800618 result = service.unregisterApp();
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530619 } 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 Hea355e5e2017-08-22 16:06:54 -0700632 * @param id Report Id, as defined in descriptor. Can be 0 in case Report Id are not defined in
Hansong Zhang53f54122017-12-04 10:31:30 -0800633 * descriptor.
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530634 * @param data Report data, not including Report Id.
Hansong Zhangceb84db2017-11-08 09:57:12 -0800635 * @return true if the command is successfully sent; otherwise false.
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530636 */
Ivan Podogovdd87cd32016-12-30 14:43:29 +0000637 public boolean sendReport(BluetoothDevice device, int id, byte[] data) {
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530638 boolean result = false;
639
Hansong Zhangc26c76c2017-10-20 15:55:59 -0700640 final IBluetoothHidDevice service = mService;
Jack He16eeac32017-08-17 12:11:18 -0700641 if (service != null) {
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530642 try {
Jack He16eeac32017-08-17 12:11:18 -0700643 result = service.sendReport(device, id, data);
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530644 } 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 Zhang53f54122017-12-04 10:31:30 -0800655 * Sends report to remote host as reply for GET_REPORT request from {@link
Ivan Podogov191ce9c2018-02-27 17:58:16 +0000656 * Callback#onGetReport(BluetoothDevice, byte, byte, int)}.
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530657 *
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 Zhangceb84db2017-11-08 09:57:12 -0800661 * @return true if the command is successfully sent; otherwise false.
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530662 */
Ivan Podogovdd87cd32016-12-30 14:43:29 +0000663 public boolean replyReport(BluetoothDevice device, byte type, byte id, byte[] data) {
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530664 boolean result = false;
665
Hansong Zhangc26c76c2017-10-20 15:55:59 -0700666 final IBluetoothHidDevice service = mService;
Jack He16eeac32017-08-17 12:11:18 -0700667 if (service != null) {
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530668 try {
Jack He16eeac32017-08-17 12:11:18 -0700669 result = service.replyReport(device, type, id, data);
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530670 } 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 Zhang53f54122017-12-04 10:31:30 -0800681 * Sends error handshake message as reply for invalid SET_REPORT request from {@link
Ivan Podogov191ce9c2018-02-27 17:58:16 +0000682 * Callback#onSetReport(BluetoothDevice, byte, byte, byte[])}.
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530683 *
684 * @param error Error to be sent for SET_REPORT via HANDSHAKE.
Hansong Zhangceb84db2017-11-08 09:57:12 -0800685 * @return true if the command is successfully sent; otherwise false.
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530686 */
Ivan Podogovdd87cd32016-12-30 14:43:29 +0000687 public boolean reportError(BluetoothDevice device, byte error) {
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530688 boolean result = false;
689
Hansong Zhangc26c76c2017-10-20 15:55:59 -0700690 final IBluetoothHidDevice service = mService;
Jack He16eeac32017-08-17 12:11:18 -0700691 if (service != null) {
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530692 try {
Jack He16eeac32017-08-17 12:11:18 -0700693 result = service.reportError(device, error);
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530694 } 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 Zhangbaca8672018-04-03 12:12:53 -0700705 * 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 Zhang53f54122017-12-04 10:31:30 -0800727 * 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 Podogov191ce9c2018-02-27 17:58:16 +0000729 * 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 Guptae88fd4b2014-04-18 11:22:45 +0530731 *
Hansong Zhangceb84db2017-11-08 09:57:12 -0800732 * @return true if the command is successfully sent; otherwise false.
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530733 */
Ivan Podogovdd87cd32016-12-30 14:43:29 +0000734 public boolean connect(BluetoothDevice device) {
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530735 boolean result = false;
736
Hansong Zhangc26c76c2017-10-20 15:55:59 -0700737 final IBluetoothHidDevice service = mService;
Jack He16eeac32017-08-17 12:11:18 -0700738 if (service != null) {
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530739 try {
Jack He16eeac32017-08-17 12:11:18 -0700740 result = service.connect(device);
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530741 } 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 Zhang53f54122017-12-04 10:31:30 -0800752 * Disconnects from currently connected host. The connection state should be tracked by the
Ivan Podogov191ce9c2018-02-27 17:58:16 +0000753 * application by handling callback from Callback#onConnectionStateChanged. The connection state
754 * is not related to the return value of this method.
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530755 *
Hansong Zhangceb84db2017-11-08 09:57:12 -0800756 * @return true if the command is successfully sent; otherwise false.
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530757 */
Ivan Podogovdd87cd32016-12-30 14:43:29 +0000758 public boolean disconnect(BluetoothDevice device) {
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530759 boolean result = false;
760
Hansong Zhangc26c76c2017-10-20 15:55:59 -0700761 final IBluetoothHidDevice service = mService;
Jack He16eeac32017-08-17 12:11:18 -0700762 if (service != null) {
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530763 try {
Jack He16eeac32017-08-17 12:11:18 -0700764 result = service.disconnect(device);
Hemant Guptae88fd4b2014-04-18 11:22:45 +0530765 } 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}