Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 1 | /* |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 2 | * Copyright (C) 2011 The Android Open Source Project |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 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 | |
| 19 | import android.annotation.SdkConstant; |
| 20 | import android.annotation.SdkConstant.SdkConstantType; |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 21 | import android.content.ComponentName; |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 22 | import android.content.Context; |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 23 | import android.content.Intent; |
| 24 | import android.content.ServiceConnection; |
Jeff Sharkey | 0a17db1 | 2016-11-04 11:23:46 -0600 | [diff] [blame] | 25 | import android.os.Binder; |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 26 | import android.os.IBinder; |
| 27 | import android.os.RemoteException; |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 28 | import android.util.Log; |
| 29 | |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 30 | import java.util.ArrayList; |
| 31 | import java.util.List; |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 32 | |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 33 | |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 34 | /** |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 35 | * This class provides the public APIs to control the Bluetooth Input |
| 36 | * Device Profile. |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 37 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 38 | * <p>BluetoothInputDevice is a proxy object for controlling the Bluetooth |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 39 | * Service via IPC. Use {@link BluetoothAdapter#getProfileProxy} to get |
| 40 | * the BluetoothInputDevice proxy object. |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 41 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 42 | * <p>Each method is protected with its appropriate permission. |
| 43 | * |
| 44 | * @hide |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 45 | */ |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 46 | public final class BluetoothInputDevice implements BluetoothProfile { |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 47 | private static final String TAG = "BluetoothInputDevice"; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 48 | private static final boolean DBG = true; |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 49 | private static final boolean VDBG = false; |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 50 | |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 51 | /** |
| 52 | * Intent used to broadcast the change in connection state of the Input |
| 53 | * Device profile. |
| 54 | * |
| 55 | * <p>This intent will have 3 extras: |
| 56 | * <ul> |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 57 | * <li> {@link #EXTRA_STATE} - The current state of the profile. </li> |
| 58 | * <li> {@link #EXTRA_PREVIOUS_STATE}- The previous state of the profile.</li> |
| 59 | * <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device. </li> |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 60 | * </ul> |
| 61 | * |
| 62 | * <p>{@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} can be any of |
| 63 | * {@link #STATE_DISCONNECTED}, {@link #STATE_CONNECTING}, |
| 64 | * {@link #STATE_CONNECTED}, {@link #STATE_DISCONNECTING}. |
| 65 | * |
| 66 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission to |
| 67 | * receive. |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 68 | */ |
| 69 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 70 | public static final String ACTION_CONNECTION_STATE_CHANGED = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 71 | "android.bluetooth.input.profile.action.CONNECTION_STATE_CHANGED"; |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 72 | |
Jaikumar Ganesh | fbe807d | 2011-01-19 13:59:32 -0800 | [diff] [blame] | 73 | /** |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 74 | * @hide |
| 75 | */ |
| 76 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 77 | public static final String ACTION_PROTOCOL_MODE_CHANGED = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 78 | "android.bluetooth.input.profile.action.PROTOCOL_MODE_CHANGED"; |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 79 | |
Mike J. Chen | d96d5cf | 2014-01-27 17:55:40 -0800 | [diff] [blame] | 80 | /** |
| 81 | * @hide |
| 82 | */ |
| 83 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Mike J. Chen | 8faffa4 | 2014-03-04 17:27:16 -0800 | [diff] [blame] | 84 | public static final String ACTION_HANDSHAKE = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 85 | "android.bluetooth.input.profile.action.HANDSHAKE"; |
Mike J. Chen | 8faffa4 | 2014-03-04 17:27:16 -0800 | [diff] [blame] | 86 | |
| 87 | /** |
| 88 | * @hide |
| 89 | */ |
| 90 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Mike J. Chen | d96d5cf | 2014-01-27 17:55:40 -0800 | [diff] [blame] | 91 | public static final String ACTION_REPORT = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 92 | "android.bluetooth.input.profile.action.REPORT"; |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 93 | |
| 94 | /** |
| 95 | * @hide |
| 96 | */ |
| 97 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 98 | public static final String ACTION_VIRTUAL_UNPLUG_STATUS = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 99 | "android.bluetooth.input.profile.action.VIRTUAL_UNPLUG_STATUS"; |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 100 | |
Hemant Gupta | cef9ce3 | 2013-07-30 16:09:20 +0530 | [diff] [blame] | 101 | /** |
| 102 | * @hide |
| 103 | */ |
| 104 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 105 | public static final String ACTION_IDLE_TIME_CHANGED = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 106 | "android.bluetooth.input.profile.action.IDLE_TIME_CHANGED"; |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 107 | |
| 108 | /** |
Jaikumar Ganesh | fbe807d | 2011-01-19 13:59:32 -0800 | [diff] [blame] | 109 | * Return codes for the connect and disconnect Bluez / Dbus calls. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 110 | * |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 111 | * @hide |
Jaikumar Ganesh | fbe807d | 2011-01-19 13:59:32 -0800 | [diff] [blame] | 112 | */ |
| 113 | public static final int INPUT_DISCONNECT_FAILED_NOT_CONNECTED = 5000; |
| 114 | |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 115 | /** |
| 116 | * @hide |
| 117 | */ |
Jaikumar Ganesh | fbe807d | 2011-01-19 13:59:32 -0800 | [diff] [blame] | 118 | public static final int INPUT_CONNECT_FAILED_ALREADY_CONNECTED = 5001; |
| 119 | |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 120 | /** |
| 121 | * @hide |
| 122 | */ |
Jaikumar Ganesh | fbe807d | 2011-01-19 13:59:32 -0800 | [diff] [blame] | 123 | public static final int INPUT_CONNECT_FAILED_ATTEMPT_FAILED = 5002; |
| 124 | |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 125 | /** |
| 126 | * @hide |
| 127 | */ |
Jaikumar Ganesh | fbe807d | 2011-01-19 13:59:32 -0800 | [diff] [blame] | 128 | public static final int INPUT_OPERATION_GENERIC_FAILURE = 5003; |
| 129 | |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 130 | /** |
| 131 | * @hide |
| 132 | */ |
Jaikumar Ganesh | fbe807d | 2011-01-19 13:59:32 -0800 | [diff] [blame] | 133 | public static final int INPUT_OPERATION_SUCCESS = 5004; |
| 134 | |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 135 | /** |
| 136 | * @hide |
| 137 | */ |
| 138 | public static final int PROTOCOL_REPORT_MODE = 0; |
| 139 | |
| 140 | /** |
| 141 | * @hide |
| 142 | */ |
| 143 | public static final int PROTOCOL_BOOT_MODE = 1; |
| 144 | |
| 145 | /** |
| 146 | * @hide |
| 147 | */ |
| 148 | public static final int PROTOCOL_UNSUPPORTED_MODE = 255; |
| 149 | |
| 150 | /* int reportType, int reportType, int bufferSize */ |
| 151 | /** |
| 152 | * @hide |
| 153 | */ |
Mike J. Chen | 1b47f7c | 2014-01-27 16:27:04 -0800 | [diff] [blame] | 154 | public static final byte REPORT_TYPE_INPUT = 1; |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 155 | |
| 156 | /** |
| 157 | * @hide |
| 158 | */ |
Mike J. Chen | 1b47f7c | 2014-01-27 16:27:04 -0800 | [diff] [blame] | 159 | public static final byte REPORT_TYPE_OUTPUT = 2; |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 160 | |
| 161 | /** |
| 162 | * @hide |
| 163 | */ |
Mike J. Chen | 1b47f7c | 2014-01-27 16:27:04 -0800 | [diff] [blame] | 164 | public static final byte REPORT_TYPE_FEATURE = 3; |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 165 | |
| 166 | /** |
| 167 | * @hide |
| 168 | */ |
| 169 | public static final int VIRTUAL_UNPLUG_STATUS_SUCCESS = 0; |
| 170 | |
| 171 | /** |
| 172 | * @hide |
| 173 | */ |
| 174 | public static final int VIRTUAL_UNPLUG_STATUS_FAIL = 1; |
| 175 | |
| 176 | /** |
| 177 | * @hide |
| 178 | */ |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 179 | public static final String EXTRA_PROTOCOL_MODE = |
| 180 | "android.bluetooth.BluetoothInputDevice.extra.PROTOCOL_MODE"; |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 181 | |
| 182 | /** |
| 183 | * @hide |
| 184 | */ |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 185 | public static final String EXTRA_REPORT_TYPE = |
| 186 | "android.bluetooth.BluetoothInputDevice.extra.REPORT_TYPE"; |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 187 | |
| 188 | /** |
| 189 | * @hide |
| 190 | */ |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 191 | public static final String EXTRA_REPORT_ID = |
| 192 | "android.bluetooth.BluetoothInputDevice.extra.REPORT_ID"; |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 193 | |
| 194 | /** |
| 195 | * @hide |
| 196 | */ |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 197 | public static final String EXTRA_REPORT_BUFFER_SIZE = |
| 198 | "android.bluetooth.BluetoothInputDevice.extra.REPORT_BUFFER_SIZE"; |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 199 | |
| 200 | /** |
| 201 | * @hide |
| 202 | */ |
| 203 | public static final String EXTRA_REPORT = "android.bluetooth.BluetoothInputDevice.extra.REPORT"; |
| 204 | |
| 205 | /** |
| 206 | * @hide |
| 207 | */ |
Mike J. Chen | 8faffa4 | 2014-03-04 17:27:16 -0800 | [diff] [blame] | 208 | public static final String EXTRA_STATUS = "android.bluetooth.BluetoothInputDevice.extra.STATUS"; |
| 209 | |
| 210 | /** |
| 211 | * @hide |
| 212 | */ |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 213 | public static final String EXTRA_VIRTUAL_UNPLUG_STATUS = |
| 214 | "android.bluetooth.BluetoothInputDevice.extra.VIRTUAL_UNPLUG_STATUS"; |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 215 | |
Hemant Gupta | cef9ce3 | 2013-07-30 16:09:20 +0530 | [diff] [blame] | 216 | /** |
| 217 | * @hide |
| 218 | */ |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 219 | public static final String EXTRA_IDLE_TIME = |
| 220 | "android.bluetooth.BluetoothInputDevice.extra.IDLE_TIME"; |
Hemant Gupta | cef9ce3 | 2013-07-30 16:09:20 +0530 | [diff] [blame] | 221 | |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 222 | private Context mContext; |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 223 | private ServiceListener mServiceListener; |
| 224 | private BluetoothAdapter mAdapter; |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 225 | private IBluetoothInputDevice mService; |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 226 | |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame^] | 227 | private final IBluetoothStateChangeCallback mBluetoothStateChangeCallback = |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 228 | new IBluetoothStateChangeCallback.Stub() { |
| 229 | public void onBluetoothStateChange(boolean up) { |
| 230 | if (DBG) Log.d(TAG, "onBluetoothStateChange: up=" + up); |
| 231 | if (!up) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 232 | if (VDBG) Log.d(TAG, "Unbinding service..."); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 233 | synchronized (mConnection) { |
| 234 | try { |
| 235 | mService = null; |
| 236 | mContext.unbindService(mConnection); |
| 237 | } catch (Exception re) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 238 | Log.e(TAG, "", re); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | } else { |
| 242 | synchronized (mConnection) { |
| 243 | try { |
| 244 | if (mService == null) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 245 | if (VDBG) Log.d(TAG, "Binding service..."); |
Dianne Hackborn | 221ea89 | 2013-08-04 16:50:16 -0700 | [diff] [blame] | 246 | doBind(); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 247 | } |
| 248 | } catch (Exception re) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 249 | Log.e(TAG, "", re); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | } |
| 253 | } |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 254 | }; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 255 | |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 256 | /** |
| 257 | * Create a BluetoothInputDevice proxy object for interacting with the local |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 258 | * Bluetooth Service which handles the InputDevice profile |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 259 | */ |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 260 | /*package*/ BluetoothInputDevice(Context context, ServiceListener l) { |
| 261 | mContext = context; |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 262 | mServiceListener = l; |
| 263 | mAdapter = BluetoothAdapter.getDefaultAdapter(); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 264 | |
| 265 | IBluetoothManager mgr = mAdapter.getBluetoothManager(); |
| 266 | if (mgr != null) { |
| 267 | try { |
| 268 | mgr.registerStateChangeCallback(mBluetoothStateChangeCallback); |
| 269 | } catch (RemoteException e) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 270 | Log.e(TAG, "", e); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | |
Dianne Hackborn | 221ea89 | 2013-08-04 16:50:16 -0700 | [diff] [blame] | 274 | doBind(); |
| 275 | } |
| 276 | |
| 277 | boolean doBind() { |
| 278 | Intent intent = new Intent(IBluetoothInputDevice.class.getName()); |
| 279 | ComponentName comp = intent.resolveSystemService(mContext.getPackageManager(), 0); |
| 280 | intent.setComponent(comp); |
Dianne Hackborn | 466ce96 | 2014-03-19 18:06:58 -0700 | [diff] [blame] | 281 | if (comp == null || !mContext.bindServiceAsUser(intent, mConnection, 0, |
| 282 | android.os.Process.myUserHandle())) { |
Dianne Hackborn | 221ea89 | 2013-08-04 16:50:16 -0700 | [diff] [blame] | 283 | Log.e(TAG, "Could not bind to Bluetooth HID Service with " + intent); |
| 284 | return false; |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 285 | } |
Dianne Hackborn | 221ea89 | 2013-08-04 16:50:16 -0700 | [diff] [blame] | 286 | return true; |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Jaikumar Ganesh | 9bb2751 | 2011-11-28 09:59:08 -0800 | [diff] [blame] | 289 | /*package*/ void close() { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 290 | if (VDBG) log("close()"); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 291 | IBluetoothManager mgr = mAdapter.getBluetoothManager(); |
| 292 | if (mgr != null) { |
| 293 | try { |
| 294 | mgr.unregisterStateChangeCallback(mBluetoothStateChangeCallback); |
| 295 | } catch (Exception e) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 296 | Log.e(TAG, "", e); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
| 300 | synchronized (mConnection) { |
| 301 | if (mService != null) { |
| 302 | try { |
| 303 | mService = null; |
| 304 | mContext.unbindService(mConnection); |
| 305 | } catch (Exception re) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 306 | Log.e(TAG, "", re); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 307 | } |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 308 | } |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 309 | } |
Jaikumar Ganesh | 9bb2751 | 2011-11-28 09:59:08 -0800 | [diff] [blame] | 310 | mServiceListener = null; |
| 311 | } |
| 312 | |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 313 | /** |
Jaikumar Ganesh | f878916 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 314 | * Initiate connection to a profile of the remote bluetooth device. |
| 315 | * |
| 316 | * <p> The system supports connection to multiple input devices. |
| 317 | * |
| 318 | * <p> This API returns false in scenarios like the profile on the |
| 319 | * device is already connected or Bluetooth is not turned on. |
| 320 | * When this API returns true, it is guaranteed that |
| 321 | * connection state intent for the profile will be broadcasted with |
| 322 | * the state. Users can get the connection state of the profile |
| 323 | * from this intent. |
| 324 | * |
| 325 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 326 | * permission. |
| 327 | * |
| 328 | * @param device Remote Bluetooth Device |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 329 | * @return false on immediate error, true otherwise |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 330 | * @hide |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 331 | */ |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 332 | public boolean connect(BluetoothDevice device) { |
| 333 | if (DBG) log("connect(" + device + ")"); |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 334 | if (mService != null && isEnabled() && isValidDevice(device)) { |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 335 | try { |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 336 | return mService.connect(device); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 337 | } catch (RemoteException e) { |
| 338 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 339 | return false; |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 340 | } |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 341 | } |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 342 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 343 | return false; |
| 344 | } |
| 345 | |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 346 | /** |
Jaikumar Ganesh | f878916 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 347 | * Initiate disconnection from a profile |
| 348 | * |
| 349 | * <p> This API will return false in scenarios like the profile on the |
| 350 | * Bluetooth device is not in connected state etc. When this API returns, |
| 351 | * true, it is guaranteed that the connection state change |
| 352 | * intent will be broadcasted with the state. Users can get the |
| 353 | * disconnection state of the profile from this intent. |
| 354 | * |
| 355 | * <p> If the disconnection is initiated by a remote device, the state |
| 356 | * will transition from {@link #STATE_CONNECTED} to |
| 357 | * {@link #STATE_DISCONNECTED}. If the disconnect is initiated by the |
| 358 | * host (local) device the state will transition from |
| 359 | * {@link #STATE_CONNECTED} to state {@link #STATE_DISCONNECTING} to |
| 360 | * state {@link #STATE_DISCONNECTED}. The transition to |
| 361 | * {@link #STATE_DISCONNECTING} can be used to distinguish between the |
| 362 | * two scenarios. |
| 363 | * |
| 364 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 365 | * permission. |
| 366 | * |
| 367 | * @param device Remote Bluetooth Device |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 368 | * @return false on immediate error, true otherwise |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 369 | * @hide |
| 370 | */ |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 371 | public boolean disconnect(BluetoothDevice device) { |
| 372 | if (DBG) log("disconnect(" + device + ")"); |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 373 | if (mService != null && isEnabled() && isValidDevice(device)) { |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 374 | try { |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 375 | return mService.disconnect(device); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 376 | } catch (RemoteException e) { |
| 377 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 378 | return false; |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 379 | } |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 380 | } |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 381 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 382 | return false; |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | /** |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 386 | * {@inheritDoc} |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 387 | */ |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame^] | 388 | @Override |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 389 | public List<BluetoothDevice> getConnectedDevices() { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 390 | if (VDBG) log("getConnectedDevices()"); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 391 | if (mService != null && isEnabled()) { |
| 392 | try { |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 393 | return mService.getConnectedDevices(); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 394 | } catch (RemoteException e) { |
| 395 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 396 | return new ArrayList<BluetoothDevice>(); |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 397 | } |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 398 | } |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 399 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 400 | return new ArrayList<BluetoothDevice>(); |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | /** |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 404 | * {@inheritDoc} |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 405 | */ |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame^] | 406 | @Override |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 407 | public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 408 | if (VDBG) log("getDevicesMatchingStates()"); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 409 | if (mService != null && isEnabled()) { |
| 410 | try { |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 411 | return mService.getDevicesMatchingConnectionStates(states); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 412 | } catch (RemoteException e) { |
| 413 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 414 | return new ArrayList<BluetoothDevice>(); |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 415 | } |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 416 | } |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 417 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 418 | return new ArrayList<BluetoothDevice>(); |
| 419 | } |
| 420 | |
| 421 | /** |
| 422 | * {@inheritDoc} |
| 423 | */ |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame^] | 424 | @Override |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 425 | public int getConnectionState(BluetoothDevice device) { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 426 | if (VDBG) log("getState(" + device + ")"); |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 427 | if (mService != null && isEnabled() && isValidDevice(device)) { |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 428 | try { |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 429 | return mService.getConnectionState(device); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 430 | } catch (RemoteException e) { |
| 431 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 432 | return BluetoothProfile.STATE_DISCONNECTED; |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 433 | } |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 434 | } |
| 435 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 436 | return BluetoothProfile.STATE_DISCONNECTED; |
| 437 | } |
| 438 | |
| 439 | /** |
Jaikumar Ganesh | f878916 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 440 | * Set priority of the profile |
| 441 | * |
| 442 | * <p> The device should already be paired. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 443 | * Priority can be one of {@link #PRIORITY_ON} or |
Jaikumar Ganesh | f878916 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 444 | * {@link #PRIORITY_OFF}, |
| 445 | * |
| 446 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 447 | * permission. |
| 448 | * |
| 449 | * @param device Paired bluetooth device |
| 450 | * @param priority |
| 451 | * @return true if priority is set, false on error |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 452 | * @hide |
| 453 | */ |
| 454 | public boolean setPriority(BluetoothDevice device, int priority) { |
| 455 | if (DBG) log("setPriority(" + device + ", " + priority + ")"); |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 456 | if (mService != null && isEnabled() && isValidDevice(device)) { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame^] | 457 | if (priority != BluetoothProfile.PRIORITY_OFF |
| 458 | && priority != BluetoothProfile.PRIORITY_ON) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 459 | return false; |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 460 | } |
| 461 | try { |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 462 | return mService.setPriority(device, priority); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 463 | } catch (RemoteException e) { |
| 464 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 465 | return false; |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 466 | } |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 467 | } |
| 468 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 469 | return false; |
| 470 | } |
| 471 | |
| 472 | /** |
Jaikumar Ganesh | f878916 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 473 | * Get the priority of the profile. |
| 474 | * |
| 475 | * <p> The priority can be any of: |
| 476 | * {@link #PRIORITY_AUTO_CONNECT}, {@link #PRIORITY_OFF}, |
| 477 | * {@link #PRIORITY_ON}, {@link #PRIORITY_UNDEFINED} |
| 478 | * |
| 479 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 480 | * |
| 481 | * @param device Bluetooth device |
| 482 | * @return priority of the device |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 483 | * @hide |
| 484 | */ |
| 485 | public int getPriority(BluetoothDevice device) { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 486 | if (VDBG) log("getPriority(" + device + ")"); |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 487 | if (mService != null && isEnabled() && isValidDevice(device)) { |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 488 | try { |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 489 | return mService.getPriority(device); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 490 | } catch (RemoteException e) { |
| 491 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 492 | return BluetoothProfile.PRIORITY_OFF; |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 493 | } |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 494 | } |
| 495 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 496 | return BluetoothProfile.PRIORITY_OFF; |
| 497 | } |
| 498 | |
Matthew Xie | 9b69399 | 2013-10-10 11:21:40 -0700 | [diff] [blame] | 499 | private final ServiceConnection mConnection = new ServiceConnection() { |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 500 | public void onServiceConnected(ComponentName className, IBinder service) { |
| 501 | if (DBG) Log.d(TAG, "Proxy object connected"); |
Jeff Sharkey | 0a17db1 | 2016-11-04 11:23:46 -0600 | [diff] [blame] | 502 | mService = IBluetoothInputDevice.Stub.asInterface(Binder.allowBlocking(service)); |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 503 | |
| 504 | if (mServiceListener != null) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 505 | mServiceListener.onServiceConnected(BluetoothProfile.INPUT_DEVICE, |
| 506 | BluetoothInputDevice.this); |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 507 | } |
| 508 | } |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 509 | |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 510 | public void onServiceDisconnected(ComponentName className) { |
| 511 | if (DBG) Log.d(TAG, "Proxy object disconnected"); |
| 512 | mService = null; |
| 513 | if (mServiceListener != null) { |
| 514 | mServiceListener.onServiceDisconnected(BluetoothProfile.INPUT_DEVICE); |
| 515 | } |
| 516 | } |
| 517 | }; |
| 518 | |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 519 | private boolean isEnabled() { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 520 | if (mAdapter.getState() == BluetoothAdapter.STATE_ON) return true; |
| 521 | return false; |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | private boolean isValidDevice(BluetoothDevice device) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 525 | if (device == null) return false; |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 526 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 527 | if (BluetoothAdapter.checkBluetoothAddress(device.getAddress())) return true; |
| 528 | return false; |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 529 | } |
| 530 | |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 531 | |
| 532 | /** |
| 533 | * Initiate virtual unplug for a HID input device. |
| 534 | * |
| 535 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission. |
| 536 | * |
| 537 | * @param device Remote Bluetooth Device |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 538 | * @return false on immediate error, true otherwise |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 539 | * @hide |
| 540 | */ |
| 541 | public boolean virtualUnplug(BluetoothDevice device) { |
| 542 | if (DBG) log("virtualUnplug(" + device + ")"); |
| 543 | if (mService != null && isEnabled() && isValidDevice(device)) { |
| 544 | try { |
| 545 | return mService.virtualUnplug(device); |
| 546 | } catch (RemoteException e) { |
| 547 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 548 | return false; |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 553 | return false; |
| 554 | |
| 555 | } |
| 556 | |
| 557 | /** |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 558 | * Send Get_Protocol_Mode command to the connected HID input device. |
| 559 | * |
| 560 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission. |
| 561 | * |
| 562 | * @param device Remote Bluetooth Device |
| 563 | * @return false on immediate error, true otherwise |
| 564 | * @hide |
| 565 | */ |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 566 | public boolean getProtocolMode(BluetoothDevice device) { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 567 | if (VDBG) log("getProtocolMode(" + device + ")"); |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 568 | if (mService != null && isEnabled() && isValidDevice(device)) { |
| 569 | try { |
| 570 | return mService.getProtocolMode(device); |
| 571 | } catch (RemoteException e) { |
| 572 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 573 | return false; |
| 574 | } |
| 575 | } |
| 576 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 577 | return false; |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | /** |
| 581 | * Send Set_Protocol_Mode command to the connected HID input device. |
| 582 | * |
| 583 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission. |
| 584 | * |
| 585 | * @param device Remote Bluetooth Device |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 586 | * @return false on immediate error, true otherwise |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 587 | * @hide |
| 588 | */ |
| 589 | public boolean setProtocolMode(BluetoothDevice device, int protocolMode) { |
| 590 | if (DBG) log("setProtocolMode(" + device + ")"); |
| 591 | if (mService != null && isEnabled() && isValidDevice(device)) { |
| 592 | try { |
| 593 | return mService.setProtocolMode(device, protocolMode); |
| 594 | } catch (RemoteException e) { |
| 595 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 596 | return false; |
| 597 | } |
| 598 | } |
| 599 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 600 | return false; |
| 601 | } |
| 602 | |
| 603 | /** |
| 604 | * Send Get_Report command to the connected HID input device. |
| 605 | * |
| 606 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission. |
| 607 | * |
| 608 | * @param device Remote Bluetooth Device |
| 609 | * @param reportType Report type |
| 610 | * @param reportId Report ID |
| 611 | * @param bufferSize Report receiving buffer size |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 612 | * @return false on immediate error, true otherwise |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 613 | * @hide |
| 614 | */ |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 615 | public boolean getReport(BluetoothDevice device, byte reportType, byte reportId, |
| 616 | int bufferSize) { |
| 617 | if (VDBG) { |
| 618 | log( |
| 619 | "getReport(" + device + "), reportType=" + reportType + " reportId=" + reportId |
| 620 | + "bufferSize=" + bufferSize); |
| 621 | } |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 622 | if (mService != null && isEnabled() && isValidDevice(device)) { |
| 623 | try { |
| 624 | return mService.getReport(device, reportType, reportId, bufferSize); |
| 625 | } catch (RemoteException e) { |
| 626 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 627 | return false; |
| 628 | } |
| 629 | } |
| 630 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 631 | return false; |
| 632 | } |
| 633 | |
| 634 | /** |
| 635 | * Send Set_Report command to the connected HID input device. |
| 636 | * |
| 637 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission. |
| 638 | * |
| 639 | * @param device Remote Bluetooth Device |
| 640 | * @param reportType Report type |
| 641 | * @param report Report receiving buffer size |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 642 | * @return false on immediate error, true otherwise |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 643 | * @hide |
| 644 | */ |
| 645 | public boolean setReport(BluetoothDevice device, byte reportType, String report) { |
Mike J. Chen | 8faffa4 | 2014-03-04 17:27:16 -0800 | [diff] [blame] | 646 | if (VDBG) log("setReport(" + device + "), reportType=" + reportType + " report=" + report); |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 647 | if (mService != null && isEnabled() && isValidDevice(device)) { |
| 648 | try { |
| 649 | return mService.setReport(device, reportType, report); |
| 650 | } catch (RemoteException e) { |
| 651 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 652 | return false; |
| 653 | } |
| 654 | } |
| 655 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 656 | return false; |
| 657 | } |
| 658 | |
| 659 | /** |
| 660 | * Send Send_Data command to the connected HID input device. |
| 661 | * |
| 662 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission. |
| 663 | * |
| 664 | * @param device Remote Bluetooth Device |
Ang Li | 707fd39 | 2014-06-25 13:16:01 -0700 | [diff] [blame] | 665 | * @param report Report to send |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 666 | * @return false on immediate error, true otherwise |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 667 | * @hide |
| 668 | */ |
| 669 | public boolean sendData(BluetoothDevice device, String report) { |
| 670 | if (DBG) log("sendData(" + device + "), report=" + report); |
| 671 | if (mService != null && isEnabled() && isValidDevice(device)) { |
| 672 | try { |
| 673 | return mService.sendData(device, report); |
| 674 | } catch (RemoteException e) { |
| 675 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 676 | return false; |
| 677 | } |
| 678 | } |
| 679 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 680 | return false; |
| 681 | } |
Hemant Gupta | cef9ce3 | 2013-07-30 16:09:20 +0530 | [diff] [blame] | 682 | |
| 683 | /** |
| 684 | * Send Get_Idle_Time command to the connected HID input device. |
| 685 | * |
| 686 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission. |
| 687 | * |
| 688 | * @param device Remote Bluetooth Device |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 689 | * @return false on immediate error, true otherwise |
Hemant Gupta | cef9ce3 | 2013-07-30 16:09:20 +0530 | [diff] [blame] | 690 | * @hide |
| 691 | */ |
| 692 | public boolean getIdleTime(BluetoothDevice device) { |
| 693 | if (DBG) log("getIdletime(" + device + ")"); |
| 694 | if (mService != null && isEnabled() && isValidDevice(device)) { |
| 695 | try { |
| 696 | return mService.getIdleTime(device); |
| 697 | } catch (RemoteException e) { |
| 698 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 699 | return false; |
| 700 | } |
| 701 | } |
| 702 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 703 | return false; |
| 704 | } |
| 705 | |
| 706 | /** |
| 707 | * Send Set_Idle_Time command to the connected HID input device. |
| 708 | * |
| 709 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission. |
| 710 | * |
| 711 | * @param device Remote Bluetooth Device |
| 712 | * @param idleTime Idle time to be set on HID Device |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 713 | * @return false on immediate error, true otherwise |
Hemant Gupta | cef9ce3 | 2013-07-30 16:09:20 +0530 | [diff] [blame] | 714 | * @hide |
| 715 | */ |
| 716 | public boolean setIdleTime(BluetoothDevice device, byte idleTime) { |
| 717 | if (DBG) log("setIdletime(" + device + "), idleTime=" + idleTime); |
| 718 | if (mService != null && isEnabled() && isValidDevice(device)) { |
| 719 | try { |
| 720 | return mService.setIdleTime(device, idleTime); |
| 721 | } catch (RemoteException e) { |
| 722 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 723 | return false; |
| 724 | } |
| 725 | } |
| 726 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 727 | return false; |
| 728 | } |
| 729 | |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 730 | private static void log(String msg) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 731 | Log.d(TAG, msg); |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 732 | } |
| 733 | } |