blob: c9df9c0eede20f8222cf4a86385b32015825b0c1 [file] [log] [blame]
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001/*
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
17package android.bluetooth;
18
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -080019import android.content.Context;
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -080020import android.os.ParcelUuid;
21import android.os.RemoteException;
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -080022import android.util.Log;
23
24import java.util.ArrayList;
25import java.util.List;
26import java.util.UUID;
27
28/**
Matthew Xieddf7e472013-03-01 18:41:02 -080029 * Public API for the Bluetooth GATT Profile.
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -080030 *
Matthew Xieddf7e472013-03-01 18:41:02 -080031 * <p>This class provides Bluetooth GATT functionality to enable communication
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -080032 * with Bluetooth Smart or Smart Ready devices.
33 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -080034 * <p>To connect to a remote peripheral device, create a {@link BluetoothGattCallback}
Matthew Xie33ec9842013-04-03 00:29:27 -070035 * and call {@link BluetoothDevice#connectGatt} to get a instance of this class.
Matthew Xieddf7e472013-03-01 18:41:02 -080036 * GATT capable devices can be discovered using the Bluetooth device discovery or BLE
37 * scan process.
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -080038 */
39public final class BluetoothGatt implements BluetoothProfile {
40 private static final String TAG = "BluetoothGatt";
41 private static final boolean DBG = true;
Matthew Xieddf7e472013-03-01 18:41:02 -080042 private static final boolean VDBG = true;
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -080043
Matthew Xieddf7e472013-03-01 18:41:02 -080044 private final Context mContext;
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -080045 private IBluetoothGatt mService;
46 private BluetoothGattCallback mCallback;
47 private int mClientIf;
48 private boolean mAuthRetry = false;
Matthew Xieddf7e472013-03-01 18:41:02 -080049 private BluetoothDevice mDevice;
50 private boolean mAutoConnect;
51 private int mConnState;
52 private final Object mStateLock = new Object();
Andre Eisenbachcc68cc92014-03-18 14:26:51 -070053 private Boolean mDeviceBusy = false;
Ganesh Ganapathi Battab88fa822014-04-18 10:00:40 -070054 private int mTransport;
Matthew Xieddf7e472013-03-01 18:41:02 -080055
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 Xie33ec9842013-04-03 00:29:27 -070060 private static final int CONN_STATE_CLOSED = 4;
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -080061
62 private List<BluetoothGattService> mServices;
63
Matthew Xieddf7e472013-03-01 18:41:02 -080064 /** A GATT operation completed successfully */
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -080065 public static final int GATT_SUCCESS = 0;
66
Matthew Xieddf7e472013-03-01 18:41:02 -080067 /** GATT read operation is not permitted */
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -080068 public static final int GATT_READ_NOT_PERMITTED = 0x2;
69
Matthew Xieddf7e472013-03-01 18:41:02 -080070 /** GATT write operation is not permitted */
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -080071 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 Xie90ca8072013-05-28 21:06:50 +000088 /** A GATT operation failed, errors other than the above */
89 public static final int GATT_FAILURE = 0x101;
90
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -080091 /**
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 Batta99081122013-02-05 15:28:33 -0800110 * 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 Xieddf7e472013-03-01 18:41:02 -0800121 if (VDBG) {
122 synchronized(mStateLock) {
123 if (mConnState != CONN_STATE_CONNECTING) {
124 Log.e(TAG, "Bad connection state: " + mConnState);
125 }
126 }
127 }
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800128 mClientIf = clientIf;
Matthew Xieddf7e472013-03-01 18:41:02 -0800129 if (status != GATT_SUCCESS) {
Matthew Xie33ec9842013-04-03 00:29:27 -0700130 mCallback.onConnectionStateChange(BluetoothGatt.this, GATT_FAILURE,
Matthew Xieddf7e472013-03-01 18:41:02 -0800131 BluetoothProfile.STATE_DISCONNECTED);
132 synchronized(mStateLock) {
133 mConnState = CONN_STATE_IDLE;
134 }
135 return;
136 }
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800137 try {
Matthew Xieddf7e472013-03-01 18:41:02 -0800138 mService.clientConnect(mClientIf, mDevice.getAddress(),
Ganesh Ganapathi Battab88fa822014-04-18 10:00:40 -0700139 !mAutoConnect, mTransport); // autoConnect is inverse of "isDirect"
Matthew Xieddf7e472013-03-01 18:41:02 -0800140 } catch (RemoteException e) {
141 Log.e(TAG,"",e);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800142 }
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 Xieddf7e472013-03-01 18:41:02 -0800153 if (!address.equals(mDevice.getAddress())) {
154 return;
155 }
156 int profileState = connected ? BluetoothProfile.STATE_CONNECTED :
157 BluetoothProfile.STATE_DISCONNECTED;
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800158 try {
Matthew Xie33ec9842013-04-03 00:29:27 -0700159 mCallback.onConnectionStateChange(BluetoothGatt.this, status, profileState);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800160 } catch (Exception ex) {
Mike Lockwood0998ff12013-05-13 14:04:12 -0700161 Log.w(TAG, "Unhandled exception in callback", ex);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800162 }
Matthew Xieddf7e472013-03-01 18:41:02 -0800163
164 synchronized(mStateLock) {
165 if (connected) {
166 mConnState = CONN_STATE_CONNECTED;
167 } else {
168 mConnState = CONN_STATE_IDLE;
169 }
170 }
Andre Eisenbachcc68cc92014-03-18 14:26:51 -0700171
172 synchronized(mDeviceBusy) {
173 mDeviceBusy = false;
174 }
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800175 }
176
177 /**
178 * Callback reporting an LE scan result.
179 * @hide
180 */
181 public void onScanResult(String address, int rssi, byte[] advData) {
Matthew Xieddf7e472013-03-01 18:41:02 -0800182 // no op
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800183 }
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 Xieddf7e472013-03-01 18:41:02 -0800194 if (!address.equals(mDevice.getAddress())) {
195 return;
196 }
197 mServices.add(new BluetoothGattService(mDevice, srvcUuid.getUuid(),
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800198 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 Xieddf7e472013-03-01 18:41:02 -0800213 if (!address.equals(mDevice.getAddress())) {
214 return;
215 }
216 BluetoothGattService service = getService(mDevice,
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800217 srvcUuid.getUuid(), srvcInstId, srvcType);
Matthew Xieddf7e472013-03-01 18:41:02 -0800218 BluetoothGattService includedService = getService(mDevice,
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800219 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 Xieddf7e472013-03-01 18:41:02 -0800239 if (!address.equals(mDevice.getAddress())) {
240 return;
241 }
242 BluetoothGattService service = getService(mDevice, srvcUuid.getUuid(),
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800243 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 Eisenbach25b9cf92013-07-08 23:58:16 -0700259 int descrInstId, ParcelUuid descUuid) {
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800260 if (DBG) Log.d(TAG, "onGetDescriptor() - Device=" + address + " UUID=" + descUuid);
261
Matthew Xieddf7e472013-03-01 18:41:02 -0800262 if (!address.equals(mDevice.getAddress())) {
263 return;
264 }
265 BluetoothGattService service = getService(mDevice, srvcUuid.getUuid(),
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800266 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 Eisenbach25b9cf92013-07-08 23:58:16 -0700274 characteristic, descUuid.getUuid(), descrInstId, 0));
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800275 }
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 Xieddf7e472013-03-01 18:41:02 -0800286 if (!address.equals(mDevice.getAddress())) {
287 return;
288 }
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800289 try {
Matthew Xie33ec9842013-04-03 00:29:27 -0700290 mCallback.onServicesDiscovered(BluetoothGatt.this, status);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800291 } catch (Exception ex) {
Mike Lockwood0998ff12013-05-13 14:04:12 -0700292 Log.w(TAG, "Unhandled exception in callback", ex);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800293 }
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 Xieddf7e472013-03-01 18:41:02 -0800307 if (!address.equals(mDevice.getAddress())) {
308 return;
309 }
Andre Eisenbachcc68cc92014-03-18 14:26:51 -0700310
311 synchronized(mDeviceBusy) {
312 mDeviceBusy = false;
313 }
314
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800315 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 Xieddf7e472013-03-01 18:41:02 -0800331 BluetoothGattService service = getService(mDevice, srvcUuid.getUuid(),
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800332 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 Xie33ec9842013-04-03 00:29:27 -0700342 mCallback.onCharacteristicRead(BluetoothGatt.this, characteristic, status);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800343 } catch (Exception ex) {
Mike Lockwood0998ff12013-05-13 14:04:12 -0700344 Log.w(TAG, "Unhandled exception in callback", ex);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800345 }
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 Xieddf7e472013-03-01 18:41:02 -0800359 if (!address.equals(mDevice.getAddress())) {
360 return;
361 }
Andre Eisenbachcc68cc92014-03-18 14:26:51 -0700362
363 synchronized(mDeviceBusy) {
364 mDeviceBusy = false;
365 }
366
Matthew Xieddf7e472013-03-01 18:41:02 -0800367 BluetoothGattService service = getService(mDevice, srvcUuid.getUuid(),
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800368 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 Xie33ec9842013-04-03 00:29:27 -0700393 mCallback.onCharacteristicWrite(BluetoothGatt.this, characteristic, status);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800394 } catch (Exception ex) {
Mike Lockwood0998ff12013-05-13 14:04:12 -0700395 Log.w(TAG, "Unhandled exception in callback", ex);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800396 }
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 Xieddf7e472013-03-01 18:41:02 -0800410 if (!address.equals(mDevice.getAddress())) {
411 return;
412 }
413 BluetoothGattService service = getService(mDevice, srvcUuid.getUuid(),
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800414 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 Xie33ec9842013-04-03 00:29:27 -0700424 mCallback.onCharacteristicChanged(BluetoothGatt.this, characteristic);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800425 } catch (Exception ex) {
Mike Lockwood0998ff12013-05-13 14:04:12 -0700426 Log.w(TAG, "Unhandled exception in callback", ex);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800427 }
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 Eisenbach25b9cf92013-07-08 23:58:16 -0700437 int descrInstId, ParcelUuid descrUuid,
438 byte[] value) {
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800439 if (DBG) Log.d(TAG, "onDescriptorRead() - Device=" + address + " UUID=" + charUuid);
440
Matthew Xieddf7e472013-03-01 18:41:02 -0800441 if (!address.equals(mDevice.getAddress())) {
442 return;
443 }
Andre Eisenbachcc68cc92014-03-18 14:26:51 -0700444
445 synchronized(mDeviceBusy) {
446 mDeviceBusy = false;
447 }
448
Matthew Xieddf7e472013-03-01 18:41:02 -0800449 BluetoothGattService service = getService(mDevice, srvcUuid.getUuid(),
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800450 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 Eisenbach25b9cf92013-07-08 23:58:16 -0700458 descrUuid.getUuid(), descrInstId);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800459 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 Eisenbach25b9cf92013-07-08 23:58:16 -0700470 descrInstId, descrUuid, AUTHENTICATION_MITM);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800471 } catch (RemoteException e) {
472 Log.e(TAG,"",e);
473 }
474 }
475
476 mAuthRetry = true;
477
478 try {
Matthew Xie33ec9842013-04-03 00:29:27 -0700479 mCallback.onDescriptorRead(BluetoothGatt.this, descriptor, status);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800480 } catch (Exception ex) {
Mike Lockwood0998ff12013-05-13 14:04:12 -0700481 Log.w(TAG, "Unhandled exception in callback", ex);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800482 }
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 Eisenbach25b9cf92013-07-08 23:58:16 -0700492 int descrInstId, ParcelUuid descrUuid) {
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800493 if (DBG) Log.d(TAG, "onDescriptorWrite() - Device=" + address + " UUID=" + charUuid);
494
Matthew Xieddf7e472013-03-01 18:41:02 -0800495 if (!address.equals(mDevice.getAddress())) {
496 return;
497 }
Andre Eisenbachcc68cc92014-03-18 14:26:51 -0700498
499 synchronized(mDeviceBusy) {
500 mDeviceBusy = false;
501 }
502
Matthew Xieddf7e472013-03-01 18:41:02 -0800503 BluetoothGattService service = getService(mDevice, srvcUuid.getUuid(),
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800504 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 Eisenbach25b9cf92013-07-08 23:58:16 -0700512 descrUuid.getUuid(), descrInstId);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800513 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 Eisenbach25b9cf92013-07-08 23:58:16 -0700522 descrInstId, descrUuid, characteristic.getWriteType(),
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800523 AUTHENTICATION_MITM, descriptor.getValue());
524 } catch (RemoteException e) {
525 Log.e(TAG,"",e);
526 }
527 }
528
529 mAuthRetry = false;
530
531 try {
Matthew Xie33ec9842013-04-03 00:29:27 -0700532 mCallback.onDescriptorWrite(BluetoothGatt.this, descriptor, status);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800533 } catch (Exception ex) {
Mike Lockwood0998ff12013-05-13 14:04:12 -0700534 Log.w(TAG, "Unhandled exception in callback", ex);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800535 }
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 Xieddf7e472013-03-01 18:41:02 -0800545 if (!address.equals(mDevice.getAddress())) {
546 return;
547 }
Andre Eisenbachcc68cc92014-03-18 14:26:51 -0700548
549 synchronized(mDeviceBusy) {
550 mDeviceBusy = false;
551 }
552
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800553 try {
Matthew Xie33ec9842013-04-03 00:29:27 -0700554 mCallback.onReliableWriteCompleted(BluetoothGatt.this, status);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800555 } catch (Exception ex) {
Mike Lockwood0998ff12013-05-13 14:04:12 -0700556 Log.w(TAG, "Unhandled exception in callback", ex);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800557 }
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 Xieddf7e472013-03-01 18:41:02 -0800567 if (!address.equals(mDevice.getAddress())) {
568 return;
569 }
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800570 try {
Matthew Xie33ec9842013-04-03 00:29:27 -0700571 mCallback.onReadRemoteRssi(BluetoothGatt.this, rssi, status);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800572 } catch (Exception ex) {
Mike Lockwood0998ff12013-05-13 14:04:12 -0700573 Log.w(TAG, "Unhandled exception in callback", ex);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800574 }
575 }
Wei Wangf3055892014-03-11 22:22:41 -0700576
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 Wangadf6aff2014-05-20 06:30:20 +0000584 }
585
586 /**
587 * @hide
588 */
589 @Override
590 public void onMultiAdvertiseCallback(int status) {
591 // no op.
592 }
Andre Eisenbach580b0a12014-03-25 06:31:50 -0700593
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 Wangf3055892014-03-11 22:22:41 -0700609 }
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800610 };
611
Ganesh Ganapathi Battab88fa822014-04-18 10:00:40 -0700612 /*package*/ BluetoothGatt(Context context, IBluetoothGatt iGatt, BluetoothDevice device,
613 int transport) {
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800614 mContext = context;
Matthew Xieddf7e472013-03-01 18:41:02 -0800615 mService = iGatt;
616 mDevice = device;
Ganesh Ganapathi Battab88fa822014-04-18 10:00:40 -0700617 mTransport = transport;
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800618 mServices = new ArrayList<BluetoothGattService>();
619
Matthew Xieddf7e472013-03-01 18:41:02 -0800620 mConnState = CONN_STATE_IDLE;
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800621 }
622
623 /**
Matthew Xie33ec9842013-04-03 00:29:27 -0700624 * Close this Bluetooth GATT client.
Matthew Xieb30f91e2013-05-29 10:19:06 -0700625 *
626 * Application should call this method as early as possible after it is done with
627 * this GATT client.
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800628 */
Matthew Xie33ec9842013-04-03 00:29:27 -0700629 public void close() {
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800630 if (DBG) Log.d(TAG, "close()");
631
632 unregisterApp();
Matthew Xie33ec9842013-04-03 00:29:27 -0700633 mConnState = CONN_STATE_CLOSED;
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800634 }
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 Xieddf7e472013-03-01 18:41:02 -0800655 * Register an application callback to start using GATT.
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800656 *
Matthew Xieddf7e472013-03-01 18:41:02 -0800657 * <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 Batta99081122013-02-05 15:28:33 -0800659 *
660 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
661 *
Matthew Xieddf7e472013-03-01 18:41:02 -0800662 * @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 Batta99081122013-02-05 15:28:33 -0800665 */
Matthew Xieddf7e472013-03-01 18:41:02 -0800666 private boolean registerApp(BluetoothGattCallback callback) {
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800667 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 Xieddf7e472013-03-01 18:41:02 -0800687 private void unregisterApp() {
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800688 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 Xieddf7e472013-03-01 18:41:02 -0800701 * Initiate a connection to a Bluetooth GATT capable device.
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800702 *
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 Xieddf7e472013-03-01 18:41:02 -0800713 * autoConnect parameter set to true.
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800714 *
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 Xieddf7e472013-03-01 18:41:02 -0800723 /*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 Batta99081122013-02-05 15:28:33 -0800736 return false;
737 }
738
Matthew Xieddf7e472013-03-01 18:41:02 -0800739 // the connection will continue after successful callback registration
740 mAutoConnect = autoConnect;
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800741 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 Batta99081122013-02-05 15:28:33 -0800749 */
Matthew Xieddf7e472013-03-01 18:41:02 -0800750 public void disconnect() {
751 if (DBG) Log.d(TAG, "cancelOpen() - device: " + mDevice.getAddress());
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800752 if (mService == null || mClientIf == 0) return;
753
754 try {
Matthew Xieddf7e472013-03-01 18:41:02 -0800755 mService.clientDisconnect(mClientIf, mDevice.getAddress());
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800756 } catch (RemoteException e) {
757 Log.e(TAG,"",e);
758 }
Matthew Xie33ec9842013-04-03 00:29:27 -0700759 }
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 Battab88fa822014-04-18 10:00:40 -0700773 false, mTransport); // autoConnect is inverse of "isDirect"
Matthew Xie33ec9842013-04-03 00:29:27 -0700774 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 Batta99081122013-02-05 15:28:33 -0800788 }
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 Batta99081122013-02-05 15:28:33 -0800801 * @return true, if the remote service discovery has been started
802 */
Matthew Xieddf7e472013-03-01 18:41:02 -0800803 public boolean discoverServices() {
804 if (DBG) Log.d(TAG, "discoverServices() - device: " + mDevice.getAddress());
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800805 if (mService == null || mClientIf == 0) return false;
806
807 mServices.clear();
808
809 try {
Matthew Xieddf7e472013-03-01 18:41:02 -0800810 mService.discoverServices(mClientIf, mDevice.getAddress());
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800811 } 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 Batta99081122013-02-05 15:28:33 -0800827 * @return List of services on the remote device. Returns an empty list
828 * if service discovery has not yet been performed.
829 */
Matthew Xieddf7e472013-03-01 18:41:02 -0800830 public List<BluetoothGattService> getServices() {
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800831 List<BluetoothGattService> result =
832 new ArrayList<BluetoothGattService>();
833
834 for (BluetoothGattService service : mServices) {
Matthew Xieddf7e472013-03-01 18:41:02 -0800835 if (service.getDevice().equals(mDevice)) {
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800836 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 Batta99081122013-02-05 15:28:33 -0800855 * @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 Xieddf7e472013-03-01 18:41:02 -0800859 public BluetoothGattService getService(UUID uuid) {
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800860 for (BluetoothGattService service : mServices) {
Matthew Xieddf7e472013-03-01 18:41:02 -0800861 if (service.getDevice().equals(mDevice) &&
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800862 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 Eisenbachcc68cc92014-03-18 14:26:51 -0700895 synchronized(mDeviceBusy) {
896 if (mDeviceBusy) return false;
897 mDeviceBusy = true;
898 }
899
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800900 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 Eisenbachcc68cc92014-03-18 14:26:51 -0700907 mDeviceBusy = false;
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800908 return false;
909 }
910
911 return true;
912 }
913
914 /**
Matthew Xieddf7e472013-03-01 18:41:02 -0800915 * Writes a given characteristic and its values to the associated remote device.
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800916 *
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 Eisenbachcc68cc92014-03-18 14:26:51 -0700940 synchronized(mDeviceBusy) {
941 if (mDeviceBusy) return false;
942 mDeviceBusy = true;
943 }
944
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800945 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 Eisenbachcc68cc92014-03-18 14:26:51 -0700954 mDeviceBusy = false;
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800955 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 Eisenbachcc68cc92014-03-18 14:26:51 -0700986 synchronized(mDeviceBusy) {
987 if (mDeviceBusy) return false;
988 mDeviceBusy = true;
989 }
990
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800991 try {
Andre Eisenbach25b9cf92013-07-08 23:58:16 -0700992 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 Batta99081122013-02-05 15:28:33 -0800997 } catch (RemoteException e) {
998 Log.e(TAG,"",e);
Andre Eisenbachcc68cc92014-03-18 14:26:51 -0700999 mDeviceBusy = false;
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001000 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 Eisenbachcc68cc92014-03-18 14:26:51 -07001030 synchronized(mDeviceBusy) {
1031 if (mDeviceBusy) return false;
1032 mDeviceBusy = true;
1033 }
1034
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001035 try {
Andre Eisenbach25b9cf92013-07-08 23:58:16 -07001036 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 Batta99081122013-02-05 15:28:33 -08001040 characteristic.getWriteType(), AUTHENTICATION_NONE,
1041 descriptor.getValue());
1042 } catch (RemoteException e) {
1043 Log.e(TAG,"",e);
Andre Eisenbachcc68cc92014-03-18 14:26:51 -07001044 mDeviceBusy = false;
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001045 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 Batta99081122013-02-05 15:28:33 -08001069 * @return true, if the reliable write transaction has been initiated
1070 */
Matthew Xieddf7e472013-03-01 18:41:02 -08001071 public boolean beginReliableWrite() {
1072 if (DBG) Log.d(TAG, "beginReliableWrite() - device: " + mDevice.getAddress());
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001073 if (mService == null || mClientIf == 0) return false;
1074
1075 try {
Matthew Xieddf7e472013-03-01 18:41:02 -08001076 mService.beginReliableWrite(mClientIf, mDevice.getAddress());
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001077 } 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 Batta99081122013-02-05 15:28:33 -08001096 * @return true, if the request to execute the transaction has been sent
1097 */
Matthew Xieddf7e472013-03-01 18:41:02 -08001098 public boolean executeReliableWrite() {
1099 if (DBG) Log.d(TAG, "executeReliableWrite() - device: " + mDevice.getAddress());
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001100 if (mService == null || mClientIf == 0) return false;
1101
Andre Eisenbachcc68cc92014-03-18 14:26:51 -07001102 synchronized(mDeviceBusy) {
1103 if (mDeviceBusy) return false;
1104 mDeviceBusy = true;
1105 }
1106
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001107 try {
Matthew Xieddf7e472013-03-01 18:41:02 -08001108 mService.endReliableWrite(mClientIf, mDevice.getAddress(), true);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001109 } catch (RemoteException e) {
1110 Log.e(TAG,"",e);
Andre Eisenbachcc68cc92014-03-18 14:26:51 -07001111 mDeviceBusy = false;
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001112 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 Batta99081122013-02-05 15:28:33 -08001125 */
John Du48f8b5d2013-08-19 12:20:37 -07001126 public void abortReliableWrite() {
Matthew Xieddf7e472013-03-01 18:41:02 -08001127 if (DBG) Log.d(TAG, "abortReliableWrite() - device: " + mDevice.getAddress());
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001128 if (mService == null || mClientIf == 0) return;
1129
1130 try {
Matthew Xieddf7e472013-03-01 18:41:02 -08001131 mService.endReliableWrite(mClientIf, mDevice.getAddress(), false);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001132 } catch (RemoteException e) {
1133 Log.e(TAG,"",e);
1134 }
1135 }
1136
1137 /**
John Dub7b7d7a2013-08-20 14:03:28 -07001138 * @deprecated Use {@link #abortReliableWrite()}
John Du48f8b5d2013-08-19 12:20:37 -07001139 */
1140 public void abortReliableWrite(BluetoothDevice mDevice) {
1141 abortReliableWrite();
1142 }
1143
1144 /**
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001145 * 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 Xieddf7e472013-03-01 18:41:02 -08001189 public boolean refresh() {
1190 if (DBG) Log.d(TAG, "refresh() - device: " + mDevice.getAddress());
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001191 if (mService == null || mClientIf == 0) return false;
1192
1193 try {
Matthew Xieddf7e472013-03-01 18:41:02 -08001194 mService.refreshDevice(mClientIf, mDevice.getAddress());
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001195 } 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 Batta99081122013-02-05 15:28:33 -08001211 * @return true, if the RSSI value has been requested successfully
1212 */
Matthew Xieddf7e472013-03-01 18:41:02 -08001213 public boolean readRemoteRssi() {
1214 if (DBG) Log.d(TAG, "readRssi() - device: " + mDevice.getAddress());
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001215 if (mService == null || mClientIf == 0) return false;
1216
1217 try {
Matthew Xieddf7e472013-03-01 18:41:02 -08001218 mService.readRemoteRssi(mClientIf, mDevice.getAddress());
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001219 } catch (RemoteException e) {
1220 Log.e(TAG,"",e);
1221 return false;
1222 }
1223
1224 return true;
1225 }
1226
1227 /**
Andre Eisenbach580b0a12014-03-25 06:31:50 -07001228 * 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 Xieddf7e472013-03-01 18:41:02 -08001258 * Not supported - please use {@link BluetoothManager#getConnectedDevices(int)}
1259 * with {@link BluetoothProfile#GATT} as argument
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001260 *
Matthew Xieddf7e472013-03-01 18:41:02 -08001261 * @throws UnsupportedOperationException
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001262 */
1263 @Override
1264 public int getConnectionState(BluetoothDevice device) {
Matthew Xieddf7e472013-03-01 18:41:02 -08001265 throw new UnsupportedOperationException("Use BluetoothManager#getConnectionState instead.");
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001266 }
1267
1268 /**
Matthew Xieddf7e472013-03-01 18:41:02 -08001269 * Not supported - please use {@link BluetoothManager#getConnectedDevices(int)}
1270 * with {@link BluetoothProfile#GATT} as argument
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001271 *
Matthew Xieddf7e472013-03-01 18:41:02 -08001272 * @throws UnsupportedOperationException
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001273 */
1274 @Override
1275 public List<BluetoothDevice> getConnectedDevices() {
Matthew Xieddf7e472013-03-01 18:41:02 -08001276 throw new UnsupportedOperationException
1277 ("Use BluetoothManager#getConnectedDevices instead.");
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001278 }
1279
1280 /**
Matthew Xieddf7e472013-03-01 18:41:02 -08001281 * Not supported - please use
1282 * {@link BluetoothManager#getDevicesMatchingConnectionStates(int, int[])}
1283 * with {@link BluetoothProfile#GATT} as first argument
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001284 *
Matthew Xieddf7e472013-03-01 18:41:02 -08001285 * @throws UnsupportedOperationException
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001286 */
1287 @Override
1288 public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) {
Matthew Xieddf7e472013-03-01 18:41:02 -08001289 throw new UnsupportedOperationException
1290 ("Use BluetoothManager#getDevicesMatchingConnectionStates instead.");
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001291 }
1292}