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 | |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 227 | final private IBluetoothStateChangeCallback mBluetoothStateChangeCallback = |
| 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 | */ |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 388 | public List<BluetoothDevice> getConnectedDevices() { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 389 | if (VDBG) log("getConnectedDevices()"); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 390 | if (mService != null && isEnabled()) { |
| 391 | try { |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 392 | return mService.getConnectedDevices(); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 393 | } catch (RemoteException e) { |
| 394 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 395 | return new ArrayList<BluetoothDevice>(); |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 396 | } |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 397 | } |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 398 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 399 | return new ArrayList<BluetoothDevice>(); |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | /** |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 403 | * {@inheritDoc} |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 404 | */ |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 405 | public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 406 | if (VDBG) log("getDevicesMatchingStates()"); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 407 | if (mService != null && isEnabled()) { |
| 408 | try { |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 409 | return mService.getDevicesMatchingConnectionStates(states); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 410 | } catch (RemoteException e) { |
| 411 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 412 | return new ArrayList<BluetoothDevice>(); |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 413 | } |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 414 | } |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 415 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 416 | return new ArrayList<BluetoothDevice>(); |
| 417 | } |
| 418 | |
| 419 | /** |
| 420 | * {@inheritDoc} |
| 421 | */ |
| 422 | public int getConnectionState(BluetoothDevice device) { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 423 | if (VDBG) log("getState(" + device + ")"); |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 424 | if (mService != null && isEnabled() && isValidDevice(device)) { |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 425 | try { |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 426 | return mService.getConnectionState(device); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 427 | } catch (RemoteException e) { |
| 428 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 429 | return BluetoothProfile.STATE_DISCONNECTED; |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 430 | } |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 431 | } |
| 432 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 433 | return BluetoothProfile.STATE_DISCONNECTED; |
| 434 | } |
| 435 | |
| 436 | /** |
Jaikumar Ganesh | f878916 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 437 | * Set priority of the profile |
| 438 | * |
| 439 | * <p> The device should already be paired. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 440 | * Priority can be one of {@link #PRIORITY_ON} or |
Jaikumar Ganesh | f878916 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 441 | * {@link #PRIORITY_OFF}, |
| 442 | * |
| 443 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 444 | * permission. |
| 445 | * |
| 446 | * @param device Paired bluetooth device |
| 447 | * @param priority |
| 448 | * @return true if priority is set, false on error |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 449 | * @hide |
| 450 | */ |
| 451 | public boolean setPriority(BluetoothDevice device, int priority) { |
| 452 | if (DBG) log("setPriority(" + device + ", " + priority + ")"); |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 453 | if (mService != null && isEnabled() && isValidDevice(device)) { |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 454 | if (priority != BluetoothProfile.PRIORITY_OFF && |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 455 | priority != BluetoothProfile.PRIORITY_ON) { |
| 456 | return false; |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 457 | } |
| 458 | try { |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 459 | return mService.setPriority(device, priority); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 460 | } catch (RemoteException e) { |
| 461 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 462 | return false; |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 463 | } |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 464 | } |
| 465 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 466 | return false; |
| 467 | } |
| 468 | |
| 469 | /** |
Jaikumar Ganesh | f878916 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 470 | * Get the priority of the profile. |
| 471 | * |
| 472 | * <p> The priority can be any of: |
| 473 | * {@link #PRIORITY_AUTO_CONNECT}, {@link #PRIORITY_OFF}, |
| 474 | * {@link #PRIORITY_ON}, {@link #PRIORITY_UNDEFINED} |
| 475 | * |
| 476 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 477 | * |
| 478 | * @param device Bluetooth device |
| 479 | * @return priority of the device |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 480 | * @hide |
| 481 | */ |
| 482 | public int getPriority(BluetoothDevice device) { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 483 | if (VDBG) log("getPriority(" + device + ")"); |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 484 | if (mService != null && isEnabled() && isValidDevice(device)) { |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 485 | try { |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 486 | return mService.getPriority(device); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 487 | } catch (RemoteException e) { |
| 488 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 489 | return BluetoothProfile.PRIORITY_OFF; |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 490 | } |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 491 | } |
| 492 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 493 | return BluetoothProfile.PRIORITY_OFF; |
| 494 | } |
| 495 | |
Matthew Xie | 9b69399 | 2013-10-10 11:21:40 -0700 | [diff] [blame] | 496 | private final ServiceConnection mConnection = new ServiceConnection() { |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 497 | public void onServiceConnected(ComponentName className, IBinder service) { |
| 498 | if (DBG) Log.d(TAG, "Proxy object connected"); |
Jeff Sharkey | 0a17db1 | 2016-11-04 11:23:46 -0600 | [diff] [blame] | 499 | mService = IBluetoothInputDevice.Stub.asInterface(Binder.allowBlocking(service)); |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 500 | |
| 501 | if (mServiceListener != null) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 502 | mServiceListener.onServiceConnected(BluetoothProfile.INPUT_DEVICE, |
| 503 | BluetoothInputDevice.this); |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 504 | } |
| 505 | } |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 506 | |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 507 | public void onServiceDisconnected(ComponentName className) { |
| 508 | if (DBG) Log.d(TAG, "Proxy object disconnected"); |
| 509 | mService = null; |
| 510 | if (mServiceListener != null) { |
| 511 | mServiceListener.onServiceDisconnected(BluetoothProfile.INPUT_DEVICE); |
| 512 | } |
| 513 | } |
| 514 | }; |
| 515 | |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 516 | private boolean isEnabled() { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 517 | if (mAdapter.getState() == BluetoothAdapter.STATE_ON) return true; |
| 518 | return false; |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | private boolean isValidDevice(BluetoothDevice device) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 522 | if (device == null) return false; |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 523 | |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 524 | if (BluetoothAdapter.checkBluetoothAddress(device.getAddress())) return true; |
| 525 | return false; |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 526 | } |
| 527 | |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 528 | |
| 529 | /** |
| 530 | * Initiate virtual unplug for a HID input device. |
| 531 | * |
| 532 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission. |
| 533 | * |
| 534 | * @param device Remote Bluetooth Device |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 535 | * @return false on immediate error, true otherwise |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 536 | * @hide |
| 537 | */ |
| 538 | public boolean virtualUnplug(BluetoothDevice device) { |
| 539 | if (DBG) log("virtualUnplug(" + device + ")"); |
| 540 | if (mService != null && isEnabled() && isValidDevice(device)) { |
| 541 | try { |
| 542 | return mService.virtualUnplug(device); |
| 543 | } catch (RemoteException e) { |
| 544 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 545 | return false; |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 550 | return false; |
| 551 | |
| 552 | } |
| 553 | |
| 554 | /** |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 555 | * Send Get_Protocol_Mode command to the connected HID input device. |
| 556 | * |
| 557 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission. |
| 558 | * |
| 559 | * @param device Remote Bluetooth Device |
| 560 | * @return false on immediate error, true otherwise |
| 561 | * @hide |
| 562 | */ |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 563 | public boolean getProtocolMode(BluetoothDevice device) { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 564 | if (VDBG) log("getProtocolMode(" + device + ")"); |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 565 | if (mService != null && isEnabled() && isValidDevice(device)) { |
| 566 | try { |
| 567 | return mService.getProtocolMode(device); |
| 568 | } catch (RemoteException e) { |
| 569 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 570 | return false; |
| 571 | } |
| 572 | } |
| 573 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 574 | return false; |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | /** |
| 578 | * Send Set_Protocol_Mode command to the connected HID input device. |
| 579 | * |
| 580 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission. |
| 581 | * |
| 582 | * @param device Remote Bluetooth Device |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 583 | * @return false on immediate error, true otherwise |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 584 | * @hide |
| 585 | */ |
| 586 | public boolean setProtocolMode(BluetoothDevice device, int protocolMode) { |
| 587 | if (DBG) log("setProtocolMode(" + device + ")"); |
| 588 | if (mService != null && isEnabled() && isValidDevice(device)) { |
| 589 | try { |
| 590 | return mService.setProtocolMode(device, protocolMode); |
| 591 | } catch (RemoteException e) { |
| 592 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 593 | return false; |
| 594 | } |
| 595 | } |
| 596 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 597 | return false; |
| 598 | } |
| 599 | |
| 600 | /** |
| 601 | * Send Get_Report command to the connected HID input device. |
| 602 | * |
| 603 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission. |
| 604 | * |
| 605 | * @param device Remote Bluetooth Device |
| 606 | * @param reportType Report type |
| 607 | * @param reportId Report ID |
| 608 | * @param bufferSize Report receiving buffer size |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 609 | * @return false on immediate error, true otherwise |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 610 | * @hide |
| 611 | */ |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 612 | public boolean getReport(BluetoothDevice device, byte reportType, byte reportId, |
| 613 | int bufferSize) { |
| 614 | if (VDBG) { |
| 615 | log( |
| 616 | "getReport(" + device + "), reportType=" + reportType + " reportId=" + reportId |
| 617 | + "bufferSize=" + bufferSize); |
| 618 | } |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 619 | if (mService != null && isEnabled() && isValidDevice(device)) { |
| 620 | try { |
| 621 | return mService.getReport(device, reportType, reportId, bufferSize); |
| 622 | } catch (RemoteException e) { |
| 623 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 624 | return false; |
| 625 | } |
| 626 | } |
| 627 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 628 | return false; |
| 629 | } |
| 630 | |
| 631 | /** |
| 632 | * Send Set_Report command to the connected HID input device. |
| 633 | * |
| 634 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission. |
| 635 | * |
| 636 | * @param device Remote Bluetooth Device |
| 637 | * @param reportType Report type |
| 638 | * @param report Report receiving buffer size |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 639 | * @return false on immediate error, true otherwise |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 640 | * @hide |
| 641 | */ |
| 642 | public boolean setReport(BluetoothDevice device, byte reportType, String report) { |
Mike J. Chen | 8faffa4 | 2014-03-04 17:27:16 -0800 | [diff] [blame] | 643 | if (VDBG) log("setReport(" + device + "), reportType=" + reportType + " report=" + report); |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 644 | if (mService != null && isEnabled() && isValidDevice(device)) { |
| 645 | try { |
| 646 | return mService.setReport(device, reportType, report); |
| 647 | } catch (RemoteException e) { |
| 648 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 649 | return false; |
| 650 | } |
| 651 | } |
| 652 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 653 | return false; |
| 654 | } |
| 655 | |
| 656 | /** |
| 657 | * Send Send_Data command to the connected HID input device. |
| 658 | * |
| 659 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission. |
| 660 | * |
| 661 | * @param device Remote Bluetooth Device |
Ang Li | 707fd39 | 2014-06-25 13:16:01 -0700 | [diff] [blame] | 662 | * @param report Report to send |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 663 | * @return false on immediate error, true otherwise |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 664 | * @hide |
| 665 | */ |
| 666 | public boolean sendData(BluetoothDevice device, String report) { |
| 667 | if (DBG) log("sendData(" + device + "), report=" + report); |
| 668 | if (mService != null && isEnabled() && isValidDevice(device)) { |
| 669 | try { |
| 670 | return mService.sendData(device, report); |
| 671 | } catch (RemoteException e) { |
| 672 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 673 | return false; |
| 674 | } |
| 675 | } |
| 676 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 677 | return false; |
| 678 | } |
Hemant Gupta | cef9ce3 | 2013-07-30 16:09:20 +0530 | [diff] [blame] | 679 | |
| 680 | /** |
| 681 | * Send Get_Idle_Time command to the connected HID input device. |
| 682 | * |
| 683 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission. |
| 684 | * |
| 685 | * @param device Remote Bluetooth Device |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 686 | * @return false on immediate error, true otherwise |
Hemant Gupta | cef9ce3 | 2013-07-30 16:09:20 +0530 | [diff] [blame] | 687 | * @hide |
| 688 | */ |
| 689 | public boolean getIdleTime(BluetoothDevice device) { |
| 690 | if (DBG) log("getIdletime(" + device + ")"); |
| 691 | if (mService != null && isEnabled() && isValidDevice(device)) { |
| 692 | try { |
| 693 | return mService.getIdleTime(device); |
| 694 | } catch (RemoteException e) { |
| 695 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 696 | return false; |
| 697 | } |
| 698 | } |
| 699 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 700 | return false; |
| 701 | } |
| 702 | |
| 703 | /** |
| 704 | * Send Set_Idle_Time command to the connected HID input device. |
| 705 | * |
| 706 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission. |
| 707 | * |
| 708 | * @param device Remote Bluetooth Device |
| 709 | * @param idleTime Idle time to be set on HID Device |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 710 | * @return false on immediate error, true otherwise |
Hemant Gupta | cef9ce3 | 2013-07-30 16:09:20 +0530 | [diff] [blame] | 711 | * @hide |
| 712 | */ |
| 713 | public boolean setIdleTime(BluetoothDevice device, byte idleTime) { |
| 714 | if (DBG) log("setIdletime(" + device + "), idleTime=" + idleTime); |
| 715 | if (mService != null && isEnabled() && isValidDevice(device)) { |
| 716 | try { |
| 717 | return mService.setIdleTime(device, idleTime); |
| 718 | } catch (RemoteException e) { |
| 719 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 720 | return false; |
| 721 | } |
| 722 | } |
| 723 | if (mService == null) Log.w(TAG, "Proxy not attached to service"); |
| 724 | return false; |
| 725 | } |
| 726 | |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 727 | private static void log(String msg) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame^] | 728 | Log.d(TAG, msg); |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 729 | } |
| 730 | } |