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