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