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