| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.bluetooth; |
| 18 | |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 19 | import android.os.ParcelUuid; |
| 20 | import android.os.RemoteException; |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 21 | import android.util.Log; |
| 22 | |
| 23 | import java.util.ArrayList; |
| 24 | import java.util.List; |
| 25 | import java.util.UUID; |
| 26 | |
| 27 | /** |
| Matthew Xie | 9afa274 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 28 | * Public API for the Bluetooth GATT Profile server role. |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 29 | * |
| Matthew Xie | 9afa274 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 30 | * <p>This class provides Bluetooth GATT server role functionality, |
| Andre Eisenbach | 25eb545 | 2014-06-27 14:31:37 -0700 | [diff] [blame] | 31 | * allowing applications to create Bluetooth Smart services and |
| 32 | * characteristics. |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 33 | * |
| 34 | * <p>BluetoothGattServer is a proxy object for controlling the Bluetooth Service |
| Andre Eisenbach | 25eb545 | 2014-06-27 14:31:37 -0700 | [diff] [blame] | 35 | * via IPC. Use {@link BluetoothManager#openGattServer} to get an instance |
| 36 | * of this class. |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 37 | */ |
| 38 | public final class BluetoothGattServer implements BluetoothProfile { |
| 39 | private static final String TAG = "BluetoothGattServer"; |
| 40 | private static final boolean DBG = true; |
| Andre Eisenbach | 10020cd | 2014-07-18 14:38:36 -0700 | [diff] [blame] | 41 | private static final boolean VDBG = false; |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 42 | |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 43 | private BluetoothAdapter mAdapter; |
| 44 | private IBluetoothGatt mService; |
| Jakub Pawlowski | cd8835e | 2017-03-22 11:22:18 -0700 | [diff] [blame] | 45 | private BluetoothGattServerCallback mCallback; |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 46 | |
| Matthew Xie | 9afa274 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 47 | private Object mServerIfLock = new Object(); |
| 48 | private int mServerIf; |
| Ganesh Ganapathi Batta | ec66191 | 2014-04-18 10:00:40 -0700 | [diff] [blame] | 49 | private int mTransport; |
| Jakub Pawlowski | 8d7adf2 | 2016-04-01 07:51:45 -0700 | [diff] [blame] | 50 | private BluetoothGattService mPendingService; |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 51 | private List<BluetoothGattService> mServices; |
| 52 | |
| Matthew Xie | 9afa274 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 53 | private static final int CALLBACK_REG_TIMEOUT = 10000; |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * Bluetooth GATT interface callbacks |
| 57 | */ |
| Jakub Pawlowski | ddf66d3 | 2017-03-27 12:14:40 -0700 | [diff] [blame] | 58 | private final IBluetoothGattServerCallback mBluetoothGattServerCallback = |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 59 | new IBluetoothGattServerCallback.Stub() { |
| 60 | /** |
| 61 | * Application interface registered - app is ready to go |
| 62 | * @hide |
| 63 | */ |
| 64 | @Override |
| 65 | public void onServerRegistered(int status, int serverIf) { |
| 66 | if (DBG) { |
| 67 | Log.d(TAG, "onServerRegistered() - status=" + status |
| 68 | + " serverIf=" + serverIf); |
| Matthew Xie | 9afa274 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 69 | } |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 70 | synchronized (mServerIfLock) { |
| 71 | if (mCallback != null) { |
| 72 | mServerIf = serverIf; |
| 73 | mServerIfLock.notify(); |
| 74 | } else { |
| 75 | // registration timeout |
| 76 | Log.e(TAG, "onServerRegistered: mCallback is null"); |
| 77 | } |
| Jakub Pawlowski | 8d7adf2 | 2016-04-01 07:51:45 -0700 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 81 | /** |
| 82 | * Server connection state changed |
| 83 | * @hide |
| 84 | */ |
| 85 | @Override |
| 86 | public void onServerConnectionState(int status, int serverIf, |
| 87 | boolean connected, String address) { |
| 88 | if (DBG) { |
| 89 | Log.d(TAG, "onServerConnectionState() - status=" + status |
| 90 | + " serverIf=" + serverIf + " device=" + address); |
| 91 | } |
| 92 | try { |
| 93 | mCallback.onConnectionStateChange(mAdapter.getRemoteDevice(address), status, |
| 94 | connected ? BluetoothProfile.STATE_CONNECTED : |
| 95 | BluetoothProfile.STATE_DISCONNECTED); |
| 96 | } catch (Exception ex) { |
| 97 | Log.w(TAG, "Unhandled exception in callback", ex); |
| 98 | } |
| Jakub Pawlowski | 8d7adf2 | 2016-04-01 07:51:45 -0700 | [diff] [blame] | 99 | } |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 100 | |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 101 | /** |
| 102 | * Service has been added |
| 103 | * @hide |
| 104 | */ |
| 105 | @Override |
| 106 | public void onServiceAdded(int status, BluetoothGattService service) { |
| 107 | if (DBG) { |
| 108 | Log.d(TAG, "onServiceAdded() - handle=" + service.getInstanceId() |
| 109 | + " uuid=" + service.getUuid() + " status=" + status); |
| 110 | } |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 111 | |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 112 | if (mPendingService == null) { |
| 113 | return; |
| 114 | } |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 115 | |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 116 | BluetoothGattService tmp = mPendingService; |
| 117 | mPendingService = null; |
| 118 | |
| 119 | // Rewrite newly assigned handles to existing service. |
| 120 | tmp.setInstanceId(service.getInstanceId()); |
| 121 | List<BluetoothGattCharacteristic> temp_chars = tmp.getCharacteristics(); |
| 122 | List<BluetoothGattCharacteristic> svc_chars = service.getCharacteristics(); |
| 123 | for (int i = 0; i < svc_chars.size(); i++) { |
| 124 | BluetoothGattCharacteristic temp_char = temp_chars.get(i); |
| 125 | BluetoothGattCharacteristic svc_char = svc_chars.get(i); |
| 126 | |
| 127 | temp_char.setInstanceId(svc_char.getInstanceId()); |
| 128 | |
| 129 | List<BluetoothGattDescriptor> temp_descs = temp_char.getDescriptors(); |
| 130 | List<BluetoothGattDescriptor> svc_descs = svc_char.getDescriptors(); |
| 131 | for (int j = 0; j < svc_descs.size(); j++) { |
| 132 | temp_descs.get(j).setInstanceId(svc_descs.get(j).getInstanceId()); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | mServices.add(tmp); |
| 137 | |
| 138 | try { |
| 139 | mCallback.onServiceAdded((int) status, tmp); |
| 140 | } catch (Exception ex) { |
| 141 | Log.w(TAG, "Unhandled exception in callback", ex); |
| 142 | } |
| Jakub Pawlowski | 8d7adf2 | 2016-04-01 07:51:45 -0700 | [diff] [blame] | 143 | } |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 144 | |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 145 | /** |
| 146 | * Remote client characteristic read request. |
| 147 | * @hide |
| 148 | */ |
| 149 | @Override |
| 150 | public void onCharacteristicReadRequest(String address, int transId, |
| 151 | int offset, boolean isLong, int handle) { |
| 152 | if (VDBG) Log.d(TAG, "onCharacteristicReadRequest() - handle=" + handle); |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 153 | |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 154 | BluetoothDevice device = mAdapter.getRemoteDevice(address); |
| 155 | BluetoothGattCharacteristic characteristic = getCharacteristicByHandle(handle); |
| 156 | if (characteristic == null) { |
| 157 | Log.w(TAG, "onCharacteristicReadRequest() no char for handle " + handle); |
| 158 | return; |
| 159 | } |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 160 | |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 161 | try { |
| 162 | mCallback.onCharacteristicReadRequest(device, transId, offset, |
| 163 | characteristic); |
| 164 | } catch (Exception ex) { |
| 165 | Log.w(TAG, "Unhandled exception in callback", ex); |
| 166 | } |
| Jakub Pawlowski | 8d7adf2 | 2016-04-01 07:51:45 -0700 | [diff] [blame] | 167 | } |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 168 | |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 169 | /** |
| 170 | * Remote client descriptor read request. |
| 171 | * @hide |
| 172 | */ |
| 173 | @Override |
| 174 | public void onDescriptorReadRequest(String address, int transId, |
| 175 | int offset, boolean isLong, int handle) { |
| 176 | if (VDBG) Log.d(TAG, "onCharacteristicReadRequest() - handle=" + handle); |
| 177 | |
| 178 | BluetoothDevice device = mAdapter.getRemoteDevice(address); |
| 179 | BluetoothGattDescriptor descriptor = getDescriptorByHandle(handle); |
| 180 | if (descriptor == null) { |
| 181 | Log.w(TAG, "onDescriptorReadRequest() no desc for handle " + handle); |
| 182 | return; |
| 183 | } |
| 184 | |
| 185 | try { |
| 186 | mCallback.onDescriptorReadRequest(device, transId, offset, descriptor); |
| 187 | } catch (Exception ex) { |
| 188 | Log.w(TAG, "Unhandled exception in callback", ex); |
| 189 | } |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 190 | } |
| 191 | |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 192 | /** |
| 193 | * Remote client characteristic write request. |
| 194 | * @hide |
| 195 | */ |
| 196 | @Override |
| 197 | public void onCharacteristicWriteRequest(String address, int transId, |
| 198 | int offset, int length, boolean isPrep, boolean needRsp, |
| 199 | int handle, byte[] value) { |
| 200 | if (VDBG) Log.d(TAG, "onCharacteristicWriteRequest() - handle=" + handle); |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 201 | |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 202 | BluetoothDevice device = mAdapter.getRemoteDevice(address); |
| 203 | BluetoothGattCharacteristic characteristic = getCharacteristicByHandle(handle); |
| 204 | if (characteristic == null) { |
| 205 | Log.w(TAG, "onCharacteristicWriteRequest() no char for handle " + handle); |
| 206 | return; |
| 207 | } |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 208 | |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 209 | try { |
| 210 | mCallback.onCharacteristicWriteRequest(device, transId, characteristic, |
| 211 | isPrep, needRsp, offset, value); |
| 212 | } catch (Exception ex) { |
| 213 | Log.w(TAG, "Unhandled exception in callback", ex); |
| 214 | } |
| 215 | |
| Jakub Pawlowski | 8d7adf2 | 2016-04-01 07:51:45 -0700 | [diff] [blame] | 216 | } |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 217 | |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 218 | /** |
| 219 | * Remote client descriptor write request. |
| 220 | * @hide |
| 221 | */ |
| 222 | @Override |
| 223 | public void onDescriptorWriteRequest(String address, int transId, int offset, |
| 224 | int length, boolean isPrep, boolean needRsp, int handle, byte[] value) { |
| 225 | if (VDBG) Log.d(TAG, "onDescriptorWriteRequest() - handle=" + handle); |
| 226 | |
| 227 | BluetoothDevice device = mAdapter.getRemoteDevice(address); |
| 228 | BluetoothGattDescriptor descriptor = getDescriptorByHandle(handle); |
| 229 | if (descriptor == null) { |
| 230 | Log.w(TAG, "onDescriptorWriteRequest() no desc for handle " + handle); |
| 231 | return; |
| 232 | } |
| 233 | |
| 234 | try { |
| 235 | mCallback.onDescriptorWriteRequest(device, transId, descriptor, |
| 236 | isPrep, needRsp, offset, value); |
| 237 | } catch (Exception ex) { |
| 238 | Log.w(TAG, "Unhandled exception in callback", ex); |
| 239 | } |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 240 | } |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 241 | |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 242 | /** |
| 243 | * Execute pending writes. |
| 244 | * @hide |
| 245 | */ |
| 246 | @Override |
| 247 | public void onExecuteWrite(String address, int transId, |
| 248 | boolean execWrite) { |
| 249 | if (DBG) { |
| 250 | Log.d(TAG, "onExecuteWrite() - " |
| 251 | + "device=" + address + ", transId=" + transId |
| 252 | + "execWrite=" + execWrite); |
| 253 | } |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 254 | |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 255 | BluetoothDevice device = mAdapter.getRemoteDevice(address); |
| 256 | if (device == null) return; |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 257 | |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 258 | try { |
| 259 | mCallback.onExecuteWrite(device, transId, execWrite); |
| 260 | } catch (Exception ex) { |
| 261 | Log.w(TAG, "Unhandled exception in callback", ex); |
| 262 | } |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 263 | } |
| Andre Eisenbach | 745ef9e | 2014-03-28 14:54:53 -0700 | [diff] [blame] | 264 | |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 265 | /** |
| 266 | * A notification/indication has been sent. |
| 267 | * @hide |
| 268 | */ |
| 269 | @Override |
| 270 | public void onNotificationSent(String address, int status) { |
| 271 | if (VDBG) { |
| 272 | Log.d(TAG, "onNotificationSent() - " |
| 273 | + "device=" + address + ", status=" + status); |
| 274 | } |
| Andre Eisenbach | 745ef9e | 2014-03-28 14:54:53 -0700 | [diff] [blame] | 275 | |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 276 | BluetoothDevice device = mAdapter.getRemoteDevice(address); |
| 277 | if (device == null) return; |
| Andre Eisenbach | 745ef9e | 2014-03-28 14:54:53 -0700 | [diff] [blame] | 278 | |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 279 | try { |
| 280 | mCallback.onNotificationSent(device, status); |
| 281 | } catch (Exception ex) { |
| 282 | Log.w(TAG, "Unhandled exception: " + ex); |
| 283 | } |
| Andre Eisenbach | 745ef9e | 2014-03-28 14:54:53 -0700 | [diff] [blame] | 284 | } |
| Andre Eisenbach | 5682be9 | 2014-11-18 11:34:26 -0800 | [diff] [blame] | 285 | |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 286 | /** |
| 287 | * The MTU for a connection has changed |
| 288 | * @hide |
| 289 | */ |
| 290 | @Override |
| 291 | public void onMtuChanged(String address, int mtu) { |
| 292 | if (DBG) { |
| 293 | Log.d(TAG, "onMtuChanged() - " |
| 294 | + "device=" + address + ", mtu=" + mtu); |
| 295 | } |
| Andre Eisenbach | 5682be9 | 2014-11-18 11:34:26 -0800 | [diff] [blame] | 296 | |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 297 | BluetoothDevice device = mAdapter.getRemoteDevice(address); |
| 298 | if (device == null) return; |
| Andre Eisenbach | 5682be9 | 2014-11-18 11:34:26 -0800 | [diff] [blame] | 299 | |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 300 | try { |
| 301 | mCallback.onMtuChanged(device, mtu); |
| 302 | } catch (Exception ex) { |
| 303 | Log.w(TAG, "Unhandled exception: " + ex); |
| 304 | } |
| Andre Eisenbach | 5682be9 | 2014-11-18 11:34:26 -0800 | [diff] [blame] | 305 | } |
| Jakub Pawlowski | ad091bb | 2017-02-02 08:07:12 -0800 | [diff] [blame] | 306 | |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 307 | /** |
| 308 | * The PHY for a connection was updated |
| 309 | * @hide |
| 310 | */ |
| 311 | @Override |
| 312 | public void onPhyUpdate(String address, int txPhy, int rxPhy, int status) { |
| 313 | if (DBG) { |
| 314 | Log.d(TAG, |
| 315 | "onPhyUpdate() - " + "device=" + address + ", txPHy=" + txPhy |
| 316 | + ", rxPHy=" + rxPhy); |
| 317 | } |
| Jakub Pawlowski | ad091bb | 2017-02-02 08:07:12 -0800 | [diff] [blame] | 318 | |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 319 | BluetoothDevice device = mAdapter.getRemoteDevice(address); |
| 320 | if (device == null) return; |
| Jakub Pawlowski | ad091bb | 2017-02-02 08:07:12 -0800 | [diff] [blame] | 321 | |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 322 | try { |
| 323 | mCallback.onPhyUpdate(device, txPhy, rxPhy, status); |
| 324 | } catch (Exception ex) { |
| 325 | Log.w(TAG, "Unhandled exception: " + ex); |
| 326 | } |
| Jakub Pawlowski | ad091bb | 2017-02-02 08:07:12 -0800 | [diff] [blame] | 327 | } |
| Jakub Pawlowski | ad091bb | 2017-02-02 08:07:12 -0800 | [diff] [blame] | 328 | |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 329 | /** |
| 330 | * The PHY for a connection was read |
| 331 | * @hide |
| 332 | */ |
| 333 | @Override |
| 334 | public void onPhyRead(String address, int txPhy, int rxPhy, int status) { |
| 335 | if (DBG) { |
| 336 | Log.d(TAG, |
| 337 | "onPhyUpdate() - " + "device=" + address + ", txPHy=" + txPhy |
| 338 | + ", rxPHy=" + rxPhy); |
| 339 | } |
| Jakub Pawlowski | ad091bb | 2017-02-02 08:07:12 -0800 | [diff] [blame] | 340 | |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 341 | BluetoothDevice device = mAdapter.getRemoteDevice(address); |
| 342 | if (device == null) return; |
| Jakub Pawlowski | ad091bb | 2017-02-02 08:07:12 -0800 | [diff] [blame] | 343 | |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 344 | try { |
| 345 | mCallback.onPhyRead(device, txPhy, rxPhy, status); |
| 346 | } catch (Exception ex) { |
| 347 | Log.w(TAG, "Unhandled exception: " + ex); |
| 348 | } |
| Jakub Pawlowski | ad091bb | 2017-02-02 08:07:12 -0800 | [diff] [blame] | 349 | } |
| Jakub Pawlowski | b651de2 | 2017-03-23 19:05:55 -0700 | [diff] [blame] | 350 | |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 351 | /** |
| 352 | * Callback invoked when the given connection is updated |
| 353 | * @hide |
| 354 | */ |
| 355 | @Override |
| 356 | public void onConnectionUpdated(String address, int interval, int latency, |
| 357 | int timeout, int status) { |
| 358 | if (DBG) { |
| Jack He | 9e045d2 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 359 | Log.d(TAG, "onConnectionUpdated() - Device=" + address |
| 360 | + " interval=" + interval + " latency=" + latency |
| 361 | + " timeout=" + timeout + " status=" + status); |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 362 | } |
| 363 | BluetoothDevice device = mAdapter.getRemoteDevice(address); |
| 364 | if (device == null) return; |
| Jakub Pawlowski | b651de2 | 2017-03-23 19:05:55 -0700 | [diff] [blame] | 365 | |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 366 | try { |
| 367 | mCallback.onConnectionUpdated(device, interval, latency, |
| 368 | timeout, status); |
| 369 | } catch (Exception ex) { |
| 370 | Log.w(TAG, "Unhandled exception: " + ex); |
| 371 | } |
| Jakub Pawlowski | b651de2 | 2017-03-23 19:05:55 -0700 | [diff] [blame] | 372 | } |
| Jakub Pawlowski | b651de2 | 2017-03-23 19:05:55 -0700 | [diff] [blame] | 373 | |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 374 | }; |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 375 | |
| 376 | /** |
| 377 | * Create a BluetoothGattServer proxy object. |
| 378 | */ |
| Jeremy Klein | 59d1565 | 2016-09-27 14:34:33 -0700 | [diff] [blame] | 379 | /*package*/ BluetoothGattServer(IBluetoothGatt iGatt, int transport) { |
| Matthew Xie | 9afa274 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 380 | mService = iGatt; |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 381 | mAdapter = BluetoothAdapter.getDefaultAdapter(); |
| Matthew Xie | 9afa274 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 382 | mCallback = null; |
| 383 | mServerIf = 0; |
| Ganesh Ganapathi Batta | ec66191 | 2014-04-18 10:00:40 -0700 | [diff] [blame] | 384 | mTransport = transport; |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 385 | mServices = new ArrayList<BluetoothGattService>(); |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | /** |
| Jakub Pawlowski | 8d7adf2 | 2016-04-01 07:51:45 -0700 | [diff] [blame] | 389 | * Returns a characteristic with given handle. |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 390 | * |
| Jakub Pawlowski | 8d7adf2 | 2016-04-01 07:51:45 -0700 | [diff] [blame] | 391 | * @hide |
| 392 | */ |
| 393 | /*package*/ BluetoothGattCharacteristic getCharacteristicByHandle(int handle) { |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 394 | for (BluetoothGattService svc : mServices) { |
| 395 | for (BluetoothGattCharacteristic charac : svc.getCharacteristics()) { |
| 396 | if (charac.getInstanceId() == handle) { |
| Jakub Pawlowski | 8d7adf2 | 2016-04-01 07:51:45 -0700 | [diff] [blame] | 397 | return charac; |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 398 | } |
| Jakub Pawlowski | 8d7adf2 | 2016-04-01 07:51:45 -0700 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | return null; |
| 402 | } |
| 403 | |
| 404 | /** |
| 405 | * Returns a descriptor with given handle. |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 406 | * |
| Jakub Pawlowski | 8d7adf2 | 2016-04-01 07:51:45 -0700 | [diff] [blame] | 407 | * @hide |
| 408 | */ |
| 409 | /*package*/ BluetoothGattDescriptor getDescriptorByHandle(int handle) { |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 410 | for (BluetoothGattService svc : mServices) { |
| 411 | for (BluetoothGattCharacteristic charac : svc.getCharacteristics()) { |
| 412 | for (BluetoothGattDescriptor desc : charac.getDescriptors()) { |
| 413 | if (desc.getInstanceId() == handle) { |
| Jakub Pawlowski | 8d7adf2 | 2016-04-01 07:51:45 -0700 | [diff] [blame] | 414 | return desc; |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 415 | } |
| Jakub Pawlowski | 8d7adf2 | 2016-04-01 07:51:45 -0700 | [diff] [blame] | 416 | } |
| 417 | } |
| 418 | } |
| 419 | return null; |
| 420 | } |
| 421 | |
| 422 | /** |
| Andre Eisenbach | 6b79dec | 2013-04-05 09:34:11 -0700 | [diff] [blame] | 423 | * Close this GATT server instance. |
| Matthew Xie | abb6558 | 2013-05-29 10:19:06 -0700 | [diff] [blame] | 424 | * |
| 425 | * Application should call this method as early as possible after it is done with |
| 426 | * this GATT server. |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 427 | */ |
| Andre Eisenbach | 6b79dec | 2013-04-05 09:34:11 -0700 | [diff] [blame] | 428 | public void close() { |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 429 | if (DBG) Log.d(TAG, "close()"); |
| Matthew Xie | 9afa274 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 430 | unregisterCallback(); |
| 431 | } |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 432 | |
| Matthew Xie | 9afa274 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 433 | /** |
| 434 | * Register an application callback to start using GattServer. |
| 435 | * |
| 436 | * <p>This is an asynchronous call. The callback is used to notify |
| 437 | * success or failure if the function returns true. |
| 438 | * |
| 439 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 440 | * |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 441 | * @param callback GATT callback handler that will receive asynchronous callbacks. |
| 442 | * @return true, the callback will be called to notify success or failure, false on immediate |
| 443 | * error |
| Matthew Xie | 9afa274 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 444 | */ |
| Jakub Pawlowski | cd8835e | 2017-03-22 11:22:18 -0700 | [diff] [blame] | 445 | /*package*/ boolean registerCallback(BluetoothGattServerCallback callback) { |
| Łukasz Rymanowski | 5d55645 | 2020-07-23 14:57:36 +0200 | [diff] [blame^] | 446 | return registerCallback(callback, false); |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * Register an application callback to start using GattServer. |
| 451 | * |
| 452 | * <p>This is an asynchronous call. The callback is used to notify |
| 453 | * success or failure if the function returns true. |
| 454 | * |
| 455 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 456 | * |
| 457 | * @param callback GATT callback handler that will receive asynchronous callbacks. |
| 458 | * @param eatt_support indicates if server can use eatt |
| 459 | * @return true, the callback will be called to notify success or failure, false on immediate |
| 460 | * error |
| 461 | * @hide |
| 462 | */ |
| 463 | /*package*/ boolean registerCallback(BluetoothGattServerCallback callback, |
| 464 | boolean eatt_support) { |
| Matthew Xie | 9afa274 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 465 | if (DBG) Log.d(TAG, "registerCallback()"); |
| 466 | if (mService == null) { |
| 467 | Log.e(TAG, "GATT service not available"); |
| 468 | return false; |
| 469 | } |
| 470 | UUID uuid = UUID.randomUUID(); |
| 471 | if (DBG) Log.d(TAG, "registerCallback() - UUID=" + uuid); |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 472 | |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 473 | synchronized (mServerIfLock) { |
| Matthew Xie | 9afa274 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 474 | if (mCallback != null) { |
| 475 | Log.e(TAG, "App can register callback only once"); |
| 476 | return false; |
| 477 | } |
| 478 | |
| 479 | mCallback = callback; |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 480 | try { |
| Łukasz Rymanowski | 5d55645 | 2020-07-23 14:57:36 +0200 | [diff] [blame^] | 481 | mService.registerServer(new ParcelUuid(uuid), mBluetoothGattServerCallback, eatt_support); |
| Matthew Xie | 9afa274 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 482 | } catch (RemoteException e) { |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 483 | Log.e(TAG, "", e); |
| Matthew Xie | 9afa274 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 484 | mCallback = null; |
| 485 | return false; |
| 486 | } |
| 487 | |
| 488 | try { |
| 489 | mServerIfLock.wait(CALLBACK_REG_TIMEOUT); |
| 490 | } catch (InterruptedException e) { |
| 491 | Log.e(TAG, "" + e); |
| 492 | mCallback = null; |
| 493 | } |
| 494 | |
| 495 | if (mServerIf == 0) { |
| 496 | mCallback = null; |
| 497 | return false; |
| 498 | } else { |
| 499 | return true; |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 500 | } |
| 501 | } |
| Matthew Xie | 9afa274 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 502 | } |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 503 | |
| Matthew Xie | 9afa274 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 504 | /** |
| 505 | * Unregister the current application and callbacks. |
| 506 | */ |
| 507 | private void unregisterCallback() { |
| 508 | if (DBG) Log.d(TAG, "unregisterCallback() - mServerIf=" + mServerIf); |
| 509 | if (mService == null || mServerIf == 0) return; |
| 510 | |
| 511 | try { |
| 512 | mCallback = null; |
| 513 | mService.unregisterServer(mServerIf); |
| 514 | mServerIf = 0; |
| 515 | } catch (RemoteException e) { |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 516 | Log.e(TAG, "", e); |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 517 | } |
| 518 | } |
| 519 | |
| 520 | /** |
| 521 | * Returns a service by UUID, instance and type. |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 522 | * |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 523 | * @hide |
| 524 | */ |
| 525 | /*package*/ BluetoothGattService getService(UUID uuid, int instanceId, int type) { |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 526 | for (BluetoothGattService svc : mServices) { |
| Jack He | 9e045d2 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 527 | if (svc.getType() == type |
| 528 | && svc.getInstanceId() == instanceId |
| 529 | && svc.getUuid().equals(uuid)) { |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 530 | return svc; |
| 531 | } |
| 532 | } |
| 533 | return null; |
| 534 | } |
| 535 | |
| 536 | /** |
| Matthew Xie | 9afa274 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 537 | * Initiate a connection to a Bluetooth GATT capable device. |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 538 | * |
| 539 | * <p>The connection may not be established right away, but will be |
| 540 | * completed when the remote device is available. A |
| Jakub Pawlowski | cd8835e | 2017-03-22 11:22:18 -0700 | [diff] [blame] | 541 | * {@link BluetoothGattServerCallback#onConnectionStateChange} callback will be |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 542 | * invoked when the connection state changes as a result of this function. |
| 543 | * |
| kopriva | 985ec79 | 2018-10-09 13:42:28 -0700 | [diff] [blame] | 544 | * <p>The autoConnect parameter determines whether to actively connect to |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 545 | * the remote device, or rather passively scan and finalize the connection |
| 546 | * when the remote device is in range/available. Generally, the first ever |
| 547 | * connection to a device should be direct (autoConnect set to false) and |
| 548 | * subsequent connections to known devices should be invoked with the |
| Matthew Xie | 9afa274 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 549 | * autoConnect parameter set to true. |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 550 | * |
| 551 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 552 | * |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 553 | * @param autoConnect Whether to directly connect to the remote device (false) or to |
| 554 | * automatically connect as soon as the remote device becomes available (true). |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 555 | * @return true, if the connection attempt was initiated successfully |
| 556 | */ |
| 557 | public boolean connect(BluetoothDevice device, boolean autoConnect) { |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 558 | if (DBG) { |
| 559 | Log.d(TAG, |
| 560 | "connect() - device: " + device.getAddress() + ", auto: " + autoConnect); |
| 561 | } |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 562 | if (mService == null || mServerIf == 0) return false; |
| 563 | |
| 564 | try { |
| Jack He | 9e045d2 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 565 | // autoConnect is inverse of "isDirect" |
| 566 | mService.serverConnect(mServerIf, device.getAddress(), !autoConnect, mTransport); |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 567 | } catch (RemoteException e) { |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 568 | Log.e(TAG, "", e); |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 569 | return false; |
| 570 | } |
| 571 | |
| 572 | return true; |
| 573 | } |
| 574 | |
| 575 | /** |
| 576 | * Disconnects an established connection, or cancels a connection attempt |
| 577 | * currently in progress. |
| 578 | * |
| 579 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 580 | * |
| 581 | * @param device Remote device |
| 582 | */ |
| 583 | public void cancelConnection(BluetoothDevice device) { |
| 584 | if (DBG) Log.d(TAG, "cancelConnection() - device: " + device.getAddress()); |
| 585 | if (mService == null || mServerIf == 0) return; |
| 586 | |
| 587 | try { |
| 588 | mService.serverDisconnect(mServerIf, device.getAddress()); |
| 589 | } catch (RemoteException e) { |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 590 | Log.e(TAG, "", e); |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 591 | } |
| 592 | } |
| 593 | |
| 594 | /** |
| Jakub Pawlowski | ad091bb | 2017-02-02 08:07:12 -0800 | [diff] [blame] | 595 | * Set the preferred connection PHY for this app. Please note that this is just a |
| Jakub Pawlowski | 1bafa47 | 2017-03-22 22:44:09 -0700 | [diff] [blame] | 596 | * recommendation, whether the PHY change will happen depends on other applications peferences, |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 597 | * local and remote controller capabilities. Controller can override these settings. <p> {@link |
| 598 | * BluetoothGattServerCallback#onPhyUpdate} will be triggered as a result of this call, even if |
| 599 | * no PHY change happens. It is also triggered when remote device updates the PHY. |
| Jakub Pawlowski | ad091bb | 2017-02-02 08:07:12 -0800 | [diff] [blame] | 600 | * |
| 601 | * @param device The remote device to send this response to |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 602 | * @param txPhy preferred transmitter PHY. Bitwise OR of any of {@link |
| 603 | * BluetoothDevice#PHY_LE_1M_MASK}, {@link BluetoothDevice#PHY_LE_2M_MASK}, and {@link |
| 604 | * BluetoothDevice#PHY_LE_CODED_MASK}. |
| 605 | * @param rxPhy preferred receiver PHY. Bitwise OR of any of {@link |
| 606 | * BluetoothDevice#PHY_LE_1M_MASK}, {@link BluetoothDevice#PHY_LE_2M_MASK}, and {@link |
| 607 | * BluetoothDevice#PHY_LE_CODED_MASK}. |
| Jakub Pawlowski | ad091bb | 2017-02-02 08:07:12 -0800 | [diff] [blame] | 608 | * @param phyOptions preferred coding to use when transmitting on the LE Coded PHY. Can be one |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 609 | * of {@link BluetoothDevice#PHY_OPTION_NO_PREFERRED}, {@link BluetoothDevice#PHY_OPTION_S2} or |
| 610 | * {@link BluetoothDevice#PHY_OPTION_S8} |
| Jakub Pawlowski | ad091bb | 2017-02-02 08:07:12 -0800 | [diff] [blame] | 611 | */ |
| 612 | public void setPreferredPhy(BluetoothDevice device, int txPhy, int rxPhy, int phyOptions) { |
| 613 | try { |
| 614 | mService.serverSetPreferredPhy(mServerIf, device.getAddress(), txPhy, rxPhy, |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 615 | phyOptions); |
| Jakub Pawlowski | ad091bb | 2017-02-02 08:07:12 -0800 | [diff] [blame] | 616 | } catch (RemoteException e) { |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 617 | Log.e(TAG, "", e); |
| Jakub Pawlowski | ad091bb | 2017-02-02 08:07:12 -0800 | [diff] [blame] | 618 | } |
| 619 | } |
| 620 | |
| 621 | /** |
| 622 | * Read the current transmitter PHY and receiver PHY of the connection. The values are returned |
| Jakub Pawlowski | cd8835e | 2017-03-22 11:22:18 -0700 | [diff] [blame] | 623 | * in {@link BluetoothGattServerCallback#onPhyRead} |
| Jakub Pawlowski | ad091bb | 2017-02-02 08:07:12 -0800 | [diff] [blame] | 624 | * |
| 625 | * @param device The remote device to send this response to |
| 626 | */ |
| 627 | public void readPhy(BluetoothDevice device) { |
| 628 | try { |
| 629 | mService.serverReadPhy(mServerIf, device.getAddress()); |
| 630 | } catch (RemoteException e) { |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 631 | Log.e(TAG, "", e); |
| Jakub Pawlowski | ad091bb | 2017-02-02 08:07:12 -0800 | [diff] [blame] | 632 | } |
| 633 | } |
| 634 | |
| 635 | /** |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 636 | * Send a response to a read or write request to a remote device. |
| 637 | * |
| 638 | * <p>This function must be invoked in when a remote read/write request |
| Matthew Xie | 9afa274 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 639 | * is received by one of these callback methods: |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 640 | * |
| 641 | * <ul> |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 642 | * <li>{@link BluetoothGattServerCallback#onCharacteristicReadRequest} |
| 643 | * <li>{@link BluetoothGattServerCallback#onCharacteristicWriteRequest} |
| 644 | * <li>{@link BluetoothGattServerCallback#onDescriptorReadRequest} |
| 645 | * <li>{@link BluetoothGattServerCallback#onDescriptorWriteRequest} |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 646 | * </ul> |
| 647 | * |
| 648 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 649 | * |
| 650 | * @param device The remote device to send this response to |
| 651 | * @param requestId The ID of the request that was received with the callback |
| 652 | * @param status The status of the request to be sent to the remote devices |
| 653 | * @param offset Value offset for partial read/write response |
| 654 | * @param value The value of the attribute that was read/written (optional) |
| 655 | */ |
| 656 | public boolean sendResponse(BluetoothDevice device, int requestId, |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 657 | int status, int offset, byte[] value) { |
| Andre Eisenbach | 10020cd | 2014-07-18 14:38:36 -0700 | [diff] [blame] | 658 | if (VDBG) Log.d(TAG, "sendResponse() - device: " + device.getAddress()); |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 659 | if (mService == null || mServerIf == 0) return false; |
| 660 | |
| 661 | try { |
| 662 | mService.sendResponse(mServerIf, device.getAddress(), requestId, |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 663 | status, offset, value); |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 664 | } catch (RemoteException e) { |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 665 | Log.e(TAG, "", e); |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 666 | return false; |
| 667 | } |
| 668 | return true; |
| 669 | } |
| 670 | |
| 671 | /** |
| 672 | * Send a notification or indication that a local characteristic has been |
| 673 | * updated. |
| 674 | * |
| 675 | * <p>A notification or indication is sent to the remote device to signal |
| 676 | * that the characteristic has been updated. This function should be invoked |
| 677 | * for every client that requests notifications/indications by writing |
| 678 | * to the "Client Configuration" descriptor for the given characteristic. |
| 679 | * |
| 680 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 681 | * |
| 682 | * @param device The remote device to receive the notification/indication |
| 683 | * @param characteristic The local characteristic that has been updated |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 684 | * @param confirm true to request confirmation from the client (indication), false to send a |
| 685 | * notification |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 686 | * @return true, if the notification has been triggered successfully |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 687 | * @throws IllegalArgumentException |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 688 | */ |
| 689 | public boolean notifyCharacteristicChanged(BluetoothDevice device, |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 690 | BluetoothGattCharacteristic characteristic, boolean confirm) { |
| Andre Eisenbach | 10020cd | 2014-07-18 14:38:36 -0700 | [diff] [blame] | 691 | if (VDBG) Log.d(TAG, "notifyCharacteristicChanged() - device: " + device.getAddress()); |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 692 | if (mService == null || mServerIf == 0) return false; |
| 693 | |
| 694 | BluetoothGattService service = characteristic.getService(); |
| 695 | if (service == null) return false; |
| 696 | |
| Prerepa Viswanadham | ffabc51 | 2014-08-13 14:46:58 -0700 | [diff] [blame] | 697 | if (characteristic.getValue() == null) { |
| 698 | throw new IllegalArgumentException("Chracteristic value is empty. Use " |
| 699 | + "BluetoothGattCharacteristic#setvalue to update"); |
| 700 | } |
| 701 | |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 702 | try { |
| 703 | mService.sendNotification(mServerIf, device.getAddress(), |
| Jakub Pawlowski | 8d7adf2 | 2016-04-01 07:51:45 -0700 | [diff] [blame] | 704 | characteristic.getInstanceId(), confirm, |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 705 | characteristic.getValue()); |
| 706 | } catch (RemoteException e) { |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 707 | Log.e(TAG, "", e); |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 708 | return false; |
| 709 | } |
| 710 | |
| 711 | return true; |
| 712 | } |
| 713 | |
| 714 | /** |
| Matthew Xie | 9afa274 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 715 | * Add a service to the list of services to be hosted. |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 716 | * |
| kopriva | 985ec79 | 2018-10-09 13:42:28 -0700 | [diff] [blame] | 717 | * <p>Once a service has been addded to the list, the service and its |
| Matthew Xie | 9afa274 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 718 | * included characteristics will be provided by the local device. |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 719 | * |
| Matthew Xie | 9afa274 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 720 | * <p>If the local device has already exposed services when this function |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 721 | * is called, a service update notification will be sent to all clients. |
| 722 | * |
| Stanley Tng | 9b60fc7 | 2018-04-13 14:54:10 -0700 | [diff] [blame] | 723 | * <p>The {@link BluetoothGattServerCallback#onServiceAdded} callback will indicate |
| 724 | * whether this service has been added successfully. Do not add another service |
| 725 | * before this callback. |
| 726 | * |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 727 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 728 | * |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 729 | * @param service Service to be added to the list of services provided by this device. |
| Stanley Tng | 9b60fc7 | 2018-04-13 14:54:10 -0700 | [diff] [blame] | 730 | * @return true, if the request to add service has been initiated |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 731 | */ |
| 732 | public boolean addService(BluetoothGattService service) { |
| 733 | if (DBG) Log.d(TAG, "addService() - service: " + service.getUuid()); |
| 734 | if (mService == null || mServerIf == 0) return false; |
| 735 | |
| Jakub Pawlowski | 8d7adf2 | 2016-04-01 07:51:45 -0700 | [diff] [blame] | 736 | mPendingService = service; |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 737 | |
| 738 | try { |
| Jakub Pawlowski | 8d7adf2 | 2016-04-01 07:51:45 -0700 | [diff] [blame] | 739 | mService.addService(mServerIf, service); |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 740 | } catch (RemoteException e) { |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 741 | Log.e(TAG, "", e); |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 742 | return false; |
| 743 | } |
| 744 | |
| 745 | return true; |
| 746 | } |
| 747 | |
| 748 | /** |
| Matthew Xie | 9afa274 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 749 | * Removes a service from the list of services to be provided. |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 750 | * |
| 751 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 752 | * |
| Matthew Xie | 9afa274 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 753 | * @param service Service to be removed. |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 754 | * @return true, if the service has been removed |
| 755 | */ |
| 756 | public boolean removeService(BluetoothGattService service) { |
| 757 | if (DBG) Log.d(TAG, "removeService() - service: " + service.getUuid()); |
| 758 | if (mService == null || mServerIf == 0) return false; |
| 759 | |
| 760 | BluetoothGattService intService = getService(service.getUuid(), |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 761 | service.getInstanceId(), service.getType()); |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 762 | if (intService == null) return false; |
| 763 | |
| 764 | try { |
| Jakub Pawlowski | 8d7adf2 | 2016-04-01 07:51:45 -0700 | [diff] [blame] | 765 | mService.removeService(mServerIf, service.getInstanceId()); |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 766 | mServices.remove(intService); |
| 767 | } catch (RemoteException e) { |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 768 | Log.e(TAG, "", e); |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 769 | return false; |
| 770 | } |
| 771 | |
| 772 | return true; |
| 773 | } |
| 774 | |
| 775 | /** |
| Matthew Xie | 9afa274 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 776 | * Remove all services from the list of provided services. |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 777 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 778 | */ |
| 779 | public void clearServices() { |
| 780 | if (DBG) Log.d(TAG, "clearServices()"); |
| 781 | if (mService == null || mServerIf == 0) return; |
| 782 | |
| 783 | try { |
| 784 | mService.clearServices(mServerIf); |
| 785 | mServices.clear(); |
| 786 | } catch (RemoteException e) { |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 787 | Log.e(TAG, "", e); |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 788 | } |
| 789 | } |
| 790 | |
| 791 | /** |
| Matthew Xie | 9afa274 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 792 | * Returns a list of GATT services offered by this device. |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 793 | * |
| 794 | * <p>An application must call {@link #addService} to add a serice to the |
| 795 | * list of services offered by this device. |
| 796 | * |
| 797 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 798 | * |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 799 | * @return List of services. Returns an empty list if no services have been added yet. |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 800 | */ |
| 801 | public List<BluetoothGattService> getServices() { |
| 802 | return mServices; |
| 803 | } |
| 804 | |
| 805 | /** |
| 806 | * Returns a {@link BluetoothGattService} from the list of services offered |
| 807 | * by this device. |
| 808 | * |
| 809 | * <p>If multiple instances of the same service (as identified by UUID) |
| 810 | * exist, the first instance of the service is returned. |
| 811 | * |
| 812 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 813 | * |
| 814 | * @param uuid UUID of the requested service |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 815 | * @return BluetoothGattService if supported, or null if the requested service is not offered by |
| 816 | * this device. |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 817 | */ |
| 818 | public BluetoothGattService getService(UUID uuid) { |
| 819 | for (BluetoothGattService service : mServices) { |
| 820 | if (service.getUuid().equals(uuid)) { |
| 821 | return service; |
| 822 | } |
| 823 | } |
| 824 | |
| 825 | return null; |
| 826 | } |
| 827 | |
| Matthew Xie | 9afa274 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 828 | |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 829 | /** |
| Matthew Xie | 9afa274 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 830 | * Not supported - please use {@link BluetoothManager#getConnectedDevices(int)} |
| 831 | * with {@link BluetoothProfile#GATT} as argument |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 832 | * |
| Matthew Xie | 9afa274 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 833 | * @throws UnsupportedOperationException |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 834 | */ |
| 835 | @Override |
| 836 | public int getConnectionState(BluetoothDevice device) { |
| Matthew Xie | 9afa274 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 837 | throw new UnsupportedOperationException("Use BluetoothManager#getConnectionState instead."); |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | /** |
| Matthew Xie | 9afa274 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 841 | * Not supported - please use {@link BluetoothManager#getConnectedDevices(int)} |
| 842 | * with {@link BluetoothProfile#GATT} as argument |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 843 | * |
| Matthew Xie | 9afa274 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 844 | * @throws UnsupportedOperationException |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 845 | */ |
| 846 | @Override |
| 847 | public List<BluetoothDevice> getConnectedDevices() { |
| Jack He | 9e045d2 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 848 | throw new UnsupportedOperationException( |
| 849 | "Use BluetoothManager#getConnectedDevices instead."); |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | /** |
| Matthew Xie | 9afa274 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 853 | * Not supported - please use |
| 854 | * {@link BluetoothManager#getDevicesMatchingConnectionStates(int, int[])} |
| 855 | * with {@link BluetoothProfile#GATT} as first argument |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 856 | * |
| Matthew Xie | 9afa274 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 857 | * @throws UnsupportedOperationException |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 858 | */ |
| 859 | @Override |
| 860 | public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { |
| Jack He | 9e045d2 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 861 | throw new UnsupportedOperationException( |
| 862 | "Use BluetoothManager#getDevicesMatchingConnectionStates instead."); |
| Ganesh Ganapathi Batta | 40b9895 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 863 | } |
| 864 | } |