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