blob: bbd0439e51a4fb943b3d98ba5c1e61f5c363a6fd [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.bluetooth.BluetoothDevice;
20import android.bluetooth.BluetoothProfile;
21import android.bluetooth.BluetoothProfile.ServiceListener;
22import android.bluetooth.IBluetoothManager;
23import android.bluetooth.IBluetoothStateChangeCallback;
24
25import android.content.ComponentName;
26import android.content.Context;
27import android.content.Intent;
28import android.content.ServiceConnection;
29import android.os.IBinder;
30import android.os.ParcelUuid;
31import android.os.RemoteException;
32import android.os.ServiceManager;
33import android.util.Log;
34
35import java.util.ArrayList;
36import java.util.List;
37import java.util.UUID;
38
39/**
Matthew Xieddf7e472013-03-01 18:41:02 -080040 * Public API for the Bluetooth GATT Profile.
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -080041 *
Matthew Xieddf7e472013-03-01 18:41:02 -080042 * <p>This class provides Bluetooth GATT functionality to enable communication
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -080043 * with Bluetooth Smart or Smart Ready devices.
44 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -080045 * <p>To connect to a remote peripheral device, create a {@link BluetoothGattCallback}
Matthew Xie33ec9842013-04-03 00:29:27 -070046 * and call {@link BluetoothDevice#connectGatt} to get a instance of this class.
Matthew Xieddf7e472013-03-01 18:41:02 -080047 * GATT capable devices can be discovered using the Bluetooth device discovery or BLE
48 * scan process.
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -080049 */
50public final class BluetoothGatt implements BluetoothProfile {
51 private static final String TAG = "BluetoothGatt";
52 private static final boolean DBG = true;
Matthew Xieddf7e472013-03-01 18:41:02 -080053 private static final boolean VDBG = true;
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -080054
Matthew Xieddf7e472013-03-01 18:41:02 -080055 private final Context mContext;
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -080056 private IBluetoothGatt mService;
57 private BluetoothGattCallback mCallback;
58 private int mClientIf;
59 private boolean mAuthRetry = false;
Matthew Xieddf7e472013-03-01 18:41:02 -080060 private BluetoothDevice mDevice;
61 private boolean mAutoConnect;
62 private int mConnState;
63 private final Object mStateLock = new Object();
64
65 private static final int CONN_STATE_IDLE = 0;
66 private static final int CONN_STATE_CONNECTING = 1;
67 private static final int CONN_STATE_CONNECTED = 2;
68 private static final int CONN_STATE_DISCONNECTING = 3;
Matthew Xie33ec9842013-04-03 00:29:27 -070069 private static final int CONN_STATE_CLOSED = 4;
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -080070
71 private List<BluetoothGattService> mServices;
72
Matthew Xieddf7e472013-03-01 18:41:02 -080073 /** A GATT operation completed successfully */
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -080074 public static final int GATT_SUCCESS = 0;
75
Matthew Xieddf7e472013-03-01 18:41:02 -080076 /** GATT read operation is not permitted */
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -080077 public static final int GATT_READ_NOT_PERMITTED = 0x2;
78
Matthew Xieddf7e472013-03-01 18:41:02 -080079 /** GATT write operation is not permitted */
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -080080 public static final int GATT_WRITE_NOT_PERMITTED = 0x3;
81
82 /** Insufficient authentication for a given operation */
83 public static final int GATT_INSUFFICIENT_AUTHENTICATION = 0x5;
84
85 /** The given request is not supported */
86 public static final int GATT_REQUEST_NOT_SUPPORTED = 0x6;
87
88 /** Insufficient encryption for a given operation */
89 public static final int GATT_INSUFFICIENT_ENCRYPTION = 0xf;
90
91 /** A read or write operation was requested with an invalid offset */
92 public static final int GATT_INVALID_OFFSET = 0x7;
93
94 /** A write operation exceeds the maximum length of the attribute */
95 public static final int GATT_INVALID_ATTRIBUTE_LENGTH = 0xd;
96
Matthew Xie90ca8072013-05-28 21:06:50 +000097 /** A GATT operation failed, errors other than the above */
98 public static final int GATT_FAILURE = 0x101;
99
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800100 /**
101 * No authentication required.
102 * @hide
103 */
104 /*package*/ static final int AUTHENTICATION_NONE = 0;
105
106 /**
107 * Authentication requested; no man-in-the-middle protection required.
108 * @hide
109 */
110 /*package*/ static final int AUTHENTICATION_NO_MITM = 1;
111
112 /**
113 * Authentication with man-in-the-middle protection requested.
114 * @hide
115 */
116 /*package*/ static final int AUTHENTICATION_MITM = 2;
117
118 /**
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800119 * Bluetooth GATT interface callbacks
120 */
121 private final IBluetoothGattCallback mBluetoothGattCallback =
122 new IBluetoothGattCallback.Stub() {
123 /**
124 * Application interface registered - app is ready to go
125 * @hide
126 */
127 public void onClientRegistered(int status, int clientIf) {
128 if (DBG) Log.d(TAG, "onClientRegistered() - status=" + status
129 + " clientIf=" + clientIf);
Matthew Xieddf7e472013-03-01 18:41:02 -0800130 if (VDBG) {
131 synchronized(mStateLock) {
132 if (mConnState != CONN_STATE_CONNECTING) {
133 Log.e(TAG, "Bad connection state: " + mConnState);
134 }
135 }
136 }
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800137 mClientIf = clientIf;
Matthew Xieddf7e472013-03-01 18:41:02 -0800138 if (status != GATT_SUCCESS) {
Matthew Xie33ec9842013-04-03 00:29:27 -0700139 mCallback.onConnectionStateChange(BluetoothGatt.this, GATT_FAILURE,
Matthew Xieddf7e472013-03-01 18:41:02 -0800140 BluetoothProfile.STATE_DISCONNECTED);
141 synchronized(mStateLock) {
142 mConnState = CONN_STATE_IDLE;
143 }
144 return;
145 }
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800146 try {
Matthew Xieddf7e472013-03-01 18:41:02 -0800147 mService.clientConnect(mClientIf, mDevice.getAddress(),
148 !mAutoConnect); // autoConnect is inverse of "isDirect"
149 } catch (RemoteException e) {
150 Log.e(TAG,"",e);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800151 }
152 }
153
154 /**
155 * Client connection state changed
156 * @hide
157 */
158 public void onClientConnectionState(int status, int clientIf,
159 boolean connected, String address) {
160 if (DBG) Log.d(TAG, "onClientConnectionState() - status=" + status
161 + " clientIf=" + clientIf + " device=" + address);
Matthew Xieddf7e472013-03-01 18:41:02 -0800162 if (!address.equals(mDevice.getAddress())) {
163 return;
164 }
165 int profileState = connected ? BluetoothProfile.STATE_CONNECTED :
166 BluetoothProfile.STATE_DISCONNECTED;
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800167 try {
Matthew Xie33ec9842013-04-03 00:29:27 -0700168 mCallback.onConnectionStateChange(BluetoothGatt.this, status, profileState);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800169 } catch (Exception ex) {
Mike Lockwood0998ff12013-05-13 14:04:12 -0700170 Log.w(TAG, "Unhandled exception in callback", ex);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800171 }
Matthew Xieddf7e472013-03-01 18:41:02 -0800172
173 synchronized(mStateLock) {
174 if (connected) {
175 mConnState = CONN_STATE_CONNECTED;
176 } else {
177 mConnState = CONN_STATE_IDLE;
178 }
179 }
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800180 }
181
182 /**
183 * Callback reporting an LE scan result.
184 * @hide
185 */
186 public void onScanResult(String address, int rssi, byte[] advData) {
Matthew Xieddf7e472013-03-01 18:41:02 -0800187 // no op
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800188 }
189
190 /**
191 * A new GATT service has been discovered.
192 * The service is added to the internal list and the search
193 * continues.
194 * @hide
195 */
196 public void onGetService(String address, int srvcType,
197 int srvcInstId, ParcelUuid srvcUuid) {
198 if (DBG) Log.d(TAG, "onGetService() - Device=" + address + " UUID=" + srvcUuid);
Matthew Xieddf7e472013-03-01 18:41:02 -0800199 if (!address.equals(mDevice.getAddress())) {
200 return;
201 }
202 mServices.add(new BluetoothGattService(mDevice, srvcUuid.getUuid(),
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800203 srvcInstId, srvcType));
204 }
205
206 /**
207 * An included service has been found durig GATT discovery.
208 * The included service is added to the respective parent.
209 * @hide
210 */
211 public void onGetIncludedService(String address, int srvcType,
212 int srvcInstId, ParcelUuid srvcUuid,
213 int inclSrvcType, int inclSrvcInstId,
214 ParcelUuid inclSrvcUuid) {
215 if (DBG) Log.d(TAG, "onGetIncludedService() - Device=" + address
216 + " UUID=" + srvcUuid + " Included=" + inclSrvcUuid);
217
Matthew Xieddf7e472013-03-01 18:41:02 -0800218 if (!address.equals(mDevice.getAddress())) {
219 return;
220 }
221 BluetoothGattService service = getService(mDevice,
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800222 srvcUuid.getUuid(), srvcInstId, srvcType);
Matthew Xieddf7e472013-03-01 18:41:02 -0800223 BluetoothGattService includedService = getService(mDevice,
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800224 inclSrvcUuid.getUuid(), inclSrvcInstId, inclSrvcType);
225
226 if (service != null && includedService != null) {
227 service.addIncludedService(includedService);
228 }
229 }
230
231 /**
232 * A new GATT characteristic has been discovered.
233 * Add the new characteristic to the relevant service and continue
234 * the remote device inspection.
235 * @hide
236 */
237 public void onGetCharacteristic(String address, int srvcType,
238 int srvcInstId, ParcelUuid srvcUuid,
239 int charInstId, ParcelUuid charUuid,
240 int charProps) {
241 if (DBG) Log.d(TAG, "onGetCharacteristic() - Device=" + address + " UUID=" +
242 charUuid);
243
Matthew Xieddf7e472013-03-01 18:41:02 -0800244 if (!address.equals(mDevice.getAddress())) {
245 return;
246 }
247 BluetoothGattService service = getService(mDevice, srvcUuid.getUuid(),
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800248 srvcInstId, srvcType);
249 if (service != null) {
250 service.addCharacteristic(new BluetoothGattCharacteristic(
251 service, charUuid.getUuid(), charInstId, charProps, 0));
252 }
253 }
254
255 /**
256 * A new GATT descriptor has been discovered.
257 * Finally, add the descriptor to the related characteristic.
258 * This should conclude the remote device update.
259 * @hide
260 */
261 public void onGetDescriptor(String address, int srvcType,
262 int srvcInstId, ParcelUuid srvcUuid,
263 int charInstId, ParcelUuid charUuid,
Andre Eisenbach25b9cf92013-07-08 23:58:16 -0700264 int descrInstId, ParcelUuid descUuid) {
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800265 if (DBG) Log.d(TAG, "onGetDescriptor() - Device=" + address + " UUID=" + descUuid);
266
Matthew Xieddf7e472013-03-01 18:41:02 -0800267 if (!address.equals(mDevice.getAddress())) {
268 return;
269 }
270 BluetoothGattService service = getService(mDevice, srvcUuid.getUuid(),
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800271 srvcInstId, srvcType);
272 if (service == null) return;
273
274 BluetoothGattCharacteristic characteristic = service.getCharacteristic(
275 charUuid.getUuid());
276 if (characteristic == null) return;
277
278 characteristic.addDescriptor(new BluetoothGattDescriptor(
Andre Eisenbach25b9cf92013-07-08 23:58:16 -0700279 characteristic, descUuid.getUuid(), descrInstId, 0));
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800280 }
281
282 /**
283 * Remote search has been completed.
284 * The internal object structure should now reflect the state
285 * of the remote device database. Let the application know that
286 * we are done at this point.
287 * @hide
288 */
289 public void onSearchComplete(String address, int status) {
290 if (DBG) Log.d(TAG, "onSearchComplete() = Device=" + address + " Status=" + status);
Matthew Xieddf7e472013-03-01 18:41:02 -0800291 if (!address.equals(mDevice.getAddress())) {
292 return;
293 }
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800294 try {
Matthew Xie33ec9842013-04-03 00:29:27 -0700295 mCallback.onServicesDiscovered(BluetoothGatt.this, status);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800296 } catch (Exception ex) {
Mike Lockwood0998ff12013-05-13 14:04:12 -0700297 Log.w(TAG, "Unhandled exception in callback", ex);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800298 }
299 }
300
301 /**
302 * Remote characteristic has been read.
303 * Updates the internal value.
304 * @hide
305 */
306 public void onCharacteristicRead(String address, int status, int srvcType,
307 int srvcInstId, ParcelUuid srvcUuid,
308 int charInstId, ParcelUuid charUuid, byte[] value) {
309 if (DBG) Log.d(TAG, "onCharacteristicRead() - Device=" + address
310 + " UUID=" + charUuid + " Status=" + status);
311
Matthew Xieddf7e472013-03-01 18:41:02 -0800312 if (!address.equals(mDevice.getAddress())) {
313 return;
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 }
362 BluetoothGattService service = getService(mDevice, srvcUuid.getUuid(),
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800363 srvcInstId, srvcType);
364 if (service == null) return;
365
366 BluetoothGattCharacteristic characteristic = service.getCharacteristic(
367 charUuid.getUuid(), charInstId);
368 if (characteristic == null) return;
369
370 if ((status == GATT_INSUFFICIENT_AUTHENTICATION
371 || status == GATT_INSUFFICIENT_ENCRYPTION)
372 && mAuthRetry == false) {
373 try {
374 mAuthRetry = true;
375 mService.writeCharacteristic(mClientIf, address,
376 srvcType, srvcInstId, srvcUuid, charInstId, charUuid,
377 characteristic.getWriteType(), AUTHENTICATION_MITM,
378 characteristic.getValue());
379 return;
380 } catch (RemoteException e) {
381 Log.e(TAG,"",e);
382 }
383 }
384
385 mAuthRetry = false;
386
387 try {
Matthew Xie33ec9842013-04-03 00:29:27 -0700388 mCallback.onCharacteristicWrite(BluetoothGatt.this, characteristic, status);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800389 } catch (Exception ex) {
Mike Lockwood0998ff12013-05-13 14:04:12 -0700390 Log.w(TAG, "Unhandled exception in callback", ex);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800391 }
392 }
393
394 /**
395 * Remote characteristic has been updated.
396 * Updates the internal value.
397 * @hide
398 */
399 public void onNotify(String address, int srvcType,
400 int srvcInstId, ParcelUuid srvcUuid,
401 int charInstId, ParcelUuid charUuid,
402 byte[] value) {
403 if (DBG) Log.d(TAG, "onNotify() - Device=" + address + " UUID=" + charUuid);
404
Matthew Xieddf7e472013-03-01 18:41:02 -0800405 if (!address.equals(mDevice.getAddress())) {
406 return;
407 }
408 BluetoothGattService service = getService(mDevice, srvcUuid.getUuid(),
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800409 srvcInstId, srvcType);
410 if (service == null) return;
411
412 BluetoothGattCharacteristic characteristic = service.getCharacteristic(
413 charUuid.getUuid(), charInstId);
414 if (characteristic == null) return;
415
416 characteristic.setValue(value);
417
418 try {
Matthew Xie33ec9842013-04-03 00:29:27 -0700419 mCallback.onCharacteristicChanged(BluetoothGatt.this, characteristic);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800420 } catch (Exception ex) {
Mike Lockwood0998ff12013-05-13 14:04:12 -0700421 Log.w(TAG, "Unhandled exception in callback", ex);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800422 }
423 }
424
425 /**
426 * Descriptor has been read.
427 * @hide
428 */
429 public void onDescriptorRead(String address, int status, int srvcType,
430 int srvcInstId, ParcelUuid srvcUuid,
431 int charInstId, ParcelUuid charUuid,
Andre Eisenbach25b9cf92013-07-08 23:58:16 -0700432 int descrInstId, ParcelUuid descrUuid,
433 byte[] value) {
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800434 if (DBG) Log.d(TAG, "onDescriptorRead() - Device=" + address + " UUID=" + charUuid);
435
Matthew Xieddf7e472013-03-01 18:41:02 -0800436 if (!address.equals(mDevice.getAddress())) {
437 return;
438 }
439 BluetoothGattService service = getService(mDevice, srvcUuid.getUuid(),
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800440 srvcInstId, srvcType);
441 if (service == null) return;
442
443 BluetoothGattCharacteristic characteristic = service.getCharacteristic(
444 charUuid.getUuid(), charInstId);
445 if (characteristic == null) return;
446
447 BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
Andre Eisenbach25b9cf92013-07-08 23:58:16 -0700448 descrUuid.getUuid(), descrInstId);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800449 if (descriptor == null) return;
450
451 if (status == 0) descriptor.setValue(value);
452
453 if ((status == GATT_INSUFFICIENT_AUTHENTICATION
454 || status == GATT_INSUFFICIENT_ENCRYPTION)
455 && mAuthRetry == false) {
456 try {
457 mAuthRetry = true;
458 mService.readDescriptor(mClientIf, address,
459 srvcType, srvcInstId, srvcUuid, charInstId, charUuid,
Andre Eisenbach25b9cf92013-07-08 23:58:16 -0700460 descrInstId, descrUuid, AUTHENTICATION_MITM);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800461 } catch (RemoteException e) {
462 Log.e(TAG,"",e);
463 }
464 }
465
466 mAuthRetry = true;
467
468 try {
Matthew Xie33ec9842013-04-03 00:29:27 -0700469 mCallback.onDescriptorRead(BluetoothGatt.this, descriptor, status);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800470 } catch (Exception ex) {
Mike Lockwood0998ff12013-05-13 14:04:12 -0700471 Log.w(TAG, "Unhandled exception in callback", ex);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800472 }
473 }
474
475 /**
476 * Descriptor write operation complete.
477 * @hide
478 */
479 public void onDescriptorWrite(String address, int status, int srvcType,
480 int srvcInstId, ParcelUuid srvcUuid,
481 int charInstId, ParcelUuid charUuid,
Andre Eisenbach25b9cf92013-07-08 23:58:16 -0700482 int descrInstId, ParcelUuid descrUuid) {
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800483 if (DBG) Log.d(TAG, "onDescriptorWrite() - Device=" + address + " UUID=" + charUuid);
484
Matthew Xieddf7e472013-03-01 18:41:02 -0800485 if (!address.equals(mDevice.getAddress())) {
486 return;
487 }
488 BluetoothGattService service = getService(mDevice, srvcUuid.getUuid(),
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800489 srvcInstId, srvcType);
490 if (service == null) return;
491
492 BluetoothGattCharacteristic characteristic = service.getCharacteristic(
493 charUuid.getUuid(), charInstId);
494 if (characteristic == null) return;
495
496 BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
Andre Eisenbach25b9cf92013-07-08 23:58:16 -0700497 descrUuid.getUuid(), descrInstId);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800498 if (descriptor == null) return;
499
500 if ((status == GATT_INSUFFICIENT_AUTHENTICATION
501 || status == GATT_INSUFFICIENT_ENCRYPTION)
502 && mAuthRetry == false) {
503 try {
504 mAuthRetry = true;
505 mService.writeDescriptor(mClientIf, address,
506 srvcType, srvcInstId, srvcUuid, charInstId, charUuid,
Andre Eisenbach25b9cf92013-07-08 23:58:16 -0700507 descrInstId, descrUuid, characteristic.getWriteType(),
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800508 AUTHENTICATION_MITM, descriptor.getValue());
509 } catch (RemoteException e) {
510 Log.e(TAG,"",e);
511 }
512 }
513
514 mAuthRetry = false;
515
516 try {
Matthew Xie33ec9842013-04-03 00:29:27 -0700517 mCallback.onDescriptorWrite(BluetoothGatt.this, descriptor, status);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800518 } catch (Exception ex) {
Mike Lockwood0998ff12013-05-13 14:04:12 -0700519 Log.w(TAG, "Unhandled exception in callback", ex);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800520 }
521 }
522
523 /**
524 * Prepared write transaction completed (or aborted)
525 * @hide
526 */
527 public void onExecuteWrite(String address, int status) {
528 if (DBG) Log.d(TAG, "onExecuteWrite() - Device=" + address
529 + " status=" + status);
Matthew Xieddf7e472013-03-01 18:41:02 -0800530 if (!address.equals(mDevice.getAddress())) {
531 return;
532 }
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800533 try {
Matthew Xie33ec9842013-04-03 00:29:27 -0700534 mCallback.onReliableWriteCompleted(BluetoothGatt.this, status);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800535 } catch (Exception ex) {
Mike Lockwood0998ff12013-05-13 14:04:12 -0700536 Log.w(TAG, "Unhandled exception in callback", ex);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800537 }
538 }
539
540 /**
541 * Remote device RSSI has been read
542 * @hide
543 */
544 public void onReadRemoteRssi(String address, int rssi, int status) {
545 if (DBG) Log.d(TAG, "onReadRemoteRssi() - Device=" + address +
546 " rssi=" + rssi + " status=" + status);
Matthew Xieddf7e472013-03-01 18:41:02 -0800547 if (!address.equals(mDevice.getAddress())) {
548 return;
549 }
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800550 try {
Matthew Xie33ec9842013-04-03 00:29:27 -0700551 mCallback.onReadRemoteRssi(BluetoothGatt.this, rssi, status);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800552 } catch (Exception ex) {
Mike Lockwood0998ff12013-05-13 14:04:12 -0700553 Log.w(TAG, "Unhandled exception in callback", ex);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800554 }
555 }
Wei Wang4b4eebb2014-03-11 22:22:41 -0700556
557 /**
558 * Advertise state change callback
559 * @hide
560 */
561 public void onAdvertiseStateChange(int state, int status) {
562 if (DBG) Log.d(TAG, "onAdvertiseStateChange() - state = "
563 + state + " status=" + status);
Andre Eisenbach580b0a12014-03-25 06:31:50 -0700564 }
565
566 /**
567 * Callback invoked when the MTU for a given connection changes
568 * @hide
569 */
570 public void onConfigureMTU(String address, int mtu, int status) {
571 if (DBG) Log.d(TAG, "onConfigureMTU() - Device=" + address +
572 " mtu=" + mtu + " status=" + status);
573 if (!address.equals(mDevice.getAddress())) {
574 return;
575 }
576 try {
577 mCallback.onConfigureMTU(BluetoothGatt.this, mtu, status);
578 } catch (Exception ex) {
579 Log.w(TAG, "Unhandled exception in callback", ex);
580 }
Wei Wang4b4eebb2014-03-11 22:22:41 -0700581 }
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800582 };
583
Matthew Xieddf7e472013-03-01 18:41:02 -0800584 /*package*/ BluetoothGatt(Context context, IBluetoothGatt iGatt, BluetoothDevice device) {
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800585 mContext = context;
Matthew Xieddf7e472013-03-01 18:41:02 -0800586 mService = iGatt;
587 mDevice = device;
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800588 mServices = new ArrayList<BluetoothGattService>();
589
Matthew Xieddf7e472013-03-01 18:41:02 -0800590 mConnState = CONN_STATE_IDLE;
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800591 }
592
593 /**
Matthew Xie33ec9842013-04-03 00:29:27 -0700594 * Close this Bluetooth GATT client.
Matthew Xieb30f91e2013-05-29 10:19:06 -0700595 *
596 * Application should call this method as early as possible after it is done with
597 * this GATT client.
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800598 */
Matthew Xie33ec9842013-04-03 00:29:27 -0700599 public void close() {
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800600 if (DBG) Log.d(TAG, "close()");
601
602 unregisterApp();
Matthew Xie33ec9842013-04-03 00:29:27 -0700603 mConnState = CONN_STATE_CLOSED;
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800604 }
605
606 /**
607 * Returns a service by UUID, instance and type.
608 * @hide
609 */
610 /*package*/ BluetoothGattService getService(BluetoothDevice device, UUID uuid,
611 int instanceId, int type) {
612 for(BluetoothGattService svc : mServices) {
613 if (svc.getDevice().equals(device) &&
614 svc.getType() == type &&
615 svc.getInstanceId() == instanceId &&
616 svc.getUuid().equals(uuid)) {
617 return svc;
618 }
619 }
620 return null;
621 }
622
623
624 /**
Matthew Xieddf7e472013-03-01 18:41:02 -0800625 * Register an application callback to start using GATT.
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800626 *
Matthew Xieddf7e472013-03-01 18:41:02 -0800627 * <p>This is an asynchronous call. The callback {@link BluetoothGattCallback#onAppRegistered}
628 * is used to notify success or failure if the function returns true.
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800629 *
630 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
631 *
Matthew Xieddf7e472013-03-01 18:41:02 -0800632 * @param callback GATT callback handler that will receive asynchronous callbacks.
633 * @return If true, the callback will be called to notify success or failure,
634 * false on immediate error
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800635 */
Matthew Xieddf7e472013-03-01 18:41:02 -0800636 private boolean registerApp(BluetoothGattCallback callback) {
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800637 if (DBG) Log.d(TAG, "registerApp()");
638 if (mService == null) return false;
639
640 mCallback = callback;
641 UUID uuid = UUID.randomUUID();
642 if (DBG) Log.d(TAG, "registerApp() - UUID=" + uuid);
643
644 try {
645 mService.registerClient(new ParcelUuid(uuid), mBluetoothGattCallback);
646 } catch (RemoteException e) {
647 Log.e(TAG,"",e);
648 return false;
649 }
650
651 return true;
652 }
653
654 /**
655 * Unregister the current application and callbacks.
656 */
Matthew Xieddf7e472013-03-01 18:41:02 -0800657 private void unregisterApp() {
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800658 if (DBG) Log.d(TAG, "unregisterApp() - mClientIf=" + mClientIf);
659 if (mService == null || mClientIf == 0) return;
660
661 try {
662 mCallback = null;
663 mService.unregisterClient(mClientIf);
664 mClientIf = 0;
665 } catch (RemoteException e) {
666 Log.e(TAG,"",e);
667 }
668 }
669
670 /**
Matthew Xieddf7e472013-03-01 18:41:02 -0800671 * Initiate a connection to a Bluetooth GATT capable device.
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800672 *
673 * <p>The connection may not be established right away, but will be
674 * completed when the remote device is available. A
675 * {@link BluetoothGattCallback#onConnectionStateChange} callback will be
676 * invoked when the connection state changes as a result of this function.
677 *
678 * <p>The autoConnect paramter determines whether to actively connect to
679 * the remote device, or rather passively scan and finalize the connection
680 * when the remote device is in range/available. Generally, the first ever
681 * connection to a device should be direct (autoConnect set to false) and
682 * subsequent connections to known devices should be invoked with the
Matthew Xieddf7e472013-03-01 18:41:02 -0800683 * autoConnect parameter set to true.
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800684 *
685 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
686 *
687 * @param device Remote device to connect to
688 * @param autoConnect Whether to directly connect to the remote device (false)
689 * or to automatically connect as soon as the remote
690 * device becomes available (true).
691 * @return true, if the connection attempt was initiated successfully
692 */
Matthew Xieddf7e472013-03-01 18:41:02 -0800693 /*package*/ boolean connect(Boolean autoConnect, BluetoothGattCallback callback) {
694 if (DBG) Log.d(TAG, "connect() - device: " + mDevice.getAddress() + ", auto: " + autoConnect);
695 synchronized(mStateLock) {
696 if (mConnState != CONN_STATE_IDLE) {
697 throw new IllegalStateException("Not idle");
698 }
699 mConnState = CONN_STATE_CONNECTING;
700 }
701 if (!registerApp(callback)) {
702 synchronized(mStateLock) {
703 mConnState = CONN_STATE_IDLE;
704 }
705 Log.e(TAG, "Failed to register callback");
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800706 return false;
707 }
708
Matthew Xieddf7e472013-03-01 18:41:02 -0800709 // the connection will continue after successful callback registration
710 mAutoConnect = autoConnect;
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800711 return true;
712 }
713
714 /**
715 * Disconnects an established connection, or cancels a connection attempt
716 * currently in progress.
717 *
718 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800719 */
Matthew Xieddf7e472013-03-01 18:41:02 -0800720 public void disconnect() {
721 if (DBG) Log.d(TAG, "cancelOpen() - device: " + mDevice.getAddress());
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800722 if (mService == null || mClientIf == 0) return;
723
724 try {
Matthew Xieddf7e472013-03-01 18:41:02 -0800725 mService.clientDisconnect(mClientIf, mDevice.getAddress());
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800726 } catch (RemoteException e) {
727 Log.e(TAG,"",e);
728 }
Matthew Xie33ec9842013-04-03 00:29:27 -0700729 }
730
731 /**
732 * Connect back to remote device.
733 *
734 * <p>This method is used to re-connect to a remote device after the
735 * connection has been dropped. If the device is not in range, the
736 * re-connection will be triggered once the device is back in range.
737 *
738 * @return true, if the connection attempt was initiated successfully
739 */
740 public boolean connect() {
741 try {
742 mService.clientConnect(mClientIf, mDevice.getAddress(),
743 false); // autoConnect is inverse of "isDirect"
744 return true;
745 } catch (RemoteException e) {
746 Log.e(TAG,"",e);
747 return false;
748 }
749 }
750
751 /**
752 * Return the remote bluetooth device this GATT client targets to
753 *
754 * @return remote bluetooth device
755 */
756 public BluetoothDevice getDevice() {
757 return mDevice;
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800758 }
759
760 /**
761 * Discovers services offered by a remote device as well as their
762 * characteristics and descriptors.
763 *
764 * <p>This is an asynchronous operation. Once service discovery is completed,
765 * the {@link BluetoothGattCallback#onServicesDiscovered} callback is
766 * triggered. If the discovery was successful, the remote services can be
767 * retrieved using the {@link #getServices} function.
768 *
769 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
770 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800771 * @return true, if the remote service discovery has been started
772 */
Matthew Xieddf7e472013-03-01 18:41:02 -0800773 public boolean discoverServices() {
774 if (DBG) Log.d(TAG, "discoverServices() - device: " + mDevice.getAddress());
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800775 if (mService == null || mClientIf == 0) return false;
776
777 mServices.clear();
778
779 try {
Matthew Xieddf7e472013-03-01 18:41:02 -0800780 mService.discoverServices(mClientIf, mDevice.getAddress());
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800781 } catch (RemoteException e) {
782 Log.e(TAG,"",e);
783 return false;
784 }
785
786 return true;
787 }
788
789 /**
790 * Returns a list of GATT services offered by the remote device.
791 *
792 * <p>This function requires that service discovery has been completed
793 * for the given device.
794 *
795 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
796 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800797 * @return List of services on the remote device. Returns an empty list
798 * if service discovery has not yet been performed.
799 */
Matthew Xieddf7e472013-03-01 18:41:02 -0800800 public List<BluetoothGattService> getServices() {
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800801 List<BluetoothGattService> result =
802 new ArrayList<BluetoothGattService>();
803
804 for (BluetoothGattService service : mServices) {
Matthew Xieddf7e472013-03-01 18:41:02 -0800805 if (service.getDevice().equals(mDevice)) {
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800806 result.add(service);
807 }
808 }
809
810 return result;
811 }
812
813 /**
814 * Returns a {@link BluetoothGattService}, if the requested UUID is
815 * supported by the remote device.
816 *
817 * <p>This function requires that service discovery has been completed
818 * for the given device.
819 *
820 * <p>If multiple instances of the same service (as identified by UUID)
821 * exist, the first instance of the service is returned.
822 *
823 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
824 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800825 * @param uuid UUID of the requested service
826 * @return BluetoothGattService if supported, or null if the requested
827 * service is not offered by the remote device.
828 */
Matthew Xieddf7e472013-03-01 18:41:02 -0800829 public BluetoothGattService getService(UUID uuid) {
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800830 for (BluetoothGattService service : mServices) {
Matthew Xieddf7e472013-03-01 18:41:02 -0800831 if (service.getDevice().equals(mDevice) &&
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800832 service.getUuid().equals(uuid)) {
833 return service;
834 }
835 }
836
837 return null;
838 }
839
840 /**
841 * Reads the requested characteristic from the associated remote device.
842 *
843 * <p>This is an asynchronous operation. The result of the read operation
844 * is reported by the {@link BluetoothGattCallback#onCharacteristicRead}
845 * callback.
846 *
847 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
848 *
849 * @param characteristic Characteristic to read from the remote device
850 * @return true, if the read operation was initiated successfully
851 */
852 public boolean readCharacteristic(BluetoothGattCharacteristic characteristic) {
853 if ((characteristic.getProperties() &
854 BluetoothGattCharacteristic.PROPERTY_READ) == 0) return false;
855
856 if (DBG) Log.d(TAG, "readCharacteristic() - uuid: " + characteristic.getUuid());
857 if (mService == null || mClientIf == 0) return false;
858
859 BluetoothGattService service = characteristic.getService();
860 if (service == null) return false;
861
862 BluetoothDevice device = service.getDevice();
863 if (device == null) return false;
864
865 try {
866 mService.readCharacteristic(mClientIf, device.getAddress(),
867 service.getType(), service.getInstanceId(),
868 new ParcelUuid(service.getUuid()), characteristic.getInstanceId(),
869 new ParcelUuid(characteristic.getUuid()), AUTHENTICATION_NONE);
870 } catch (RemoteException e) {
871 Log.e(TAG,"",e);
872 return false;
873 }
874
875 return true;
876 }
877
878 /**
Matthew Xieddf7e472013-03-01 18:41:02 -0800879 * Writes a given characteristic and its values to the associated remote device.
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800880 *
881 * <p>Once the write operation has been completed, the
882 * {@link BluetoothGattCallback#onCharacteristicWrite} callback is invoked,
883 * reporting the result of the operation.
884 *
885 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
886 *
887 * @param characteristic Characteristic to write on the remote device
888 * @return true, if the write operation was initiated successfully
889 */
890 public boolean writeCharacteristic(BluetoothGattCharacteristic characteristic) {
891 if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE) == 0
892 && (characteristic.getProperties() &
893 BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) == 0) return false;
894
895 if (DBG) Log.d(TAG, "writeCharacteristic() - uuid: " + characteristic.getUuid());
896 if (mService == null || mClientIf == 0) return false;
897
898 BluetoothGattService service = characteristic.getService();
899 if (service == null) return false;
900
901 BluetoothDevice device = service.getDevice();
902 if (device == null) return false;
903
904 try {
905 mService.writeCharacteristic(mClientIf, device.getAddress(),
906 service.getType(), service.getInstanceId(),
907 new ParcelUuid(service.getUuid()), characteristic.getInstanceId(),
908 new ParcelUuid(characteristic.getUuid()),
909 characteristic.getWriteType(), AUTHENTICATION_NONE,
910 characteristic.getValue());
911 } catch (RemoteException e) {
912 Log.e(TAG,"",e);
913 return false;
914 }
915
916 return true;
917 }
918
919 /**
920 * Reads the value for a given descriptor from the associated remote device.
921 *
922 * <p>Once the read operation has been completed, the
923 * {@link BluetoothGattCallback#onDescriptorRead} callback is
924 * triggered, signaling the result of the operation.
925 *
926 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
927 *
928 * @param descriptor Descriptor value to read from the remote device
929 * @return true, if the read operation was initiated successfully
930 */
931 public boolean readDescriptor(BluetoothGattDescriptor descriptor) {
932 if (DBG) Log.d(TAG, "readDescriptor() - uuid: " + descriptor.getUuid());
933 if (mService == null || mClientIf == 0) return false;
934
935 BluetoothGattCharacteristic characteristic = descriptor.getCharacteristic();
936 if (characteristic == null) return false;
937
938 BluetoothGattService service = characteristic.getService();
939 if (service == null) return false;
940
941 BluetoothDevice device = service.getDevice();
942 if (device == null) return false;
943
944 try {
Andre Eisenbach25b9cf92013-07-08 23:58:16 -0700945 mService.readDescriptor(mClientIf, device.getAddress(), service.getType(),
946 service.getInstanceId(), new ParcelUuid(service.getUuid()),
947 characteristic.getInstanceId(), new ParcelUuid(characteristic.getUuid()),
948 descriptor.getInstanceId(), new ParcelUuid(descriptor.getUuid()),
949 AUTHENTICATION_NONE);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800950 } catch (RemoteException e) {
951 Log.e(TAG,"",e);
952 return false;
953 }
954
955 return true;
956 }
957
958 /**
959 * Write the value of a given descriptor to the associated remote device.
960 *
961 * <p>A {@link BluetoothGattCallback#onDescriptorWrite} callback is
962 * triggered to report the result of the write operation.
963 *
964 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
965 *
966 * @param descriptor Descriptor to write to the associated remote device
967 * @return true, if the write operation was initiated successfully
968 */
969 public boolean writeDescriptor(BluetoothGattDescriptor descriptor) {
970 if (DBG) Log.d(TAG, "writeDescriptor() - uuid: " + descriptor.getUuid());
971 if (mService == null || mClientIf == 0) return false;
972
973 BluetoothGattCharacteristic characteristic = descriptor.getCharacteristic();
974 if (characteristic == null) return false;
975
976 BluetoothGattService service = characteristic.getService();
977 if (service == null) return false;
978
979 BluetoothDevice device = service.getDevice();
980 if (device == null) return false;
981
982 try {
Andre Eisenbach25b9cf92013-07-08 23:58:16 -0700983 mService.writeDescriptor(mClientIf, device.getAddress(), service.getType(),
984 service.getInstanceId(), new ParcelUuid(service.getUuid()),
985 characteristic.getInstanceId(), new ParcelUuid(characteristic.getUuid()),
986 descriptor.getInstanceId(), new ParcelUuid(descriptor.getUuid()),
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -0800987 characteristic.getWriteType(), AUTHENTICATION_NONE,
988 descriptor.getValue());
989 } catch (RemoteException e) {
990 Log.e(TAG,"",e);
991 return false;
992 }
993
994 return true;
995 }
996
997 /**
998 * Initiates a reliable write transaction for a given remote device.
999 *
1000 * <p>Once a reliable write transaction has been initiated, all calls
1001 * to {@link #writeCharacteristic} are sent to the remote device for
1002 * verification and queued up for atomic execution. The application will
1003 * receive an {@link BluetoothGattCallback#onCharacteristicWrite} callback
1004 * in response to every {@link #writeCharacteristic} call and is responsible
1005 * for verifying if the value has been transmitted accurately.
1006 *
1007 * <p>After all characteristics have been queued up and verified,
1008 * {@link #executeReliableWrite} will execute all writes. If a characteristic
1009 * was not written correctly, calling {@link #abortReliableWrite} will
1010 * cancel the current transaction without commiting any values on the
1011 * remote device.
1012 *
1013 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
1014 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001015 * @return true, if the reliable write transaction has been initiated
1016 */
Matthew Xieddf7e472013-03-01 18:41:02 -08001017 public boolean beginReliableWrite() {
1018 if (DBG) Log.d(TAG, "beginReliableWrite() - device: " + mDevice.getAddress());
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001019 if (mService == null || mClientIf == 0) return false;
1020
1021 try {
Matthew Xieddf7e472013-03-01 18:41:02 -08001022 mService.beginReliableWrite(mClientIf, mDevice.getAddress());
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001023 } catch (RemoteException e) {
1024 Log.e(TAG,"",e);
1025 return false;
1026 }
1027
1028 return true;
1029 }
1030
1031 /**
1032 * Executes a reliable write transaction for a given remote device.
1033 *
1034 * <p>This function will commit all queued up characteristic write
1035 * operations for a given remote device.
1036 *
1037 * <p>A {@link BluetoothGattCallback#onReliableWriteCompleted} callback is
1038 * invoked to indicate whether the transaction has been executed correctly.
1039 *
1040 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
1041 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001042 * @return true, if the request to execute the transaction has been sent
1043 */
Matthew Xieddf7e472013-03-01 18:41:02 -08001044 public boolean executeReliableWrite() {
1045 if (DBG) Log.d(TAG, "executeReliableWrite() - device: " + mDevice.getAddress());
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001046 if (mService == null || mClientIf == 0) return false;
1047
1048 try {
Matthew Xieddf7e472013-03-01 18:41:02 -08001049 mService.endReliableWrite(mClientIf, mDevice.getAddress(), true);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001050 } catch (RemoteException e) {
1051 Log.e(TAG,"",e);
1052 return false;
1053 }
1054
1055 return true;
1056 }
1057
1058 /**
1059 * Cancels a reliable write transaction for a given device.
1060 *
1061 * <p>Calling this function will discard all queued characteristic write
1062 * operations for a given remote device.
1063 *
1064 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001065 */
John Du48f8b5d2013-08-19 12:20:37 -07001066 public void abortReliableWrite() {
Matthew Xieddf7e472013-03-01 18:41:02 -08001067 if (DBG) Log.d(TAG, "abortReliableWrite() - device: " + mDevice.getAddress());
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001068 if (mService == null || mClientIf == 0) return;
1069
1070 try {
Matthew Xieddf7e472013-03-01 18:41:02 -08001071 mService.endReliableWrite(mClientIf, mDevice.getAddress(), false);
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001072 } catch (RemoteException e) {
1073 Log.e(TAG,"",e);
1074 }
1075 }
1076
1077 /**
John Dub7b7d7a2013-08-20 14:03:28 -07001078 * @deprecated Use {@link #abortReliableWrite()}
John Du48f8b5d2013-08-19 12:20:37 -07001079 */
1080 public void abortReliableWrite(BluetoothDevice mDevice) {
1081 abortReliableWrite();
1082 }
1083
1084 /**
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001085 * Enable or disable notifications/indications for a given characteristic.
1086 *
1087 * <p>Once notifications are enabled for a characteristic, a
1088 * {@link BluetoothGattCallback#onCharacteristicChanged} callback will be
1089 * triggered if the remote device indicates that the given characteristic
1090 * has changed.
1091 *
1092 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
1093 *
1094 * @param characteristic The characteristic for which to enable notifications
1095 * @param enable Set to true to enable notifications/indications
1096 * @return true, if the requested notification status was set successfully
1097 */
1098 public boolean setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
1099 boolean enable) {
1100 if (DBG) Log.d(TAG, "setCharacteristicNotification() - uuid: " + characteristic.getUuid()
1101 + " enable: " + enable);
1102 if (mService == null || mClientIf == 0) return false;
1103
1104 BluetoothGattService service = characteristic.getService();
1105 if (service == null) return false;
1106
1107 BluetoothDevice device = service.getDevice();
1108 if (device == null) return false;
1109
1110 try {
1111 mService.registerForNotification(mClientIf, device.getAddress(),
1112 service.getType(), service.getInstanceId(),
1113 new ParcelUuid(service.getUuid()), characteristic.getInstanceId(),
1114 new ParcelUuid(characteristic.getUuid()),
1115 enable);
1116 } catch (RemoteException e) {
1117 Log.e(TAG,"",e);
1118 return false;
1119 }
1120
1121 return true;
1122 }
1123
1124 /**
1125 * Clears the internal cache and forces a refresh of the services from the
1126 * remote device.
1127 * @hide
1128 */
Matthew Xieddf7e472013-03-01 18:41:02 -08001129 public boolean refresh() {
1130 if (DBG) Log.d(TAG, "refresh() - device: " + mDevice.getAddress());
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001131 if (mService == null || mClientIf == 0) return false;
1132
1133 try {
Matthew Xieddf7e472013-03-01 18:41:02 -08001134 mService.refreshDevice(mClientIf, mDevice.getAddress());
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001135 } catch (RemoteException e) {
1136 Log.e(TAG,"",e);
1137 return false;
1138 }
1139
1140 return true;
1141 }
1142
1143 /**
1144 * Read the RSSI for a connected remote device.
1145 *
1146 * <p>The {@link BluetoothGattCallback#onReadRemoteRssi} callback will be
1147 * invoked when the RSSI value has been read.
1148 *
1149 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
1150 *
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001151 * @return true, if the RSSI value has been requested successfully
1152 */
Matthew Xieddf7e472013-03-01 18:41:02 -08001153 public boolean readRemoteRssi() {
1154 if (DBG) Log.d(TAG, "readRssi() - device: " + mDevice.getAddress());
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001155 if (mService == null || mClientIf == 0) return false;
1156
1157 try {
Matthew Xieddf7e472013-03-01 18:41:02 -08001158 mService.readRemoteRssi(mClientIf, mDevice.getAddress());
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001159 } catch (RemoteException e) {
1160 Log.e(TAG,"",e);
1161 return false;
1162 }
1163
1164 return true;
1165 }
1166
1167 /**
Andre Eisenbach580b0a12014-03-25 06:31:50 -07001168 * Configure the MTU used for a given connection.
1169 *
1170 * <p>When performing a write request operation (write without response),
1171 * the data sent is truncated to the MTU size. This function may be used
1172 * to request a larget MTU size to be able to send more data at once.
1173 *
1174 * <p>A {@link BluetoothGattCallback#onConfigureMTU} callback will indicate
1175 * whether this operation was successful.
1176 *
1177 * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission.
1178 *
1179 * @return true, if the new MTU value has been requested successfully
1180 * @hide
1181 */
1182 public boolean configureMTU(int mtu) {
1183 if (DBG) Log.d(TAG, "configureMTU() - device: " + mDevice.getAddress()
1184 + " mtu: " + mtu);
1185 if (mService == null || mClientIf == 0) return false;
1186
1187 try {
1188 mService.configureMTU(mClientIf, mDevice.getAddress(), mtu);
1189 } catch (RemoteException e) {
1190 Log.e(TAG,"",e);
1191 return false;
1192 }
1193
1194 return true;
1195 }
1196
1197 /**
Matthew Xieddf7e472013-03-01 18:41:02 -08001198 * Not supported - please use {@link BluetoothManager#getConnectedDevices(int)}
1199 * with {@link BluetoothProfile#GATT} as argument
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001200 *
Matthew Xieddf7e472013-03-01 18:41:02 -08001201 * @throws UnsupportedOperationException
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001202 */
1203 @Override
1204 public int getConnectionState(BluetoothDevice device) {
Matthew Xieddf7e472013-03-01 18:41:02 -08001205 throw new UnsupportedOperationException("Use BluetoothManager#getConnectionState instead.");
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001206 }
1207
1208 /**
Matthew Xieddf7e472013-03-01 18:41:02 -08001209 * Not supported - please use {@link BluetoothManager#getConnectedDevices(int)}
1210 * with {@link BluetoothProfile#GATT} as argument
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001211 *
Matthew Xieddf7e472013-03-01 18:41:02 -08001212 * @throws UnsupportedOperationException
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001213 */
1214 @Override
1215 public List<BluetoothDevice> getConnectedDevices() {
Matthew Xieddf7e472013-03-01 18:41:02 -08001216 throw new UnsupportedOperationException
1217 ("Use BluetoothManager#getConnectedDevices instead.");
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001218 }
1219
1220 /**
Matthew Xieddf7e472013-03-01 18:41:02 -08001221 * Not supported - please use
1222 * {@link BluetoothManager#getDevicesMatchingConnectionStates(int, int[])}
1223 * with {@link BluetoothProfile#GATT} as first argument
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001224 *
Matthew Xieddf7e472013-03-01 18:41:02 -08001225 * @throws UnsupportedOperationException
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001226 */
1227 @Override
1228 public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) {
Matthew Xieddf7e472013-03-01 18:41:02 -08001229 throw new UnsupportedOperationException
1230 ("Use BluetoothManager#getDevicesMatchingConnectionStates instead.");
Ganesh Ganapathi Batta99081122013-02-05 15:28:33 -08001231 }
1232}