Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.bluetooth; |
| 18 | |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 19 | import android.bluetooth.BluetoothDevice; |
| 20 | import android.bluetooth.BluetoothProfile; |
| 21 | import android.bluetooth.BluetoothProfile.ServiceListener; |
| 22 | import android.bluetooth.IBluetoothManager; |
| 23 | import android.bluetooth.IBluetoothStateChangeCallback; |
| 24 | |
| 25 | import android.content.ComponentName; |
| 26 | import android.content.Context; |
| 27 | import android.content.Intent; |
| 28 | import android.content.ServiceConnection; |
| 29 | import android.os.IBinder; |
| 30 | import android.os.ParcelUuid; |
| 31 | import android.os.RemoteException; |
| 32 | import android.os.ServiceManager; |
| 33 | import android.util.Log; |
| 34 | |
| 35 | import java.util.ArrayList; |
| 36 | import java.util.List; |
| 37 | import java.util.UUID; |
| 38 | |
| 39 | /** |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 40 | * Public API for the Bluetooth GATT Profile. |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 41 | * |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 42 | * <p>This class provides Bluetooth GATT functionality to enable communication |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 43 | * with Bluetooth Smart or Smart Ready devices. |
| 44 | * |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 45 | * <p>To connect to a remote peripheral device, create a {@link BluetoothGattCallback} |
Matthew Xie | 33ec984 | 2013-04-03 00:29:27 -0700 | [diff] [blame] | 46 | * and call {@link BluetoothDevice#connectGatt} to get a instance of this class. |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 47 | * GATT capable devices can be discovered using the Bluetooth device discovery or BLE |
| 48 | * scan process. |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 49 | */ |
| 50 | public final class BluetoothGatt implements BluetoothProfile { |
| 51 | private static final String TAG = "BluetoothGatt"; |
| 52 | private static final boolean DBG = true; |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 53 | private static final boolean VDBG = true; |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 54 | |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 55 | private final Context mContext; |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 56 | private IBluetoothGatt mService; |
| 57 | private BluetoothGattCallback mCallback; |
| 58 | private int mClientIf; |
| 59 | private boolean mAuthRetry = false; |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 60 | private BluetoothDevice mDevice; |
| 61 | private boolean mAutoConnect; |
| 62 | private int mConnState; |
| 63 | private final Object mStateLock = new Object(); |
| 64 | |
| 65 | private static final int CONN_STATE_IDLE = 0; |
| 66 | private static final int CONN_STATE_CONNECTING = 1; |
| 67 | private static final int CONN_STATE_CONNECTED = 2; |
| 68 | private static final int CONN_STATE_DISCONNECTING = 3; |
Matthew Xie | 33ec984 | 2013-04-03 00:29:27 -0700 | [diff] [blame] | 69 | private static final int CONN_STATE_CLOSED = 4; |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 70 | |
| 71 | private List<BluetoothGattService> mServices; |
| 72 | |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 73 | /** A GATT operation completed successfully */ |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 74 | public static final int GATT_SUCCESS = 0; |
| 75 | |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 76 | /** GATT read operation is not permitted */ |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 77 | public static final int GATT_READ_NOT_PERMITTED = 0x2; |
| 78 | |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 79 | /** GATT write operation is not permitted */ |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 80 | public static final int GATT_WRITE_NOT_PERMITTED = 0x3; |
| 81 | |
| 82 | /** Insufficient authentication for a given operation */ |
| 83 | public static final int GATT_INSUFFICIENT_AUTHENTICATION = 0x5; |
| 84 | |
| 85 | /** The given request is not supported */ |
| 86 | public static final int GATT_REQUEST_NOT_SUPPORTED = 0x6; |
| 87 | |
| 88 | /** Insufficient encryption for a given operation */ |
| 89 | public static final int GATT_INSUFFICIENT_ENCRYPTION = 0xf; |
| 90 | |
| 91 | /** A read or write operation was requested with an invalid offset */ |
| 92 | public static final int GATT_INVALID_OFFSET = 0x7; |
| 93 | |
| 94 | /** A write operation exceeds the maximum length of the attribute */ |
| 95 | public static final int GATT_INVALID_ATTRIBUTE_LENGTH = 0xd; |
| 96 | |
Matthew Xie | 90ca807 | 2013-05-28 21:06:50 +0000 | [diff] [blame] | 97 | /** A GATT operation failed, errors other than the above */ |
| 98 | public static final int GATT_FAILURE = 0x101; |
| 99 | |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 100 | /** |
| 101 | * No authentication required. |
| 102 | * @hide |
| 103 | */ |
| 104 | /*package*/ static final int AUTHENTICATION_NONE = 0; |
| 105 | |
| 106 | /** |
| 107 | * Authentication requested; no man-in-the-middle protection required. |
| 108 | * @hide |
| 109 | */ |
| 110 | /*package*/ static final int AUTHENTICATION_NO_MITM = 1; |
| 111 | |
| 112 | /** |
| 113 | * Authentication with man-in-the-middle protection requested. |
| 114 | * @hide |
| 115 | */ |
| 116 | /*package*/ static final int AUTHENTICATION_MITM = 2; |
| 117 | |
| 118 | /** |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 119 | * Bluetooth GATT interface callbacks |
| 120 | */ |
| 121 | private final IBluetoothGattCallback mBluetoothGattCallback = |
| 122 | new IBluetoothGattCallback.Stub() { |
| 123 | /** |
| 124 | * Application interface registered - app is ready to go |
| 125 | * @hide |
| 126 | */ |
| 127 | public void onClientRegistered(int status, int clientIf) { |
| 128 | if (DBG) Log.d(TAG, "onClientRegistered() - status=" + status |
| 129 | + " clientIf=" + clientIf); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 130 | if (VDBG) { |
| 131 | synchronized(mStateLock) { |
| 132 | if (mConnState != CONN_STATE_CONNECTING) { |
| 133 | Log.e(TAG, "Bad connection state: " + mConnState); |
| 134 | } |
| 135 | } |
| 136 | } |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 137 | mClientIf = clientIf; |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 138 | if (status != GATT_SUCCESS) { |
Matthew Xie | 33ec984 | 2013-04-03 00:29:27 -0700 | [diff] [blame] | 139 | mCallback.onConnectionStateChange(BluetoothGatt.this, GATT_FAILURE, |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 140 | BluetoothProfile.STATE_DISCONNECTED); |
| 141 | synchronized(mStateLock) { |
| 142 | mConnState = CONN_STATE_IDLE; |
| 143 | } |
| 144 | return; |
| 145 | } |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 146 | try { |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 147 | mService.clientConnect(mClientIf, mDevice.getAddress(), |
| 148 | !mAutoConnect); // autoConnect is inverse of "isDirect" |
| 149 | } catch (RemoteException e) { |
| 150 | Log.e(TAG,"",e); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Client connection state changed |
| 156 | * @hide |
| 157 | */ |
| 158 | public void onClientConnectionState(int status, int clientIf, |
| 159 | boolean connected, String address) { |
| 160 | if (DBG) Log.d(TAG, "onClientConnectionState() - status=" + status |
| 161 | + " clientIf=" + clientIf + " device=" + address); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 162 | if (!address.equals(mDevice.getAddress())) { |
| 163 | return; |
| 164 | } |
| 165 | int profileState = connected ? BluetoothProfile.STATE_CONNECTED : |
| 166 | BluetoothProfile.STATE_DISCONNECTED; |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 167 | try { |
Matthew Xie | 33ec984 | 2013-04-03 00:29:27 -0700 | [diff] [blame] | 168 | mCallback.onConnectionStateChange(BluetoothGatt.this, status, profileState); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 169 | } catch (Exception ex) { |
Mike Lockwood | 0998ff1 | 2013-05-13 14:04:12 -0700 | [diff] [blame] | 170 | Log.w(TAG, "Unhandled exception in callback", ex); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 171 | } |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 172 | |
| 173 | synchronized(mStateLock) { |
| 174 | if (connected) { |
| 175 | mConnState = CONN_STATE_CONNECTED; |
| 176 | } else { |
| 177 | mConnState = CONN_STATE_IDLE; |
| 178 | } |
| 179 | } |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Callback reporting an LE scan result. |
| 184 | * @hide |
| 185 | */ |
| 186 | public void onScanResult(String address, int rssi, byte[] advData) { |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 187 | // no op |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /** |
| 191 | * A new GATT service has been discovered. |
| 192 | * The service is added to the internal list and the search |
| 193 | * continues. |
| 194 | * @hide |
| 195 | */ |
| 196 | public void onGetService(String address, int srvcType, |
| 197 | int srvcInstId, ParcelUuid srvcUuid) { |
| 198 | if (DBG) Log.d(TAG, "onGetService() - Device=" + address + " UUID=" + srvcUuid); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 199 | if (!address.equals(mDevice.getAddress())) { |
| 200 | return; |
| 201 | } |
| 202 | mServices.add(new BluetoothGattService(mDevice, srvcUuid.getUuid(), |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 203 | srvcInstId, srvcType)); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * An included service has been found durig GATT discovery. |
| 208 | * The included service is added to the respective parent. |
| 209 | * @hide |
| 210 | */ |
| 211 | public void onGetIncludedService(String address, int srvcType, |
| 212 | int srvcInstId, ParcelUuid srvcUuid, |
| 213 | int inclSrvcType, int inclSrvcInstId, |
| 214 | ParcelUuid inclSrvcUuid) { |
| 215 | if (DBG) Log.d(TAG, "onGetIncludedService() - Device=" + address |
| 216 | + " UUID=" + srvcUuid + " Included=" + inclSrvcUuid); |
| 217 | |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 218 | if (!address.equals(mDevice.getAddress())) { |
| 219 | return; |
| 220 | } |
| 221 | BluetoothGattService service = getService(mDevice, |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 222 | srvcUuid.getUuid(), srvcInstId, srvcType); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 223 | BluetoothGattService includedService = getService(mDevice, |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 224 | inclSrvcUuid.getUuid(), inclSrvcInstId, inclSrvcType); |
| 225 | |
| 226 | if (service != null && includedService != null) { |
| 227 | service.addIncludedService(includedService); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * A new GATT characteristic has been discovered. |
| 233 | * Add the new characteristic to the relevant service and continue |
| 234 | * the remote device inspection. |
| 235 | * @hide |
| 236 | */ |
| 237 | public void onGetCharacteristic(String address, int srvcType, |
| 238 | int srvcInstId, ParcelUuid srvcUuid, |
| 239 | int charInstId, ParcelUuid charUuid, |
| 240 | int charProps) { |
| 241 | if (DBG) Log.d(TAG, "onGetCharacteristic() - Device=" + address + " UUID=" + |
| 242 | charUuid); |
| 243 | |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 244 | if (!address.equals(mDevice.getAddress())) { |
| 245 | return; |
| 246 | } |
| 247 | BluetoothGattService service = getService(mDevice, srvcUuid.getUuid(), |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 248 | srvcInstId, srvcType); |
| 249 | if (service != null) { |
| 250 | service.addCharacteristic(new BluetoothGattCharacteristic( |
| 251 | service, charUuid.getUuid(), charInstId, charProps, 0)); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * A new GATT descriptor has been discovered. |
| 257 | * Finally, add the descriptor to the related characteristic. |
| 258 | * This should conclude the remote device update. |
| 259 | * @hide |
| 260 | */ |
| 261 | public void onGetDescriptor(String address, int srvcType, |
| 262 | int srvcInstId, ParcelUuid srvcUuid, |
| 263 | int charInstId, ParcelUuid charUuid, |
Andre Eisenbach | 25b9cf9 | 2013-07-08 23:58:16 -0700 | [diff] [blame] | 264 | int descrInstId, ParcelUuid descUuid) { |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 265 | if (DBG) Log.d(TAG, "onGetDescriptor() - Device=" + address + " UUID=" + descUuid); |
| 266 | |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 267 | if (!address.equals(mDevice.getAddress())) { |
| 268 | return; |
| 269 | } |
| 270 | BluetoothGattService service = getService(mDevice, srvcUuid.getUuid(), |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 271 | srvcInstId, srvcType); |
| 272 | if (service == null) return; |
| 273 | |
| 274 | BluetoothGattCharacteristic characteristic = service.getCharacteristic( |
| 275 | charUuid.getUuid()); |
| 276 | if (characteristic == null) return; |
| 277 | |
| 278 | characteristic.addDescriptor(new BluetoothGattDescriptor( |
Andre Eisenbach | 25b9cf9 | 2013-07-08 23:58:16 -0700 | [diff] [blame] | 279 | characteristic, descUuid.getUuid(), descrInstId, 0)); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Remote search has been completed. |
| 284 | * The internal object structure should now reflect the state |
| 285 | * of the remote device database. Let the application know that |
| 286 | * we are done at this point. |
| 287 | * @hide |
| 288 | */ |
| 289 | public void onSearchComplete(String address, int status) { |
| 290 | if (DBG) Log.d(TAG, "onSearchComplete() = Device=" + address + " Status=" + status); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 291 | if (!address.equals(mDevice.getAddress())) { |
| 292 | return; |
| 293 | } |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 294 | try { |
Matthew Xie | 33ec984 | 2013-04-03 00:29:27 -0700 | [diff] [blame] | 295 | mCallback.onServicesDiscovered(BluetoothGatt.this, status); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 296 | } catch (Exception ex) { |
Mike Lockwood | 0998ff1 | 2013-05-13 14:04:12 -0700 | [diff] [blame] | 297 | Log.w(TAG, "Unhandled exception in callback", ex); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Remote characteristic has been read. |
| 303 | * Updates the internal value. |
| 304 | * @hide |
| 305 | */ |
| 306 | public void onCharacteristicRead(String address, int status, int srvcType, |
| 307 | int srvcInstId, ParcelUuid srvcUuid, |
| 308 | int charInstId, ParcelUuid charUuid, byte[] value) { |
| 309 | if (DBG) Log.d(TAG, "onCharacteristicRead() - Device=" + address |
| 310 | + " UUID=" + charUuid + " Status=" + status); |
| 311 | |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 312 | if (!address.equals(mDevice.getAddress())) { |
| 313 | return; |
| 314 | } |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 315 | if ((status == GATT_INSUFFICIENT_AUTHENTICATION |
| 316 | || status == GATT_INSUFFICIENT_ENCRYPTION) |
| 317 | && mAuthRetry == false) { |
| 318 | try { |
| 319 | mAuthRetry = true; |
| 320 | mService.readCharacteristic(mClientIf, address, |
| 321 | srvcType, srvcInstId, srvcUuid, |
| 322 | charInstId, charUuid, AUTHENTICATION_MITM); |
| 323 | return; |
| 324 | } catch (RemoteException e) { |
| 325 | Log.e(TAG,"",e); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | mAuthRetry = false; |
| 330 | |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 331 | BluetoothGattService service = getService(mDevice, srvcUuid.getUuid(), |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 332 | srvcInstId, srvcType); |
| 333 | if (service == null) return; |
| 334 | |
| 335 | BluetoothGattCharacteristic characteristic = service.getCharacteristic( |
| 336 | charUuid.getUuid(), charInstId); |
| 337 | if (characteristic == null) return; |
| 338 | |
| 339 | if (status == 0) characteristic.setValue(value); |
| 340 | |
| 341 | try { |
Matthew Xie | 33ec984 | 2013-04-03 00:29:27 -0700 | [diff] [blame] | 342 | mCallback.onCharacteristicRead(BluetoothGatt.this, characteristic, status); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 343 | } catch (Exception ex) { |
Mike Lockwood | 0998ff1 | 2013-05-13 14:04:12 -0700 | [diff] [blame] | 344 | Log.w(TAG, "Unhandled exception in callback", ex); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * Characteristic has been written to the remote device. |
| 350 | * Let the app know how we did... |
| 351 | * @hide |
| 352 | */ |
| 353 | public void onCharacteristicWrite(String address, int status, int srvcType, |
| 354 | int srvcInstId, ParcelUuid srvcUuid, |
| 355 | int charInstId, ParcelUuid charUuid) { |
| 356 | if (DBG) Log.d(TAG, "onCharacteristicWrite() - Device=" + address |
| 357 | + " UUID=" + charUuid + " Status=" + status); |
| 358 | |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 359 | if (!address.equals(mDevice.getAddress())) { |
| 360 | return; |
| 361 | } |
| 362 | BluetoothGattService service = getService(mDevice, srvcUuid.getUuid(), |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 363 | srvcInstId, srvcType); |
| 364 | if (service == null) return; |
| 365 | |
| 366 | BluetoothGattCharacteristic characteristic = service.getCharacteristic( |
| 367 | charUuid.getUuid(), charInstId); |
| 368 | if (characteristic == null) return; |
| 369 | |
| 370 | if ((status == GATT_INSUFFICIENT_AUTHENTICATION |
| 371 | || status == GATT_INSUFFICIENT_ENCRYPTION) |
| 372 | && mAuthRetry == false) { |
| 373 | try { |
| 374 | mAuthRetry = true; |
| 375 | mService.writeCharacteristic(mClientIf, address, |
| 376 | srvcType, srvcInstId, srvcUuid, charInstId, charUuid, |
| 377 | characteristic.getWriteType(), AUTHENTICATION_MITM, |
| 378 | characteristic.getValue()); |
| 379 | return; |
| 380 | } catch (RemoteException e) { |
| 381 | Log.e(TAG,"",e); |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | mAuthRetry = false; |
| 386 | |
| 387 | try { |
Matthew Xie | 33ec984 | 2013-04-03 00:29:27 -0700 | [diff] [blame] | 388 | mCallback.onCharacteristicWrite(BluetoothGatt.this, characteristic, status); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 389 | } catch (Exception ex) { |
Mike Lockwood | 0998ff1 | 2013-05-13 14:04:12 -0700 | [diff] [blame] | 390 | Log.w(TAG, "Unhandled exception in callback", ex); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * Remote characteristic has been updated. |
| 396 | * Updates the internal value. |
| 397 | * @hide |
| 398 | */ |
| 399 | public void onNotify(String address, int srvcType, |
| 400 | int srvcInstId, ParcelUuid srvcUuid, |
| 401 | int charInstId, ParcelUuid charUuid, |
| 402 | byte[] value) { |
| 403 | if (DBG) Log.d(TAG, "onNotify() - Device=" + address + " UUID=" + charUuid); |
| 404 | |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 405 | if (!address.equals(mDevice.getAddress())) { |
| 406 | return; |
| 407 | } |
| 408 | BluetoothGattService service = getService(mDevice, srvcUuid.getUuid(), |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 409 | srvcInstId, srvcType); |
| 410 | if (service == null) return; |
| 411 | |
| 412 | BluetoothGattCharacteristic characteristic = service.getCharacteristic( |
| 413 | charUuid.getUuid(), charInstId); |
| 414 | if (characteristic == null) return; |
| 415 | |
| 416 | characteristic.setValue(value); |
| 417 | |
| 418 | try { |
Matthew Xie | 33ec984 | 2013-04-03 00:29:27 -0700 | [diff] [blame] | 419 | mCallback.onCharacteristicChanged(BluetoothGatt.this, characteristic); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 420 | } catch (Exception ex) { |
Mike Lockwood | 0998ff1 | 2013-05-13 14:04:12 -0700 | [diff] [blame] | 421 | Log.w(TAG, "Unhandled exception in callback", ex); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 422 | } |
| 423 | } |
| 424 | |
| 425 | /** |
| 426 | * Descriptor has been read. |
| 427 | * @hide |
| 428 | */ |
| 429 | public void onDescriptorRead(String address, int status, int srvcType, |
| 430 | int srvcInstId, ParcelUuid srvcUuid, |
| 431 | int charInstId, ParcelUuid charUuid, |
Andre Eisenbach | 25b9cf9 | 2013-07-08 23:58:16 -0700 | [diff] [blame] | 432 | int descrInstId, ParcelUuid descrUuid, |
| 433 | byte[] value) { |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 434 | if (DBG) Log.d(TAG, "onDescriptorRead() - Device=" + address + " UUID=" + charUuid); |
| 435 | |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 436 | if (!address.equals(mDevice.getAddress())) { |
| 437 | return; |
| 438 | } |
| 439 | BluetoothGattService service = getService(mDevice, srvcUuid.getUuid(), |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 440 | srvcInstId, srvcType); |
| 441 | if (service == null) return; |
| 442 | |
| 443 | BluetoothGattCharacteristic characteristic = service.getCharacteristic( |
| 444 | charUuid.getUuid(), charInstId); |
| 445 | if (characteristic == null) return; |
| 446 | |
| 447 | BluetoothGattDescriptor descriptor = characteristic.getDescriptor( |
Andre Eisenbach | 25b9cf9 | 2013-07-08 23:58:16 -0700 | [diff] [blame] | 448 | descrUuid.getUuid(), descrInstId); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 449 | if (descriptor == null) return; |
| 450 | |
| 451 | if (status == 0) descriptor.setValue(value); |
| 452 | |
| 453 | if ((status == GATT_INSUFFICIENT_AUTHENTICATION |
| 454 | || status == GATT_INSUFFICIENT_ENCRYPTION) |
| 455 | && mAuthRetry == false) { |
| 456 | try { |
| 457 | mAuthRetry = true; |
| 458 | mService.readDescriptor(mClientIf, address, |
| 459 | srvcType, srvcInstId, srvcUuid, charInstId, charUuid, |
Andre Eisenbach | 25b9cf9 | 2013-07-08 23:58:16 -0700 | [diff] [blame] | 460 | descrInstId, descrUuid, AUTHENTICATION_MITM); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 461 | } catch (RemoteException e) { |
| 462 | Log.e(TAG,"",e); |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | mAuthRetry = true; |
| 467 | |
| 468 | try { |
Matthew Xie | 33ec984 | 2013-04-03 00:29:27 -0700 | [diff] [blame] | 469 | mCallback.onDescriptorRead(BluetoothGatt.this, descriptor, status); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 470 | } catch (Exception ex) { |
Mike Lockwood | 0998ff1 | 2013-05-13 14:04:12 -0700 | [diff] [blame] | 471 | Log.w(TAG, "Unhandled exception in callback", ex); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 472 | } |
| 473 | } |
| 474 | |
| 475 | /** |
| 476 | * Descriptor write operation complete. |
| 477 | * @hide |
| 478 | */ |
| 479 | public void onDescriptorWrite(String address, int status, int srvcType, |
| 480 | int srvcInstId, ParcelUuid srvcUuid, |
| 481 | int charInstId, ParcelUuid charUuid, |
Andre Eisenbach | 25b9cf9 | 2013-07-08 23:58:16 -0700 | [diff] [blame] | 482 | int descrInstId, ParcelUuid descrUuid) { |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 483 | if (DBG) Log.d(TAG, "onDescriptorWrite() - Device=" + address + " UUID=" + charUuid); |
| 484 | |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 485 | if (!address.equals(mDevice.getAddress())) { |
| 486 | return; |
| 487 | } |
| 488 | BluetoothGattService service = getService(mDevice, srvcUuid.getUuid(), |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 489 | srvcInstId, srvcType); |
| 490 | if (service == null) return; |
| 491 | |
| 492 | BluetoothGattCharacteristic characteristic = service.getCharacteristic( |
| 493 | charUuid.getUuid(), charInstId); |
| 494 | if (characteristic == null) return; |
| 495 | |
| 496 | BluetoothGattDescriptor descriptor = characteristic.getDescriptor( |
Andre Eisenbach | 25b9cf9 | 2013-07-08 23:58:16 -0700 | [diff] [blame] | 497 | descrUuid.getUuid(), descrInstId); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 498 | if (descriptor == null) return; |
| 499 | |
| 500 | if ((status == GATT_INSUFFICIENT_AUTHENTICATION |
| 501 | || status == GATT_INSUFFICIENT_ENCRYPTION) |
| 502 | && mAuthRetry == false) { |
| 503 | try { |
| 504 | mAuthRetry = true; |
| 505 | mService.writeDescriptor(mClientIf, address, |
| 506 | srvcType, srvcInstId, srvcUuid, charInstId, charUuid, |
Andre Eisenbach | 25b9cf9 | 2013-07-08 23:58:16 -0700 | [diff] [blame] | 507 | descrInstId, descrUuid, characteristic.getWriteType(), |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 508 | AUTHENTICATION_MITM, descriptor.getValue()); |
| 509 | } catch (RemoteException e) { |
| 510 | Log.e(TAG,"",e); |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | mAuthRetry = false; |
| 515 | |
| 516 | try { |
Matthew Xie | 33ec984 | 2013-04-03 00:29:27 -0700 | [diff] [blame] | 517 | mCallback.onDescriptorWrite(BluetoothGatt.this, descriptor, status); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 518 | } catch (Exception ex) { |
Mike Lockwood | 0998ff1 | 2013-05-13 14:04:12 -0700 | [diff] [blame] | 519 | Log.w(TAG, "Unhandled exception in callback", ex); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 520 | } |
| 521 | } |
| 522 | |
| 523 | /** |
| 524 | * Prepared write transaction completed (or aborted) |
| 525 | * @hide |
| 526 | */ |
| 527 | public void onExecuteWrite(String address, int status) { |
| 528 | if (DBG) Log.d(TAG, "onExecuteWrite() - Device=" + address |
| 529 | + " status=" + status); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 530 | if (!address.equals(mDevice.getAddress())) { |
| 531 | return; |
| 532 | } |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 533 | try { |
Matthew Xie | 33ec984 | 2013-04-03 00:29:27 -0700 | [diff] [blame] | 534 | mCallback.onReliableWriteCompleted(BluetoothGatt.this, status); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 535 | } catch (Exception ex) { |
Mike Lockwood | 0998ff1 | 2013-05-13 14:04:12 -0700 | [diff] [blame] | 536 | Log.w(TAG, "Unhandled exception in callback", ex); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 537 | } |
| 538 | } |
| 539 | |
| 540 | /** |
| 541 | * Remote device RSSI has been read |
| 542 | * @hide |
| 543 | */ |
| 544 | public void onReadRemoteRssi(String address, int rssi, int status) { |
| 545 | if (DBG) Log.d(TAG, "onReadRemoteRssi() - Device=" + address + |
| 546 | " rssi=" + rssi + " status=" + status); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 547 | if (!address.equals(mDevice.getAddress())) { |
| 548 | return; |
| 549 | } |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 550 | try { |
Matthew Xie | 33ec984 | 2013-04-03 00:29:27 -0700 | [diff] [blame] | 551 | mCallback.onReadRemoteRssi(BluetoothGatt.this, rssi, status); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 552 | } catch (Exception ex) { |
Mike Lockwood | 0998ff1 | 2013-05-13 14:04:12 -0700 | [diff] [blame] | 553 | Log.w(TAG, "Unhandled exception in callback", ex); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 554 | } |
| 555 | } |
| 556 | }; |
| 557 | |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 558 | /*package*/ BluetoothGatt(Context context, IBluetoothGatt iGatt, BluetoothDevice device) { |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 559 | mContext = context; |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 560 | mService = iGatt; |
| 561 | mDevice = device; |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 562 | mServices = new ArrayList<BluetoothGattService>(); |
| 563 | |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 564 | mConnState = CONN_STATE_IDLE; |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | /** |
Matthew Xie | 33ec984 | 2013-04-03 00:29:27 -0700 | [diff] [blame] | 568 | * Close this Bluetooth GATT client. |
Matthew Xie | b30f91e | 2013-05-29 10:19:06 -0700 | [diff] [blame] | 569 | * |
| 570 | * Application should call this method as early as possible after it is done with |
| 571 | * this GATT client. |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 572 | */ |
Matthew Xie | 33ec984 | 2013-04-03 00:29:27 -0700 | [diff] [blame] | 573 | public void close() { |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 574 | if (DBG) Log.d(TAG, "close()"); |
| 575 | |
| 576 | unregisterApp(); |
Matthew Xie | 33ec984 | 2013-04-03 00:29:27 -0700 | [diff] [blame] | 577 | mConnState = CONN_STATE_CLOSED; |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | /** |
| 581 | * Returns a service by UUID, instance and type. |
| 582 | * @hide |
| 583 | */ |
| 584 | /*package*/ BluetoothGattService getService(BluetoothDevice device, UUID uuid, |
| 585 | int instanceId, int type) { |
| 586 | for(BluetoothGattService svc : mServices) { |
| 587 | if (svc.getDevice().equals(device) && |
| 588 | svc.getType() == type && |
| 589 | svc.getInstanceId() == instanceId && |
| 590 | svc.getUuid().equals(uuid)) { |
| 591 | return svc; |
| 592 | } |
| 593 | } |
| 594 | return null; |
| 595 | } |
| 596 | |
| 597 | |
| 598 | /** |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 599 | * Register an application callback to start using GATT. |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 600 | * |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 601 | * <p>This is an asynchronous call. The callback {@link BluetoothGattCallback#onAppRegistered} |
| 602 | * is used to notify success or failure if the function returns true. |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 603 | * |
| 604 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 605 | * |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 606 | * @param callback GATT callback handler that will receive asynchronous callbacks. |
| 607 | * @return If true, the callback will be called to notify success or failure, |
| 608 | * false on immediate error |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 609 | */ |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 610 | private boolean registerApp(BluetoothGattCallback callback) { |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 611 | if (DBG) Log.d(TAG, "registerApp()"); |
| 612 | if (mService == null) return false; |
| 613 | |
| 614 | mCallback = callback; |
| 615 | UUID uuid = UUID.randomUUID(); |
| 616 | if (DBG) Log.d(TAG, "registerApp() - UUID=" + uuid); |
| 617 | |
| 618 | try { |
| 619 | mService.registerClient(new ParcelUuid(uuid), mBluetoothGattCallback); |
| 620 | } catch (RemoteException e) { |
| 621 | Log.e(TAG,"",e); |
| 622 | return false; |
| 623 | } |
| 624 | |
| 625 | return true; |
| 626 | } |
| 627 | |
| 628 | /** |
| 629 | * Unregister the current application and callbacks. |
| 630 | */ |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 631 | private void unregisterApp() { |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 632 | if (DBG) Log.d(TAG, "unregisterApp() - mClientIf=" + mClientIf); |
| 633 | if (mService == null || mClientIf == 0) return; |
| 634 | |
| 635 | try { |
| 636 | mCallback = null; |
| 637 | mService.unregisterClient(mClientIf); |
| 638 | mClientIf = 0; |
| 639 | } catch (RemoteException e) { |
| 640 | Log.e(TAG,"",e); |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | /** |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 645 | * Initiate a connection to a Bluetooth GATT capable device. |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 646 | * |
| 647 | * <p>The connection may not be established right away, but will be |
| 648 | * completed when the remote device is available. A |
| 649 | * {@link BluetoothGattCallback#onConnectionStateChange} callback will be |
| 650 | * invoked when the connection state changes as a result of this function. |
| 651 | * |
| 652 | * <p>The autoConnect paramter determines whether to actively connect to |
| 653 | * the remote device, or rather passively scan and finalize the connection |
| 654 | * when the remote device is in range/available. Generally, the first ever |
| 655 | * connection to a device should be direct (autoConnect set to false) and |
| 656 | * subsequent connections to known devices should be invoked with the |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 657 | * autoConnect parameter set to true. |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 658 | * |
| 659 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 660 | * |
| 661 | * @param device Remote device to connect to |
| 662 | * @param autoConnect Whether to directly connect to the remote device (false) |
| 663 | * or to automatically connect as soon as the remote |
| 664 | * device becomes available (true). |
| 665 | * @return true, if the connection attempt was initiated successfully |
| 666 | */ |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 667 | /*package*/ boolean connect(Boolean autoConnect, BluetoothGattCallback callback) { |
| 668 | if (DBG) Log.d(TAG, "connect() - device: " + mDevice.getAddress() + ", auto: " + autoConnect); |
| 669 | synchronized(mStateLock) { |
| 670 | if (mConnState != CONN_STATE_IDLE) { |
| 671 | throw new IllegalStateException("Not idle"); |
| 672 | } |
| 673 | mConnState = CONN_STATE_CONNECTING; |
| 674 | } |
| 675 | if (!registerApp(callback)) { |
| 676 | synchronized(mStateLock) { |
| 677 | mConnState = CONN_STATE_IDLE; |
| 678 | } |
| 679 | Log.e(TAG, "Failed to register callback"); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 680 | return false; |
| 681 | } |
| 682 | |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 683 | // the connection will continue after successful callback registration |
| 684 | mAutoConnect = autoConnect; |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 685 | return true; |
| 686 | } |
| 687 | |
| 688 | /** |
| 689 | * Disconnects an established connection, or cancels a connection attempt |
| 690 | * currently in progress. |
| 691 | * |
| 692 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 693 | */ |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 694 | public void disconnect() { |
| 695 | if (DBG) Log.d(TAG, "cancelOpen() - device: " + mDevice.getAddress()); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 696 | if (mService == null || mClientIf == 0) return; |
| 697 | |
| 698 | try { |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 699 | mService.clientDisconnect(mClientIf, mDevice.getAddress()); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 700 | } catch (RemoteException e) { |
| 701 | Log.e(TAG,"",e); |
| 702 | } |
Matthew Xie | 33ec984 | 2013-04-03 00:29:27 -0700 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | /** |
| 706 | * Connect back to remote device. |
| 707 | * |
| 708 | * <p>This method is used to re-connect to a remote device after the |
| 709 | * connection has been dropped. If the device is not in range, the |
| 710 | * re-connection will be triggered once the device is back in range. |
| 711 | * |
| 712 | * @return true, if the connection attempt was initiated successfully |
| 713 | */ |
| 714 | public boolean connect() { |
| 715 | try { |
| 716 | mService.clientConnect(mClientIf, mDevice.getAddress(), |
| 717 | false); // autoConnect is inverse of "isDirect" |
| 718 | return true; |
| 719 | } catch (RemoteException e) { |
| 720 | Log.e(TAG,"",e); |
| 721 | return false; |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | /** |
| 726 | * Return the remote bluetooth device this GATT client targets to |
| 727 | * |
| 728 | * @return remote bluetooth device |
| 729 | */ |
| 730 | public BluetoothDevice getDevice() { |
| 731 | return mDevice; |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | /** |
| 735 | * Discovers services offered by a remote device as well as their |
| 736 | * characteristics and descriptors. |
| 737 | * |
| 738 | * <p>This is an asynchronous operation. Once service discovery is completed, |
| 739 | * the {@link BluetoothGattCallback#onServicesDiscovered} callback is |
| 740 | * triggered. If the discovery was successful, the remote services can be |
| 741 | * retrieved using the {@link #getServices} function. |
| 742 | * |
| 743 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 744 | * |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 745 | * @return true, if the remote service discovery has been started |
| 746 | */ |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 747 | public boolean discoverServices() { |
| 748 | if (DBG) Log.d(TAG, "discoverServices() - device: " + mDevice.getAddress()); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 749 | if (mService == null || mClientIf == 0) return false; |
| 750 | |
| 751 | mServices.clear(); |
| 752 | |
| 753 | try { |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 754 | mService.discoverServices(mClientIf, mDevice.getAddress()); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 755 | } catch (RemoteException e) { |
| 756 | Log.e(TAG,"",e); |
| 757 | return false; |
| 758 | } |
| 759 | |
| 760 | return true; |
| 761 | } |
| 762 | |
| 763 | /** |
| 764 | * Returns a list of GATT services offered by the remote device. |
| 765 | * |
| 766 | * <p>This function requires that service discovery has been completed |
| 767 | * for the given device. |
| 768 | * |
| 769 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 770 | * |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 771 | * @return List of services on the remote device. Returns an empty list |
| 772 | * if service discovery has not yet been performed. |
| 773 | */ |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 774 | public List<BluetoothGattService> getServices() { |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 775 | List<BluetoothGattService> result = |
| 776 | new ArrayList<BluetoothGattService>(); |
| 777 | |
| 778 | for (BluetoothGattService service : mServices) { |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 779 | if (service.getDevice().equals(mDevice)) { |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 780 | result.add(service); |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | return result; |
| 785 | } |
| 786 | |
| 787 | /** |
| 788 | * Returns a {@link BluetoothGattService}, if the requested UUID is |
| 789 | * supported by the remote device. |
| 790 | * |
| 791 | * <p>This function requires that service discovery has been completed |
| 792 | * for the given device. |
| 793 | * |
| 794 | * <p>If multiple instances of the same service (as identified by UUID) |
| 795 | * exist, the first instance of the service is returned. |
| 796 | * |
| 797 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 798 | * |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 799 | * @param uuid UUID of the requested service |
| 800 | * @return BluetoothGattService if supported, or null if the requested |
| 801 | * service is not offered by the remote device. |
| 802 | */ |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 803 | public BluetoothGattService getService(UUID uuid) { |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 804 | for (BluetoothGattService service : mServices) { |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 805 | if (service.getDevice().equals(mDevice) && |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 806 | service.getUuid().equals(uuid)) { |
| 807 | return service; |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | return null; |
| 812 | } |
| 813 | |
| 814 | /** |
| 815 | * Reads the requested characteristic from the associated remote device. |
| 816 | * |
| 817 | * <p>This is an asynchronous operation. The result of the read operation |
| 818 | * is reported by the {@link BluetoothGattCallback#onCharacteristicRead} |
| 819 | * callback. |
| 820 | * |
| 821 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 822 | * |
| 823 | * @param characteristic Characteristic to read from the remote device |
| 824 | * @return true, if the read operation was initiated successfully |
| 825 | */ |
| 826 | public boolean readCharacteristic(BluetoothGattCharacteristic characteristic) { |
| 827 | if ((characteristic.getProperties() & |
| 828 | BluetoothGattCharacteristic.PROPERTY_READ) == 0) return false; |
| 829 | |
| 830 | if (DBG) Log.d(TAG, "readCharacteristic() - uuid: " + characteristic.getUuid()); |
| 831 | if (mService == null || mClientIf == 0) return false; |
| 832 | |
| 833 | BluetoothGattService service = characteristic.getService(); |
| 834 | if (service == null) return false; |
| 835 | |
| 836 | BluetoothDevice device = service.getDevice(); |
| 837 | if (device == null) return false; |
| 838 | |
| 839 | try { |
| 840 | mService.readCharacteristic(mClientIf, device.getAddress(), |
| 841 | service.getType(), service.getInstanceId(), |
| 842 | new ParcelUuid(service.getUuid()), characteristic.getInstanceId(), |
| 843 | new ParcelUuid(characteristic.getUuid()), AUTHENTICATION_NONE); |
| 844 | } catch (RemoteException e) { |
| 845 | Log.e(TAG,"",e); |
| 846 | return false; |
| 847 | } |
| 848 | |
| 849 | return true; |
| 850 | } |
| 851 | |
| 852 | /** |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 853 | * Writes a given characteristic and its values to the associated remote device. |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 854 | * |
| 855 | * <p>Once the write operation has been completed, the |
| 856 | * {@link BluetoothGattCallback#onCharacteristicWrite} callback is invoked, |
| 857 | * reporting the result of the operation. |
| 858 | * |
| 859 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 860 | * |
| 861 | * @param characteristic Characteristic to write on the remote device |
| 862 | * @return true, if the write operation was initiated successfully |
| 863 | */ |
| 864 | public boolean writeCharacteristic(BluetoothGattCharacteristic characteristic) { |
| 865 | if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE) == 0 |
| 866 | && (characteristic.getProperties() & |
| 867 | BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) == 0) return false; |
| 868 | |
| 869 | if (DBG) Log.d(TAG, "writeCharacteristic() - uuid: " + characteristic.getUuid()); |
| 870 | if (mService == null || mClientIf == 0) return false; |
| 871 | |
| 872 | BluetoothGattService service = characteristic.getService(); |
| 873 | if (service == null) return false; |
| 874 | |
| 875 | BluetoothDevice device = service.getDevice(); |
| 876 | if (device == null) return false; |
| 877 | |
| 878 | try { |
| 879 | mService.writeCharacteristic(mClientIf, device.getAddress(), |
| 880 | service.getType(), service.getInstanceId(), |
| 881 | new ParcelUuid(service.getUuid()), characteristic.getInstanceId(), |
| 882 | new ParcelUuid(characteristic.getUuid()), |
| 883 | characteristic.getWriteType(), AUTHENTICATION_NONE, |
| 884 | characteristic.getValue()); |
| 885 | } catch (RemoteException e) { |
| 886 | Log.e(TAG,"",e); |
| 887 | return false; |
| 888 | } |
| 889 | |
| 890 | return true; |
| 891 | } |
| 892 | |
| 893 | /** |
| 894 | * Reads the value for a given descriptor from the associated remote device. |
| 895 | * |
| 896 | * <p>Once the read operation has been completed, the |
| 897 | * {@link BluetoothGattCallback#onDescriptorRead} callback is |
| 898 | * triggered, signaling the result of the operation. |
| 899 | * |
| 900 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 901 | * |
| 902 | * @param descriptor Descriptor value to read from the remote device |
| 903 | * @return true, if the read operation was initiated successfully |
| 904 | */ |
| 905 | public boolean readDescriptor(BluetoothGattDescriptor descriptor) { |
| 906 | if (DBG) Log.d(TAG, "readDescriptor() - uuid: " + descriptor.getUuid()); |
| 907 | if (mService == null || mClientIf == 0) return false; |
| 908 | |
| 909 | BluetoothGattCharacteristic characteristic = descriptor.getCharacteristic(); |
| 910 | if (characteristic == null) return false; |
| 911 | |
| 912 | BluetoothGattService service = characteristic.getService(); |
| 913 | if (service == null) return false; |
| 914 | |
| 915 | BluetoothDevice device = service.getDevice(); |
| 916 | if (device == null) return false; |
| 917 | |
| 918 | try { |
Andre Eisenbach | 25b9cf9 | 2013-07-08 23:58:16 -0700 | [diff] [blame] | 919 | mService.readDescriptor(mClientIf, device.getAddress(), service.getType(), |
| 920 | service.getInstanceId(), new ParcelUuid(service.getUuid()), |
| 921 | characteristic.getInstanceId(), new ParcelUuid(characteristic.getUuid()), |
| 922 | descriptor.getInstanceId(), new ParcelUuid(descriptor.getUuid()), |
| 923 | AUTHENTICATION_NONE); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 924 | } catch (RemoteException e) { |
| 925 | Log.e(TAG,"",e); |
| 926 | return false; |
| 927 | } |
| 928 | |
| 929 | return true; |
| 930 | } |
| 931 | |
| 932 | /** |
| 933 | * Write the value of a given descriptor to the associated remote device. |
| 934 | * |
| 935 | * <p>A {@link BluetoothGattCallback#onDescriptorWrite} callback is |
| 936 | * triggered to report the result of the write operation. |
| 937 | * |
| 938 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 939 | * |
| 940 | * @param descriptor Descriptor to write to the associated remote device |
| 941 | * @return true, if the write operation was initiated successfully |
| 942 | */ |
| 943 | public boolean writeDescriptor(BluetoothGattDescriptor descriptor) { |
| 944 | if (DBG) Log.d(TAG, "writeDescriptor() - uuid: " + descriptor.getUuid()); |
| 945 | if (mService == null || mClientIf == 0) return false; |
| 946 | |
| 947 | BluetoothGattCharacteristic characteristic = descriptor.getCharacteristic(); |
| 948 | if (characteristic == null) return false; |
| 949 | |
| 950 | BluetoothGattService service = characteristic.getService(); |
| 951 | if (service == null) return false; |
| 952 | |
| 953 | BluetoothDevice device = service.getDevice(); |
| 954 | if (device == null) return false; |
| 955 | |
| 956 | try { |
Andre Eisenbach | 25b9cf9 | 2013-07-08 23:58:16 -0700 | [diff] [blame] | 957 | mService.writeDescriptor(mClientIf, device.getAddress(), service.getType(), |
| 958 | service.getInstanceId(), new ParcelUuid(service.getUuid()), |
| 959 | characteristic.getInstanceId(), new ParcelUuid(characteristic.getUuid()), |
| 960 | descriptor.getInstanceId(), new ParcelUuid(descriptor.getUuid()), |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 961 | characteristic.getWriteType(), AUTHENTICATION_NONE, |
| 962 | descriptor.getValue()); |
| 963 | } catch (RemoteException e) { |
| 964 | Log.e(TAG,"",e); |
| 965 | return false; |
| 966 | } |
| 967 | |
| 968 | return true; |
| 969 | } |
| 970 | |
| 971 | /** |
| 972 | * Initiates a reliable write transaction for a given remote device. |
| 973 | * |
| 974 | * <p>Once a reliable write transaction has been initiated, all calls |
| 975 | * to {@link #writeCharacteristic} are sent to the remote device for |
| 976 | * verification and queued up for atomic execution. The application will |
| 977 | * receive an {@link BluetoothGattCallback#onCharacteristicWrite} callback |
| 978 | * in response to every {@link #writeCharacteristic} call and is responsible |
| 979 | * for verifying if the value has been transmitted accurately. |
| 980 | * |
| 981 | * <p>After all characteristics have been queued up and verified, |
| 982 | * {@link #executeReliableWrite} will execute all writes. If a characteristic |
| 983 | * was not written correctly, calling {@link #abortReliableWrite} will |
| 984 | * cancel the current transaction without commiting any values on the |
| 985 | * remote device. |
| 986 | * |
| 987 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 988 | * |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 989 | * @return true, if the reliable write transaction has been initiated |
| 990 | */ |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 991 | public boolean beginReliableWrite() { |
| 992 | if (DBG) Log.d(TAG, "beginReliableWrite() - device: " + mDevice.getAddress()); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 993 | if (mService == null || mClientIf == 0) return false; |
| 994 | |
| 995 | try { |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 996 | mService.beginReliableWrite(mClientIf, mDevice.getAddress()); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 997 | } catch (RemoteException e) { |
| 998 | Log.e(TAG,"",e); |
| 999 | return false; |
| 1000 | } |
| 1001 | |
| 1002 | return true; |
| 1003 | } |
| 1004 | |
| 1005 | /** |
| 1006 | * Executes a reliable write transaction for a given remote device. |
| 1007 | * |
| 1008 | * <p>This function will commit all queued up characteristic write |
| 1009 | * operations for a given remote device. |
| 1010 | * |
| 1011 | * <p>A {@link BluetoothGattCallback#onReliableWriteCompleted} callback is |
| 1012 | * invoked to indicate whether the transaction has been executed correctly. |
| 1013 | * |
| 1014 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 1015 | * |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 1016 | * @return true, if the request to execute the transaction has been sent |
| 1017 | */ |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1018 | public boolean executeReliableWrite() { |
| 1019 | if (DBG) Log.d(TAG, "executeReliableWrite() - device: " + mDevice.getAddress()); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 1020 | if (mService == null || mClientIf == 0) return false; |
| 1021 | |
| 1022 | try { |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1023 | mService.endReliableWrite(mClientIf, mDevice.getAddress(), true); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 1024 | } catch (RemoteException e) { |
| 1025 | Log.e(TAG,"",e); |
| 1026 | return false; |
| 1027 | } |
| 1028 | |
| 1029 | return true; |
| 1030 | } |
| 1031 | |
| 1032 | /** |
| 1033 | * Cancels a reliable write transaction for a given device. |
| 1034 | * |
| 1035 | * <p>Calling this function will discard all queued characteristic write |
| 1036 | * operations for a given remote device. |
| 1037 | * |
| 1038 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 1039 | */ |
John Du | 48f8b5d | 2013-08-19 12:20:37 -0700 | [diff] [blame] | 1040 | public void abortReliableWrite() { |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1041 | if (DBG) Log.d(TAG, "abortReliableWrite() - device: " + mDevice.getAddress()); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 1042 | if (mService == null || mClientIf == 0) return; |
| 1043 | |
| 1044 | try { |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1045 | mService.endReliableWrite(mClientIf, mDevice.getAddress(), false); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 1046 | } catch (RemoteException e) { |
| 1047 | Log.e(TAG,"",e); |
| 1048 | } |
| 1049 | } |
| 1050 | |
| 1051 | /** |
John Du | b7b7d7a | 2013-08-20 14:03:28 -0700 | [diff] [blame] | 1052 | * @deprecated Use {@link #abortReliableWrite()} |
John Du | 48f8b5d | 2013-08-19 12:20:37 -0700 | [diff] [blame] | 1053 | */ |
| 1054 | public void abortReliableWrite(BluetoothDevice mDevice) { |
| 1055 | abortReliableWrite(); |
| 1056 | } |
| 1057 | |
| 1058 | /** |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 1059 | * Enable or disable notifications/indications for a given characteristic. |
| 1060 | * |
| 1061 | * <p>Once notifications are enabled for a characteristic, a |
| 1062 | * {@link BluetoothGattCallback#onCharacteristicChanged} callback will be |
| 1063 | * triggered if the remote device indicates that the given characteristic |
| 1064 | * has changed. |
| 1065 | * |
| 1066 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 1067 | * |
| 1068 | * @param characteristic The characteristic for which to enable notifications |
| 1069 | * @param enable Set to true to enable notifications/indications |
| 1070 | * @return true, if the requested notification status was set successfully |
| 1071 | */ |
| 1072 | public boolean setCharacteristicNotification(BluetoothGattCharacteristic characteristic, |
| 1073 | boolean enable) { |
| 1074 | if (DBG) Log.d(TAG, "setCharacteristicNotification() - uuid: " + characteristic.getUuid() |
| 1075 | + " enable: " + enable); |
| 1076 | if (mService == null || mClientIf == 0) return false; |
| 1077 | |
| 1078 | BluetoothGattService service = characteristic.getService(); |
| 1079 | if (service == null) return false; |
| 1080 | |
| 1081 | BluetoothDevice device = service.getDevice(); |
| 1082 | if (device == null) return false; |
| 1083 | |
| 1084 | try { |
| 1085 | mService.registerForNotification(mClientIf, device.getAddress(), |
| 1086 | service.getType(), service.getInstanceId(), |
| 1087 | new ParcelUuid(service.getUuid()), characteristic.getInstanceId(), |
| 1088 | new ParcelUuid(characteristic.getUuid()), |
| 1089 | enable); |
| 1090 | } catch (RemoteException e) { |
| 1091 | Log.e(TAG,"",e); |
| 1092 | return false; |
| 1093 | } |
| 1094 | |
| 1095 | return true; |
| 1096 | } |
| 1097 | |
| 1098 | /** |
| 1099 | * Clears the internal cache and forces a refresh of the services from the |
| 1100 | * remote device. |
| 1101 | * @hide |
| 1102 | */ |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1103 | public boolean refresh() { |
| 1104 | if (DBG) Log.d(TAG, "refresh() - device: " + mDevice.getAddress()); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 1105 | if (mService == null || mClientIf == 0) return false; |
| 1106 | |
| 1107 | try { |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1108 | mService.refreshDevice(mClientIf, mDevice.getAddress()); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 1109 | } catch (RemoteException e) { |
| 1110 | Log.e(TAG,"",e); |
| 1111 | return false; |
| 1112 | } |
| 1113 | |
| 1114 | return true; |
| 1115 | } |
| 1116 | |
| 1117 | /** |
| 1118 | * Read the RSSI for a connected remote device. |
| 1119 | * |
| 1120 | * <p>The {@link BluetoothGattCallback#onReadRemoteRssi} callback will be |
| 1121 | * invoked when the RSSI value has been read. |
| 1122 | * |
| 1123 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 1124 | * |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 1125 | * @return true, if the RSSI value has been requested successfully |
| 1126 | */ |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1127 | public boolean readRemoteRssi() { |
| 1128 | if (DBG) Log.d(TAG, "readRssi() - device: " + mDevice.getAddress()); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 1129 | if (mService == null || mClientIf == 0) return false; |
| 1130 | |
| 1131 | try { |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1132 | mService.readRemoteRssi(mClientIf, mDevice.getAddress()); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 1133 | } catch (RemoteException e) { |
| 1134 | Log.e(TAG,"",e); |
| 1135 | return false; |
| 1136 | } |
| 1137 | |
| 1138 | return true; |
| 1139 | } |
| 1140 | |
| 1141 | /** |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1142 | * Not supported - please use {@link BluetoothManager#getConnectedDevices(int)} |
| 1143 | * with {@link BluetoothProfile#GATT} as argument |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 1144 | * |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1145 | * @throws UnsupportedOperationException |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 1146 | */ |
| 1147 | @Override |
| 1148 | public int getConnectionState(BluetoothDevice device) { |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1149 | throw new UnsupportedOperationException("Use BluetoothManager#getConnectionState instead."); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 1150 | } |
| 1151 | |
| 1152 | /** |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1153 | * Not supported - please use {@link BluetoothManager#getConnectedDevices(int)} |
| 1154 | * with {@link BluetoothProfile#GATT} as argument |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 1155 | * |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1156 | * @throws UnsupportedOperationException |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 1157 | */ |
| 1158 | @Override |
| 1159 | public List<BluetoothDevice> getConnectedDevices() { |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1160 | throw new UnsupportedOperationException |
| 1161 | ("Use BluetoothManager#getConnectedDevices instead."); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 1162 | } |
| 1163 | |
| 1164 | /** |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1165 | * Not supported - please use |
| 1166 | * {@link BluetoothManager#getDevicesMatchingConnectionStates(int, int[])} |
| 1167 | * with {@link BluetoothProfile#GATT} as first argument |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 1168 | * |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1169 | * @throws UnsupportedOperationException |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 1170 | */ |
| 1171 | @Override |
| 1172 | public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 1173 | throw new UnsupportedOperationException |
| 1174 | ("Use BluetoothManager#getDevicesMatchingConnectionStates instead."); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 1175 | } |
| 1176 | } |