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