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 | 8d312a8 | 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 | 8d312a8 | 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 | */ |
Jakub Pawlowski | a9e27ec | 2016-03-17 16:00:16 -0700 | [diff] [blame^] | 249 | public void onCharacteristicRead(String address, int status, int handle, byte[] value) { |
Andre Eisenbach | 55d19e4 | 2014-07-18 14:38:36 -0700 | [diff] [blame] | 250 | if (VDBG) Log.d(TAG, "onCharacteristicRead() - Device=" + address |
Jakub Pawlowski | a9e27ec | 2016-03-17 16:00:16 -0700 | [diff] [blame^] | 251 | + " handle=" + handle + " Status=" + status); |
| 252 | |
| 253 | Log.w(TAG, "onCharacteristicRead() - Device=" + address |
| 254 | + " handle=" + handle + " Status=" + status); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 255 | |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 256 | if (!address.equals(mDevice.getAddress())) { |
| 257 | return; |
| 258 | } |
Andre Eisenbach | cc68cc9 | 2014-03-18 14:26:51 -0700 | [diff] [blame] | 259 | |
| 260 | synchronized(mDeviceBusy) { |
| 261 | mDeviceBusy = false; |
| 262 | } |
| 263 | |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 264 | if ((status == GATT_INSUFFICIENT_AUTHENTICATION |
| 265 | || status == GATT_INSUFFICIENT_ENCRYPTION) |
| 266 | && mAuthRetry == false) { |
| 267 | try { |
| 268 | mAuthRetry = true; |
Jakub Pawlowski | a9e27ec | 2016-03-17 16:00:16 -0700 | [diff] [blame^] | 269 | mService.readCharacteristic(mClientIf, address, handle, AUTHENTICATION_MITM); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 270 | return; |
| 271 | } catch (RemoteException e) { |
| 272 | Log.e(TAG,"",e); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | mAuthRetry = false; |
| 277 | |
Jakub Pawlowski | a9e27ec | 2016-03-17 16:00:16 -0700 | [diff] [blame^] | 278 | BluetoothGattCharacteristic characteristic = getCharacteristicById(mDevice, handle); |
| 279 | if (characteristic == null) { |
| 280 | Log.w(TAG, "onCharacteristicRead() failed to find characteristic!"); |
| 281 | return; |
| 282 | } |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 283 | |
| 284 | if (status == 0) characteristic.setValue(value); |
| 285 | |
| 286 | try { |
Matthew Xie | 33ec984 | 2013-04-03 00:29:27 -0700 | [diff] [blame] | 287 | mCallback.onCharacteristicRead(BluetoothGatt.this, characteristic, status); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 288 | } catch (Exception ex) { |
Mike Lockwood | 0998ff1 | 2013-05-13 14:04:12 -0700 | [diff] [blame] | 289 | Log.w(TAG, "Unhandled exception in callback", ex); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Characteristic has been written to the remote device. |
| 295 | * Let the app know how we did... |
| 296 | * @hide |
| 297 | */ |
Jakub Pawlowski | a9e27ec | 2016-03-17 16:00:16 -0700 | [diff] [blame^] | 298 | public void onCharacteristicWrite(String address, int status, int handle) { |
Andre Eisenbach | 55d19e4 | 2014-07-18 14:38:36 -0700 | [diff] [blame] | 299 | if (VDBG) Log.d(TAG, "onCharacteristicWrite() - Device=" + address |
Jakub Pawlowski | a9e27ec | 2016-03-17 16:00:16 -0700 | [diff] [blame^] | 300 | + " handle=" + handle + " Status=" + status); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 301 | |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 302 | if (!address.equals(mDevice.getAddress())) { |
| 303 | return; |
| 304 | } |
Andre Eisenbach | cc68cc9 | 2014-03-18 14:26:51 -0700 | [diff] [blame] | 305 | |
| 306 | synchronized(mDeviceBusy) { |
| 307 | mDeviceBusy = false; |
| 308 | } |
| 309 | |
Jakub Pawlowski | a9e27ec | 2016-03-17 16:00:16 -0700 | [diff] [blame^] | 310 | BluetoothGattCharacteristic characteristic = getCharacteristicById(mDevice, handle); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 311 | if (characteristic == null) return; |
| 312 | |
| 313 | if ((status == GATT_INSUFFICIENT_AUTHENTICATION |
| 314 | || status == GATT_INSUFFICIENT_ENCRYPTION) |
| 315 | && mAuthRetry == false) { |
| 316 | try { |
| 317 | mAuthRetry = true; |
Jakub Pawlowski | a9e27ec | 2016-03-17 16:00:16 -0700 | [diff] [blame^] | 318 | mService.writeCharacteristic(mClientIf, address, handle, |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 319 | characteristic.getWriteType(), AUTHENTICATION_MITM, |
| 320 | characteristic.getValue()); |
| 321 | return; |
| 322 | } catch (RemoteException e) { |
| 323 | Log.e(TAG,"",e); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | mAuthRetry = false; |
| 328 | |
| 329 | try { |
Matthew Xie | 33ec984 | 2013-04-03 00:29:27 -0700 | [diff] [blame] | 330 | mCallback.onCharacteristicWrite(BluetoothGatt.this, characteristic, status); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 331 | } catch (Exception ex) { |
Mike Lockwood | 0998ff1 | 2013-05-13 14:04:12 -0700 | [diff] [blame] | 332 | Log.w(TAG, "Unhandled exception in callback", ex); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * Remote characteristic has been updated. |
| 338 | * Updates the internal value. |
| 339 | * @hide |
| 340 | */ |
Jakub Pawlowski | a9e27ec | 2016-03-17 16:00:16 -0700 | [diff] [blame^] | 341 | public void onNotify(String address, int handle, byte[] value) { |
| 342 | if (VDBG) Log.d(TAG, "onNotify() - Device=" + address + " handle=" + handle); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 343 | |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 344 | if (!address.equals(mDevice.getAddress())) { |
| 345 | return; |
| 346 | } |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 347 | |
Jakub Pawlowski | a9e27ec | 2016-03-17 16:00:16 -0700 | [diff] [blame^] | 348 | BluetoothGattCharacteristic characteristic = getCharacteristicById(mDevice, handle); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 349 | if (characteristic == null) return; |
| 350 | |
| 351 | characteristic.setValue(value); |
| 352 | |
| 353 | try { |
Matthew Xie | 33ec984 | 2013-04-03 00:29:27 -0700 | [diff] [blame] | 354 | mCallback.onCharacteristicChanged(BluetoothGatt.this, characteristic); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 355 | } catch (Exception ex) { |
Mike Lockwood | 0998ff1 | 2013-05-13 14:04:12 -0700 | [diff] [blame] | 356 | Log.w(TAG, "Unhandled exception in callback", ex); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
| 360 | /** |
| 361 | * Descriptor has been read. |
| 362 | * @hide |
| 363 | */ |
Jakub Pawlowski | a9e27ec | 2016-03-17 16:00:16 -0700 | [diff] [blame^] | 364 | public void onDescriptorRead(String address, int status, int handle, byte[] value) { |
| 365 | if (VDBG) Log.d(TAG, "onDescriptorRead() - Device=" + address + " handle=" + handle); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 366 | |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 367 | if (!address.equals(mDevice.getAddress())) { |
| 368 | return; |
| 369 | } |
Andre Eisenbach | cc68cc9 | 2014-03-18 14:26:51 -0700 | [diff] [blame] | 370 | |
| 371 | synchronized(mDeviceBusy) { |
| 372 | mDeviceBusy = false; |
| 373 | } |
| 374 | |
Jakub Pawlowski | a9e27ec | 2016-03-17 16:00:16 -0700 | [diff] [blame^] | 375 | BluetoothGattDescriptor descriptor = getDescriptorById(mDevice, handle); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 376 | if (descriptor == null) return; |
| 377 | |
| 378 | if (status == 0) descriptor.setValue(value); |
| 379 | |
| 380 | if ((status == GATT_INSUFFICIENT_AUTHENTICATION |
| 381 | || status == GATT_INSUFFICIENT_ENCRYPTION) |
| 382 | && mAuthRetry == false) { |
| 383 | try { |
| 384 | mAuthRetry = true; |
Jakub Pawlowski | a9e27ec | 2016-03-17 16:00:16 -0700 | [diff] [blame^] | 385 | mService.readDescriptor(mClientIf, address, handle, AUTHENTICATION_MITM); |
Andre Eisenbach | d65e8f4 | 2014-07-25 15:16:11 -0700 | [diff] [blame] | 386 | return; |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 387 | } catch (RemoteException e) { |
| 388 | Log.e(TAG,"",e); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | mAuthRetry = true; |
| 393 | |
| 394 | try { |
Matthew Xie | 33ec984 | 2013-04-03 00:29:27 -0700 | [diff] [blame] | 395 | mCallback.onDescriptorRead(BluetoothGatt.this, descriptor, status); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 396 | } catch (Exception ex) { |
Mike Lockwood | 0998ff1 | 2013-05-13 14:04:12 -0700 | [diff] [blame] | 397 | Log.w(TAG, "Unhandled exception in callback", ex); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * Descriptor write operation complete. |
| 403 | * @hide |
| 404 | */ |
Jakub Pawlowski | a9e27ec | 2016-03-17 16:00:16 -0700 | [diff] [blame^] | 405 | public void onDescriptorWrite(String address, int status, int handle) { |
| 406 | if (VDBG) Log.d(TAG, "onDescriptorWrite() - Device=" + address + " handle=" + handle); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 407 | |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 408 | if (!address.equals(mDevice.getAddress())) { |
| 409 | return; |
| 410 | } |
Andre Eisenbach | cc68cc9 | 2014-03-18 14:26:51 -0700 | [diff] [blame] | 411 | |
| 412 | synchronized(mDeviceBusy) { |
| 413 | mDeviceBusy = false; |
| 414 | } |
| 415 | |
Jakub Pawlowski | a9e27ec | 2016-03-17 16:00:16 -0700 | [diff] [blame^] | 416 | BluetoothGattDescriptor descriptor = getDescriptorById(mDevice, handle); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 417 | if (descriptor == null) return; |
| 418 | |
| 419 | if ((status == GATT_INSUFFICIENT_AUTHENTICATION |
| 420 | || status == GATT_INSUFFICIENT_ENCRYPTION) |
| 421 | && mAuthRetry == false) { |
| 422 | try { |
| 423 | mAuthRetry = true; |
Jakub Pawlowski | a9e27ec | 2016-03-17 16:00:16 -0700 | [diff] [blame^] | 424 | mService.writeDescriptor(mClientIf, address, handle, |
| 425 | descriptor.getCharacteristic().getWriteType(), |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 426 | AUTHENTICATION_MITM, descriptor.getValue()); |
Andre Eisenbach | d65e8f4 | 2014-07-25 15:16:11 -0700 | [diff] [blame] | 427 | return; |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 428 | } catch (RemoteException e) { |
| 429 | Log.e(TAG,"",e); |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | mAuthRetry = false; |
| 434 | |
| 435 | try { |
Matthew Xie | 33ec984 | 2013-04-03 00:29:27 -0700 | [diff] [blame] | 436 | mCallback.onDescriptorWrite(BluetoothGatt.this, descriptor, status); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 437 | } catch (Exception ex) { |
Mike Lockwood | 0998ff1 | 2013-05-13 14:04:12 -0700 | [diff] [blame] | 438 | Log.w(TAG, "Unhandled exception in callback", ex); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 439 | } |
| 440 | } |
| 441 | |
| 442 | /** |
| 443 | * Prepared write transaction completed (or aborted) |
| 444 | * @hide |
| 445 | */ |
| 446 | public void onExecuteWrite(String address, int status) { |
Andre Eisenbach | 55d19e4 | 2014-07-18 14:38:36 -0700 | [diff] [blame] | 447 | if (VDBG) Log.d(TAG, "onExecuteWrite() - Device=" + address |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 448 | + " status=" + status); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 449 | if (!address.equals(mDevice.getAddress())) { |
| 450 | return; |
| 451 | } |
Andre Eisenbach | cc68cc9 | 2014-03-18 14:26:51 -0700 | [diff] [blame] | 452 | |
| 453 | synchronized(mDeviceBusy) { |
| 454 | mDeviceBusy = false; |
| 455 | } |
| 456 | |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 457 | try { |
Matthew Xie | 33ec984 | 2013-04-03 00:29:27 -0700 | [diff] [blame] | 458 | mCallback.onReliableWriteCompleted(BluetoothGatt.this, status); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 459 | } catch (Exception ex) { |
Mike Lockwood | 0998ff1 | 2013-05-13 14:04:12 -0700 | [diff] [blame] | 460 | Log.w(TAG, "Unhandled exception in callback", ex); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | |
| 464 | /** |
| 465 | * Remote device RSSI has been read |
| 466 | * @hide |
| 467 | */ |
| 468 | public void onReadRemoteRssi(String address, int rssi, int status) { |
Andre Eisenbach | 55d19e4 | 2014-07-18 14:38:36 -0700 | [diff] [blame] | 469 | if (VDBG) Log.d(TAG, "onReadRemoteRssi() - Device=" + address + |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 470 | " rssi=" + rssi + " status=" + status); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 471 | if (!address.equals(mDevice.getAddress())) { |
| 472 | return; |
| 473 | } |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 474 | try { |
Matthew Xie | 33ec984 | 2013-04-03 00:29:27 -0700 | [diff] [blame] | 475 | mCallback.onReadRemoteRssi(BluetoothGatt.this, rssi, status); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 476 | } catch (Exception ex) { |
Mike Lockwood | 0998ff1 | 2013-05-13 14:04:12 -0700 | [diff] [blame] | 477 | Log.w(TAG, "Unhandled exception in callback", ex); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 478 | } |
| 479 | } |
Wei Wang | f305589 | 2014-03-11 22:22:41 -0700 | [diff] [blame] | 480 | |
| 481 | /** |
Andre Eisenbach | 580b0a1 | 2014-03-25 06:31:50 -0700 | [diff] [blame] | 482 | * Callback invoked when the MTU for a given connection changes |
| 483 | * @hide |
| 484 | */ |
| 485 | public void onConfigureMTU(String address, int mtu, int status) { |
| 486 | if (DBG) Log.d(TAG, "onConfigureMTU() - Device=" + address + |
| 487 | " mtu=" + mtu + " status=" + status); |
| 488 | if (!address.equals(mDevice.getAddress())) { |
| 489 | return; |
| 490 | } |
| 491 | try { |
Andre Eisenbach | 4072da0 | 2014-08-19 17:58:55 -0700 | [diff] [blame] | 492 | mCallback.onMtuChanged(BluetoothGatt.this, mtu, status); |
Andre Eisenbach | 580b0a1 | 2014-03-25 06:31:50 -0700 | [diff] [blame] | 493 | } catch (Exception ex) { |
| 494 | Log.w(TAG, "Unhandled exception in callback", ex); |
| 495 | } |
Wei Wang | f305589 | 2014-03-11 22:22:41 -0700 | [diff] [blame] | 496 | } |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 497 | }; |
| 498 | |
Ganesh Ganapathi Batta | b88fa82 | 2014-04-18 10:00:40 -0700 | [diff] [blame] | 499 | /*package*/ BluetoothGatt(Context context, IBluetoothGatt iGatt, BluetoothDevice device, |
| 500 | int transport) { |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 501 | mContext = context; |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 502 | mService = iGatt; |
| 503 | mDevice = device; |
Ganesh Ganapathi Batta | b88fa82 | 2014-04-18 10:00:40 -0700 | [diff] [blame] | 504 | mTransport = transport; |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 505 | mServices = new ArrayList<BluetoothGattService>(); |
| 506 | |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 507 | mConnState = CONN_STATE_IDLE; |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | /** |
Matthew Xie | 33ec984 | 2013-04-03 00:29:27 -0700 | [diff] [blame] | 511 | * Close this Bluetooth GATT client. |
Matthew Xie | b30f91e | 2013-05-29 10:19:06 -0700 | [diff] [blame] | 512 | * |
| 513 | * Application should call this method as early as possible after it is done with |
| 514 | * this GATT client. |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 515 | */ |
Matthew Xie | 33ec984 | 2013-04-03 00:29:27 -0700 | [diff] [blame] | 516 | public void close() { |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 517 | if (DBG) Log.d(TAG, "close()"); |
| 518 | |
| 519 | unregisterApp(); |
Matthew Xie | 33ec984 | 2013-04-03 00:29:27 -0700 | [diff] [blame] | 520 | mConnState = CONN_STATE_CLOSED; |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | /** |
| 524 | * Returns a service by UUID, instance and type. |
| 525 | * @hide |
| 526 | */ |
| 527 | /*package*/ BluetoothGattService getService(BluetoothDevice device, UUID uuid, |
| 528 | int instanceId, int type) { |
| 529 | for(BluetoothGattService svc : mServices) { |
| 530 | if (svc.getDevice().equals(device) && |
| 531 | svc.getType() == type && |
| 532 | svc.getInstanceId() == instanceId && |
| 533 | svc.getUuid().equals(uuid)) { |
| 534 | return svc; |
| 535 | } |
| 536 | } |
| 537 | return null; |
| 538 | } |
| 539 | |
| 540 | |
| 541 | /** |
Jakub Pawlowski | a9e27ec | 2016-03-17 16:00:16 -0700 | [diff] [blame^] | 542 | * Returns a characteristic with id equal to instanceId. |
| 543 | * @hide |
| 544 | */ |
| 545 | /*package*/ BluetoothGattCharacteristic getCharacteristicById(BluetoothDevice device, int instanceId) { |
| 546 | for(BluetoothGattService svc : mServices) { |
| 547 | for(BluetoothGattCharacteristic charac : svc.getCharacteristics()) { |
| 548 | Log.w(TAG, "getCharacteristicById() comparing " + charac.getInstanceId() + " and " + instanceId); |
| 549 | if (charac.getInstanceId() == instanceId) |
| 550 | return charac; |
| 551 | } |
| 552 | } |
| 553 | return null; |
| 554 | } |
| 555 | |
| 556 | /** |
| 557 | * Returns a descriptor with id equal to instanceId. |
| 558 | * @hide |
| 559 | */ |
| 560 | /*package*/ BluetoothGattDescriptor getDescriptorById(BluetoothDevice device, int instanceId) { |
| 561 | for(BluetoothGattService svc : mServices) { |
| 562 | for(BluetoothGattCharacteristic charac : svc.getCharacteristics()) { |
| 563 | for(BluetoothGattDescriptor desc : charac.getDescriptors()) { |
| 564 | if (desc.getInstanceId() == instanceId) |
| 565 | return desc; |
| 566 | } |
| 567 | } |
| 568 | } |
| 569 | return null; |
| 570 | } |
| 571 | |
| 572 | /** |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 573 | * Register an application callback to start using GATT. |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 574 | * |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 575 | * <p>This is an asynchronous call. The callback {@link BluetoothGattCallback#onAppRegistered} |
| 576 | * 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] | 577 | * |
| 578 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 579 | * |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 580 | * @param callback GATT callback handler that will receive asynchronous callbacks. |
| 581 | * @return If true, the callback will be called to notify success or failure, |
| 582 | * false on immediate error |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 583 | */ |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 584 | private boolean registerApp(BluetoothGattCallback callback) { |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 585 | if (DBG) Log.d(TAG, "registerApp()"); |
| 586 | if (mService == null) return false; |
| 587 | |
| 588 | mCallback = callback; |
| 589 | UUID uuid = UUID.randomUUID(); |
| 590 | if (DBG) Log.d(TAG, "registerApp() - UUID=" + uuid); |
| 591 | |
| 592 | try { |
| 593 | mService.registerClient(new ParcelUuid(uuid), mBluetoothGattCallback); |
| 594 | } catch (RemoteException e) { |
| 595 | Log.e(TAG,"",e); |
| 596 | return false; |
| 597 | } |
| 598 | |
| 599 | return true; |
| 600 | } |
| 601 | |
| 602 | /** |
| 603 | * Unregister the current application and callbacks. |
| 604 | */ |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 605 | private void unregisterApp() { |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 606 | if (DBG) Log.d(TAG, "unregisterApp() - mClientIf=" + mClientIf); |
| 607 | if (mService == null || mClientIf == 0) return; |
| 608 | |
| 609 | try { |
| 610 | mCallback = null; |
| 611 | mService.unregisterClient(mClientIf); |
| 612 | mClientIf = 0; |
| 613 | } catch (RemoteException e) { |
| 614 | Log.e(TAG,"",e); |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | /** |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 619 | * Initiate a connection to a Bluetooth GATT capable device. |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 620 | * |
| 621 | * <p>The connection may not be established right away, but will be |
| 622 | * completed when the remote device is available. A |
| 623 | * {@link BluetoothGattCallback#onConnectionStateChange} callback will be |
| 624 | * invoked when the connection state changes as a result of this function. |
| 625 | * |
Andre Eisenbach | 6ce4db0 | 2014-07-16 23:02:42 -0700 | [diff] [blame] | 626 | * <p>The autoConnect parameter determines whether to actively connect to |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 627 | * the remote device, or rather passively scan and finalize the connection |
| 628 | * when the remote device is in range/available. Generally, the first ever |
| 629 | * connection to a device should be direct (autoConnect set to false) and |
| 630 | * subsequent connections to known devices should be invoked with the |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 631 | * autoConnect parameter set to true. |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 632 | * |
| 633 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 634 | * |
| 635 | * @param device Remote device to connect to |
| 636 | * @param autoConnect Whether to directly connect to the remote device (false) |
| 637 | * or to automatically connect as soon as the remote |
| 638 | * device becomes available (true). |
| 639 | * @return true, if the connection attempt was initiated successfully |
| 640 | */ |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 641 | /*package*/ boolean connect(Boolean autoConnect, BluetoothGattCallback callback) { |
| 642 | if (DBG) Log.d(TAG, "connect() - device: " + mDevice.getAddress() + ", auto: " + autoConnect); |
| 643 | synchronized(mStateLock) { |
| 644 | if (mConnState != CONN_STATE_IDLE) { |
| 645 | throw new IllegalStateException("Not idle"); |
| 646 | } |
| 647 | mConnState = CONN_STATE_CONNECTING; |
| 648 | } |
| 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 | |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 657 | // the connection will continue after successful callback registration |
| 658 | mAutoConnect = autoConnect; |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 659 | return true; |
| 660 | } |
| 661 | |
| 662 | /** |
| 663 | * Disconnects an established connection, or cancels a connection attempt |
| 664 | * currently in progress. |
| 665 | * |
| 666 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 667 | */ |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 668 | public void disconnect() { |
| 669 | if (DBG) Log.d(TAG, "cancelOpen() - device: " + mDevice.getAddress()); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 670 | if (mService == null || mClientIf == 0) return; |
| 671 | |
| 672 | try { |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 673 | mService.clientDisconnect(mClientIf, mDevice.getAddress()); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 674 | } catch (RemoteException e) { |
| 675 | Log.e(TAG,"",e); |
| 676 | } |
Matthew Xie | 33ec984 | 2013-04-03 00:29:27 -0700 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | /** |
| 680 | * Connect back to remote device. |
| 681 | * |
| 682 | * <p>This method is used to re-connect to a remote device after the |
| 683 | * connection has been dropped. If the device is not in range, the |
| 684 | * re-connection will be triggered once the device is back in range. |
| 685 | * |
| 686 | * @return true, if the connection attempt was initiated successfully |
| 687 | */ |
| 688 | public boolean connect() { |
| 689 | try { |
| 690 | mService.clientConnect(mClientIf, mDevice.getAddress(), |
Ganesh Ganapathi Batta | b88fa82 | 2014-04-18 10:00:40 -0700 | [diff] [blame] | 691 | false, mTransport); // autoConnect is inverse of "isDirect" |
Matthew Xie | 33ec984 | 2013-04-03 00:29:27 -0700 | [diff] [blame] | 692 | return true; |
| 693 | } catch (RemoteException e) { |
| 694 | Log.e(TAG,"",e); |
| 695 | return false; |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | /** |
| 700 | * Return the remote bluetooth device this GATT client targets to |
| 701 | * |
| 702 | * @return remote bluetooth device |
| 703 | */ |
| 704 | public BluetoothDevice getDevice() { |
| 705 | return mDevice; |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | /** |
| 709 | * Discovers services offered by a remote device as well as their |
| 710 | * characteristics and descriptors. |
| 711 | * |
| 712 | * <p>This is an asynchronous operation. Once service discovery is completed, |
| 713 | * the {@link BluetoothGattCallback#onServicesDiscovered} callback is |
| 714 | * triggered. If the discovery was successful, the remote services can be |
| 715 | * retrieved using the {@link #getServices} function. |
| 716 | * |
| 717 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 718 | * |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 719 | * @return true, if the remote service discovery has been started |
| 720 | */ |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 721 | public boolean discoverServices() { |
| 722 | if (DBG) Log.d(TAG, "discoverServices() - device: " + mDevice.getAddress()); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 723 | if (mService == null || mClientIf == 0) return false; |
| 724 | |
| 725 | mServices.clear(); |
| 726 | |
| 727 | try { |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 728 | mService.discoverServices(mClientIf, mDevice.getAddress()); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 729 | } catch (RemoteException e) { |
| 730 | Log.e(TAG,"",e); |
| 731 | return false; |
| 732 | } |
| 733 | |
| 734 | return true; |
| 735 | } |
| 736 | |
| 737 | /** |
| 738 | * Returns a list of GATT services offered by the remote device. |
| 739 | * |
| 740 | * <p>This function requires that service discovery has been completed |
| 741 | * for the given device. |
| 742 | * |
| 743 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 744 | * |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 745 | * @return List of services on the remote device. Returns an empty list |
| 746 | * if service discovery has not yet been performed. |
| 747 | */ |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 748 | public List<BluetoothGattService> getServices() { |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 749 | List<BluetoothGattService> result = |
| 750 | new ArrayList<BluetoothGattService>(); |
| 751 | |
| 752 | for (BluetoothGattService service : mServices) { |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 753 | if (service.getDevice().equals(mDevice)) { |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 754 | result.add(service); |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | return result; |
| 759 | } |
| 760 | |
| 761 | /** |
| 762 | * Returns a {@link BluetoothGattService}, if the requested UUID is |
| 763 | * supported by the remote device. |
| 764 | * |
| 765 | * <p>This function requires that service discovery has been completed |
| 766 | * for the given device. |
| 767 | * |
| 768 | * <p>If multiple instances of the same service (as identified by UUID) |
| 769 | * exist, the first instance of the service is returned. |
| 770 | * |
| 771 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 772 | * |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 773 | * @param uuid UUID of the requested service |
| 774 | * @return BluetoothGattService if supported, or null if the requested |
| 775 | * service is not offered by the remote device. |
| 776 | */ |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 777 | public BluetoothGattService getService(UUID uuid) { |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 778 | for (BluetoothGattService service : mServices) { |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 779 | if (service.getDevice().equals(mDevice) && |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 780 | service.getUuid().equals(uuid)) { |
| 781 | return service; |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | return null; |
| 786 | } |
| 787 | |
| 788 | /** |
| 789 | * Reads the requested characteristic from the associated remote device. |
| 790 | * |
| 791 | * <p>This is an asynchronous operation. The result of the read operation |
| 792 | * is reported by the {@link BluetoothGattCallback#onCharacteristicRead} |
| 793 | * callback. |
| 794 | * |
| 795 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 796 | * |
| 797 | * @param characteristic Characteristic to read from the remote device |
| 798 | * @return true, if the read operation was initiated successfully |
| 799 | */ |
| 800 | public boolean readCharacteristic(BluetoothGattCharacteristic characteristic) { |
| 801 | if ((characteristic.getProperties() & |
| 802 | BluetoothGattCharacteristic.PROPERTY_READ) == 0) return false; |
| 803 | |
Andre Eisenbach | 55d19e4 | 2014-07-18 14:38:36 -0700 | [diff] [blame] | 804 | if (VDBG) Log.d(TAG, "readCharacteristic() - uuid: " + characteristic.getUuid()); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 805 | if (mService == null || mClientIf == 0) return false; |
| 806 | |
| 807 | BluetoothGattService service = characteristic.getService(); |
| 808 | if (service == null) return false; |
| 809 | |
| 810 | BluetoothDevice device = service.getDevice(); |
| 811 | if (device == null) return false; |
| 812 | |
Andre Eisenbach | cc68cc9 | 2014-03-18 14:26:51 -0700 | [diff] [blame] | 813 | synchronized(mDeviceBusy) { |
| 814 | if (mDeviceBusy) return false; |
| 815 | mDeviceBusy = true; |
| 816 | } |
| 817 | |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 818 | try { |
| 819 | mService.readCharacteristic(mClientIf, device.getAddress(), |
Jakub Pawlowski | a9e27ec | 2016-03-17 16:00:16 -0700 | [diff] [blame^] | 820 | characteristic.getInstanceId(), AUTHENTICATION_NONE); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 821 | } catch (RemoteException e) { |
| 822 | Log.e(TAG,"",e); |
Andre Eisenbach | cc68cc9 | 2014-03-18 14:26:51 -0700 | [diff] [blame] | 823 | mDeviceBusy = false; |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 824 | return false; |
| 825 | } |
| 826 | |
| 827 | return true; |
| 828 | } |
| 829 | |
| 830 | /** |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 831 | * 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] | 832 | * |
| 833 | * <p>Once the write operation has been completed, the |
| 834 | * {@link BluetoothGattCallback#onCharacteristicWrite} callback is invoked, |
| 835 | * reporting the result of the operation. |
| 836 | * |
| 837 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 838 | * |
| 839 | * @param characteristic Characteristic to write on the remote device |
| 840 | * @return true, if the write operation was initiated successfully |
| 841 | */ |
| 842 | public boolean writeCharacteristic(BluetoothGattCharacteristic characteristic) { |
| 843 | if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE) == 0 |
| 844 | && (characteristic.getProperties() & |
| 845 | BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) == 0) return false; |
| 846 | |
Andre Eisenbach | 55d19e4 | 2014-07-18 14:38:36 -0700 | [diff] [blame] | 847 | if (VDBG) Log.d(TAG, "writeCharacteristic() - uuid: " + characteristic.getUuid()); |
Prerepa Viswanadham | ff5e5db3 | 2014-12-04 10:12:55 -0800 | [diff] [blame] | 848 | if (mService == null || mClientIf == 0 || characteristic.getValue() == null) return false; |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 849 | |
| 850 | BluetoothGattService service = characteristic.getService(); |
| 851 | if (service == null) return false; |
| 852 | |
| 853 | BluetoothDevice device = service.getDevice(); |
| 854 | if (device == null) return false; |
| 855 | |
Andre Eisenbach | cc68cc9 | 2014-03-18 14:26:51 -0700 | [diff] [blame] | 856 | synchronized(mDeviceBusy) { |
| 857 | if (mDeviceBusy) return false; |
| 858 | mDeviceBusy = true; |
| 859 | } |
| 860 | |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 861 | try { |
| 862 | mService.writeCharacteristic(mClientIf, device.getAddress(), |
Jakub Pawlowski | a9e27ec | 2016-03-17 16:00:16 -0700 | [diff] [blame^] | 863 | characteristic.getInstanceId(), characteristic.getWriteType(), |
| 864 | AUTHENTICATION_NONE, characteristic.getValue()); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 865 | } catch (RemoteException e) { |
| 866 | Log.e(TAG,"",e); |
Andre Eisenbach | cc68cc9 | 2014-03-18 14:26:51 -0700 | [diff] [blame] | 867 | mDeviceBusy = false; |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 868 | return false; |
| 869 | } |
| 870 | |
| 871 | return true; |
| 872 | } |
| 873 | |
| 874 | /** |
| 875 | * Reads the value for a given descriptor from the associated remote device. |
| 876 | * |
| 877 | * <p>Once the read operation has been completed, the |
| 878 | * {@link BluetoothGattCallback#onDescriptorRead} callback is |
| 879 | * triggered, signaling the result of the operation. |
| 880 | * |
| 881 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 882 | * |
| 883 | * @param descriptor Descriptor value to read from the remote device |
| 884 | * @return true, if the read operation was initiated successfully |
| 885 | */ |
| 886 | public boolean readDescriptor(BluetoothGattDescriptor descriptor) { |
Andre Eisenbach | 55d19e4 | 2014-07-18 14:38:36 -0700 | [diff] [blame] | 887 | if (VDBG) Log.d(TAG, "readDescriptor() - uuid: " + descriptor.getUuid()); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 888 | if (mService == null || mClientIf == 0) return false; |
| 889 | |
| 890 | BluetoothGattCharacteristic characteristic = descriptor.getCharacteristic(); |
| 891 | if (characteristic == null) return false; |
| 892 | |
| 893 | BluetoothGattService service = characteristic.getService(); |
| 894 | if (service == null) return false; |
| 895 | |
| 896 | BluetoothDevice device = service.getDevice(); |
| 897 | if (device == null) return false; |
| 898 | |
Andre Eisenbach | cc68cc9 | 2014-03-18 14:26:51 -0700 | [diff] [blame] | 899 | synchronized(mDeviceBusy) { |
| 900 | if (mDeviceBusy) return false; |
| 901 | mDeviceBusy = true; |
| 902 | } |
| 903 | |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 904 | try { |
Jakub Pawlowski | a9e27ec | 2016-03-17 16:00:16 -0700 | [diff] [blame^] | 905 | mService.readDescriptor(mClientIf, device.getAddress(), |
| 906 | descriptor.getInstanceId(), AUTHENTICATION_NONE); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 907 | } catch (RemoteException e) { |
| 908 | Log.e(TAG,"",e); |
Andre Eisenbach | cc68cc9 | 2014-03-18 14:26:51 -0700 | [diff] [blame] | 909 | mDeviceBusy = false; |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 910 | return false; |
| 911 | } |
| 912 | |
| 913 | return true; |
| 914 | } |
| 915 | |
| 916 | /** |
| 917 | * Write the value of a given descriptor to the associated remote device. |
| 918 | * |
| 919 | * <p>A {@link BluetoothGattCallback#onDescriptorWrite} callback is |
| 920 | * triggered to report the result of the write operation. |
| 921 | * |
| 922 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 923 | * |
| 924 | * @param descriptor Descriptor to write to the associated remote device |
| 925 | * @return true, if the write operation was initiated successfully |
| 926 | */ |
| 927 | public boolean writeDescriptor(BluetoothGattDescriptor descriptor) { |
Andre Eisenbach | 55d19e4 | 2014-07-18 14:38:36 -0700 | [diff] [blame] | 928 | if (VDBG) Log.d(TAG, "writeDescriptor() - uuid: " + descriptor.getUuid()); |
Prerepa Viswanadham | ff5e5db3 | 2014-12-04 10:12:55 -0800 | [diff] [blame] | 929 | if (mService == null || mClientIf == 0 || descriptor.getValue() == null) return false; |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 930 | |
| 931 | BluetoothGattCharacteristic characteristic = descriptor.getCharacteristic(); |
| 932 | if (characteristic == null) return false; |
| 933 | |
| 934 | BluetoothGattService service = characteristic.getService(); |
| 935 | if (service == null) return false; |
| 936 | |
| 937 | BluetoothDevice device = service.getDevice(); |
| 938 | if (device == null) return false; |
| 939 | |
Andre Eisenbach | cc68cc9 | 2014-03-18 14:26:51 -0700 | [diff] [blame] | 940 | synchronized(mDeviceBusy) { |
| 941 | if (mDeviceBusy) return false; |
| 942 | mDeviceBusy = true; |
| 943 | } |
| 944 | |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 945 | try { |
Jakub Pawlowski | a9e27ec | 2016-03-17 16:00:16 -0700 | [diff] [blame^] | 946 | mService.writeDescriptor(mClientIf, device.getAddress(), descriptor.getInstanceId(), |
| 947 | characteristic.getWriteType(), AUTHENTICATION_NONE, 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 | a9e27ec | 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 | } |