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