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