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