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