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 | |
| 19 | import android.bluetooth.BluetoothAdapter; |
| 20 | import android.bluetooth.BluetoothDevice; |
| 21 | import android.bluetooth.BluetoothProfile; |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 22 | import android.content.Context; |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 23 | import android.os.ParcelUuid; |
| 24 | import android.os.RemoteException; |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 25 | import android.util.Log; |
| 26 | |
| 27 | import java.util.ArrayList; |
| 28 | import java.util.List; |
| 29 | import java.util.UUID; |
| 30 | |
| 31 | /** |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 32 | * Public API for the Bluetooth GATT Profile server role. |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 33 | * |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 34 | * <p>This class provides Bluetooth GATT server role functionality, |
Andre Eisenbach | 061cfd0 | 2014-06-27 14:31:37 -0700 | [diff] [blame] | 35 | * allowing applications to create Bluetooth Smart services and |
| 36 | * characteristics. |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 37 | * |
| 38 | * <p>BluetoothGattServer is a proxy object for controlling the Bluetooth Service |
Andre Eisenbach | 061cfd0 | 2014-06-27 14:31:37 -0700 | [diff] [blame] | 39 | * via IPC. Use {@link BluetoothManager#openGattServer} to get an instance |
| 40 | * of this class. |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 41 | */ |
| 42 | public final class BluetoothGattServer implements BluetoothProfile { |
| 43 | private static final String TAG = "BluetoothGattServer"; |
| 44 | private static final boolean DBG = true; |
Andre Eisenbach | 55d19e4 | 2014-07-18 14:38:36 -0700 | [diff] [blame] | 45 | private static final boolean VDBG = false; |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 46 | |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 47 | private BluetoothAdapter mAdapter; |
| 48 | private IBluetoothGatt mService; |
| 49 | private BluetoothGattServerCallback mCallback; |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 50 | |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 51 | private Object mServerIfLock = new Object(); |
| 52 | private int mServerIf; |
Ganesh Ganapathi Batta | b88fa82 | 2014-04-18 10:00:40 -0700 | [diff] [blame] | 53 | private int mTransport; |
Jakub Pawlowski | d75f512 | 2016-04-01 07:51:45 -0700 | [diff] [blame] | 54 | private BluetoothGattService mPendingService; |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 55 | private List<BluetoothGattService> mServices; |
| 56 | |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 57 | private static final int CALLBACK_REG_TIMEOUT = 10000; |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 58 | |
| 59 | /** |
| 60 | * Bluetooth GATT interface callbacks |
| 61 | */ |
| 62 | private final IBluetoothGattServerCallback mBluetoothGattServerCallback = |
| 63 | new IBluetoothGattServerCallback.Stub() { |
| 64 | /** |
| 65 | * Application interface registered - app is ready to go |
| 66 | * @hide |
| 67 | */ |
| 68 | public void onServerRegistered(int status, int serverIf) { |
| 69 | if (DBG) Log.d(TAG, "onServerRegistered() - status=" + status |
| 70 | + " serverIf=" + serverIf); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 71 | synchronized(mServerIfLock) { |
| 72 | if (mCallback != null) { |
| 73 | mServerIf = serverIf; |
| 74 | mServerIfLock.notify(); |
| 75 | } else { |
| 76 | // registration timeout |
| 77 | Log.e(TAG, "onServerRegistered: mCallback is null"); |
| 78 | } |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Callback reporting an LE scan result. |
| 84 | * @hide |
| 85 | */ |
| 86 | public void onScanResult(String address, int rssi, byte[] advData) { |
Andre Eisenbach | 55d19e4 | 2014-07-18 14:38:36 -0700 | [diff] [blame] | 87 | if (VDBG) Log.d(TAG, "onScanResult() - Device=" + address + " RSSI=" +rssi); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 88 | // no op |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Server connection state changed |
| 93 | * @hide |
| 94 | */ |
| 95 | public void onServerConnectionState(int status, int serverIf, |
| 96 | boolean connected, String address) { |
| 97 | if (DBG) Log.d(TAG, "onServerConnectionState() - status=" + status |
| 98 | + " serverIf=" + serverIf + " device=" + address); |
| 99 | try { |
| 100 | mCallback.onConnectionStateChange(mAdapter.getRemoteDevice(address), status, |
| 101 | connected ? BluetoothProfile.STATE_CONNECTED : |
| 102 | BluetoothProfile.STATE_DISCONNECTED); |
| 103 | } catch (Exception ex) { |
Mike Lockwood | 0998ff1 | 2013-05-13 14:04:12 -0700 | [diff] [blame] | 104 | Log.w(TAG, "Unhandled exception in callback", ex); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Service has been added |
| 110 | * @hide |
| 111 | */ |
Jakub Pawlowski | d75f512 | 2016-04-01 07:51:45 -0700 | [diff] [blame] | 112 | public void onServiceAdded(int status, BluetoothGattService service) { |
| 113 | if (DBG) Log.d(TAG, "onServiceAdded() - handle=" + service.getInstanceId() |
| 114 | + " uuid=" + service.getUuid() + " status=" + status); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 115 | |
Jakub Pawlowski | d75f512 | 2016-04-01 07:51:45 -0700 | [diff] [blame] | 116 | if (mPendingService == null) |
| 117 | return; |
| 118 | |
| 119 | BluetoothGattService tmp = mPendingService; |
| 120 | mPendingService = null; |
| 121 | |
| 122 | // Rewrite newly assigned handles to existing service. |
| 123 | tmp.setInstanceId(service.getInstanceId()); |
| 124 | List<BluetoothGattCharacteristic> temp_chars = tmp.getCharacteristics(); |
| 125 | List<BluetoothGattCharacteristic> svc_chars = service.getCharacteristics(); |
| 126 | for (int i=0; i<svc_chars.size(); i++) { |
| 127 | BluetoothGattCharacteristic temp_char = temp_chars.get(i); |
| 128 | BluetoothGattCharacteristic svc_char = svc_chars.get(i); |
| 129 | |
| 130 | temp_char.setInstanceId(svc_char.getInstanceId()); |
| 131 | |
| 132 | List<BluetoothGattDescriptor> temp_descs = temp_char.getDescriptors(); |
| 133 | List<BluetoothGattDescriptor> svc_descs = svc_char.getDescriptors(); |
| 134 | for (int j=0; j<svc_descs.size(); j++) { |
Jakub Pawlowski | d167db1 | 2016-08-04 13:16:32 -0700 | [diff] [blame] | 135 | temp_descs.get(j).setInstanceId(svc_descs.get(j).getInstanceId()); |
Jakub Pawlowski | d75f512 | 2016-04-01 07:51:45 -0700 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
| 139 | mServices.add(tmp); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 140 | |
| 141 | try { |
Jakub Pawlowski | d75f512 | 2016-04-01 07:51:45 -0700 | [diff] [blame] | 142 | mCallback.onServiceAdded((int)status, tmp); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 143 | } catch (Exception ex) { |
Mike Lockwood | 0998ff1 | 2013-05-13 14:04:12 -0700 | [diff] [blame] | 144 | Log.w(TAG, "Unhandled exception in callback", ex); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Remote client characteristic read request. |
| 150 | * @hide |
| 151 | */ |
| 152 | public void onCharacteristicReadRequest(String address, int transId, |
Jakub Pawlowski | d75f512 | 2016-04-01 07:51:45 -0700 | [diff] [blame] | 153 | int offset, boolean isLong, int handle) { |
| 154 | if (VDBG) Log.d(TAG, "onCharacteristicReadRequest() - handle=" + handle); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 155 | |
| 156 | BluetoothDevice device = mAdapter.getRemoteDevice(address); |
Jakub Pawlowski | d75f512 | 2016-04-01 07:51:45 -0700 | [diff] [blame] | 157 | BluetoothGattCharacteristic characteristic = getCharacteristicByHandle(handle); |
| 158 | if (characteristic == null) { |
| 159 | Log.w(TAG, "onCharacteristicReadRequest() no char for handle " + handle); |
| 160 | return; |
| 161 | } |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 162 | |
| 163 | try { |
| 164 | mCallback.onCharacteristicReadRequest(device, transId, offset, characteristic); |
| 165 | } catch (Exception ex) { |
Mike Lockwood | 0998ff1 | 2013-05-13 14:04:12 -0700 | [diff] [blame] | 166 | Log.w(TAG, "Unhandled exception in callback", ex); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Remote client descriptor read request. |
| 172 | * @hide |
| 173 | */ |
| 174 | public void onDescriptorReadRequest(String address, int transId, |
Jakub Pawlowski | d75f512 | 2016-04-01 07:51:45 -0700 | [diff] [blame] | 175 | int offset, boolean isLong, int handle) { |
| 176 | if (VDBG) Log.d(TAG, "onCharacteristicReadRequest() - handle=" + handle); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 177 | |
| 178 | BluetoothDevice device = mAdapter.getRemoteDevice(address); |
Jakub Pawlowski | d75f512 | 2016-04-01 07:51:45 -0700 | [diff] [blame] | 179 | BluetoothGattDescriptor descriptor = getDescriptorByHandle(handle); |
| 180 | if (descriptor == null) { |
| 181 | Log.w(TAG, "onDescriptorReadRequest() no desc for handle " + handle); |
| 182 | return; |
| 183 | } |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 184 | |
| 185 | try { |
| 186 | mCallback.onDescriptorReadRequest(device, transId, offset, descriptor); |
| 187 | } catch (Exception ex) { |
Mike Lockwood | 0998ff1 | 2013-05-13 14:04:12 -0700 | [diff] [blame] | 188 | Log.w(TAG, "Unhandled exception in callback", ex); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Remote client characteristic write request. |
| 194 | * @hide |
| 195 | */ |
| 196 | public void onCharacteristicWriteRequest(String address, int transId, |
| 197 | int offset, int length, boolean isPrep, boolean needRsp, |
Jakub Pawlowski | d75f512 | 2016-04-01 07:51:45 -0700 | [diff] [blame] | 198 | int handle, byte[] value) { |
| 199 | if (VDBG) Log.d(TAG, "onCharacteristicWriteRequest() - handle=" + handle); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 200 | |
| 201 | BluetoothDevice device = mAdapter.getRemoteDevice(address); |
Jakub Pawlowski | d75f512 | 2016-04-01 07:51:45 -0700 | [diff] [blame] | 202 | BluetoothGattCharacteristic characteristic = getCharacteristicByHandle(handle); |
| 203 | if (characteristic == null) { |
| 204 | Log.w(TAG, "onCharacteristicWriteRequest() no char for handle " + handle); |
| 205 | return; |
| 206 | } |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 207 | |
| 208 | try { |
| 209 | mCallback.onCharacteristicWriteRequest(device, transId, characteristic, |
| 210 | isPrep, needRsp, offset, value); |
| 211 | } catch (Exception ex) { |
Mike Lockwood | 0998ff1 | 2013-05-13 14:04:12 -0700 | [diff] [blame] | 212 | Log.w(TAG, "Unhandled exception in callback", ex); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Remote client descriptor write request. |
| 219 | * @hide |
| 220 | */ |
Jakub Pawlowski | d75f512 | 2016-04-01 07:51:45 -0700 | [diff] [blame] | 221 | public void onDescriptorWriteRequest(String address, int transId, int offset, |
| 222 | int length, boolean isPrep, boolean needRsp, int handle, byte[] value) { |
| 223 | if (VDBG) Log.d(TAG, "onDescriptorWriteRequest() - handle=" + handle); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 224 | |
| 225 | BluetoothDevice device = mAdapter.getRemoteDevice(address); |
Jakub Pawlowski | d75f512 | 2016-04-01 07:51:45 -0700 | [diff] [blame] | 226 | BluetoothGattDescriptor descriptor = getDescriptorByHandle(handle); |
| 227 | if (descriptor == null) { |
| 228 | Log.w(TAG, "onDescriptorWriteRequest() no desc for handle " + handle); |
| 229 | return; |
| 230 | } |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 231 | |
| 232 | try { |
| 233 | mCallback.onDescriptorWriteRequest(device, transId, descriptor, |
| 234 | isPrep, needRsp, offset, value); |
| 235 | } catch (Exception ex) { |
Mike Lockwood | 0998ff1 | 2013-05-13 14:04:12 -0700 | [diff] [blame] | 236 | Log.w(TAG, "Unhandled exception in callback", ex); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Execute pending writes. |
| 242 | * @hide |
| 243 | */ |
| 244 | public void onExecuteWrite(String address, int transId, |
| 245 | boolean execWrite) { |
| 246 | if (DBG) Log.d(TAG, "onExecuteWrite() - " |
| 247 | + "device=" + address + ", transId=" + transId |
| 248 | + "execWrite=" + execWrite); |
| 249 | |
| 250 | BluetoothDevice device = mAdapter.getRemoteDevice(address); |
| 251 | if (device == null) return; |
| 252 | |
| 253 | try { |
| 254 | mCallback.onExecuteWrite(device, transId, execWrite); |
| 255 | } catch (Exception ex) { |
Mike Lockwood | 0998ff1 | 2013-05-13 14:04:12 -0700 | [diff] [blame] | 256 | Log.w(TAG, "Unhandled exception in callback", ex); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 257 | } |
| 258 | } |
Andre Eisenbach | dadefda | 2014-03-28 14:54:53 -0700 | [diff] [blame] | 259 | |
| 260 | /** |
| 261 | * A notification/indication has been sent. |
| 262 | * @hide |
| 263 | */ |
| 264 | public void onNotificationSent(String address, int status) { |
Andre Eisenbach | 55d19e4 | 2014-07-18 14:38:36 -0700 | [diff] [blame] | 265 | if (VDBG) Log.d(TAG, "onNotificationSent() - " |
Andre Eisenbach | dadefda | 2014-03-28 14:54:53 -0700 | [diff] [blame] | 266 | + "device=" + address + ", status=" + status); |
| 267 | |
| 268 | BluetoothDevice device = mAdapter.getRemoteDevice(address); |
| 269 | if (device == null) return; |
| 270 | |
| 271 | try { |
| 272 | mCallback.onNotificationSent(device, status); |
| 273 | } catch (Exception ex) { |
| 274 | Log.w(TAG, "Unhandled exception: " + ex); |
| 275 | } |
| 276 | } |
Andre Eisenbach | 16bf846 | 2014-11-18 11:34:26 -0800 | [diff] [blame] | 277 | |
| 278 | /** |
| 279 | * The MTU for a connection has changed |
| 280 | * @hide |
| 281 | */ |
| 282 | public void onMtuChanged(String address, int mtu) { |
| 283 | if (DBG) Log.d(TAG, "onMtuChanged() - " |
| 284 | + "device=" + address + ", mtu=" + mtu); |
| 285 | |
| 286 | BluetoothDevice device = mAdapter.getRemoteDevice(address); |
| 287 | if (device == null) return; |
| 288 | |
| 289 | try { |
| 290 | mCallback.onMtuChanged(device, mtu); |
| 291 | } catch (Exception ex) { |
| 292 | Log.w(TAG, "Unhandled exception: " + ex); |
| 293 | } |
| 294 | } |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 295 | }; |
| 296 | |
| 297 | /** |
| 298 | * Create a BluetoothGattServer proxy object. |
| 299 | */ |
Jeremy Klein | adc26ec | 2016-09-27 14:34:33 -0700 | [diff] [blame] | 300 | /*package*/ BluetoothGattServer(IBluetoothGatt iGatt, int transport) { |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 301 | mService = iGatt; |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 302 | mAdapter = BluetoothAdapter.getDefaultAdapter(); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 303 | mCallback = null; |
| 304 | mServerIf = 0; |
Ganesh Ganapathi Batta | b88fa82 | 2014-04-18 10:00:40 -0700 | [diff] [blame] | 305 | mTransport = transport; |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 306 | mServices = new ArrayList<BluetoothGattService>(); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | /** |
Jakub Pawlowski | d75f512 | 2016-04-01 07:51:45 -0700 | [diff] [blame] | 310 | * Returns a characteristic with given handle. |
| 311 | * @hide |
| 312 | */ |
| 313 | /*package*/ BluetoothGattCharacteristic getCharacteristicByHandle(int handle) { |
| 314 | for(BluetoothGattService svc : mServices) { |
| 315 | for(BluetoothGattCharacteristic charac : svc.getCharacteristics()) { |
| 316 | if (charac.getInstanceId() == handle) |
| 317 | return charac; |
| 318 | } |
| 319 | } |
| 320 | return null; |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * Returns a descriptor with given handle. |
| 325 | * @hide |
| 326 | */ |
| 327 | /*package*/ BluetoothGattDescriptor getDescriptorByHandle(int handle) { |
| 328 | for(BluetoothGattService svc : mServices) { |
| 329 | for(BluetoothGattCharacteristic charac : svc.getCharacteristics()) { |
| 330 | for(BluetoothGattDescriptor desc : charac.getDescriptors()) { |
| 331 | if (desc.getInstanceId() == handle) |
| 332 | return desc; |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | return null; |
| 337 | } |
| 338 | |
| 339 | /** |
Andre Eisenbach | 3b64f38 | 2013-04-05 09:34:11 -0700 | [diff] [blame] | 340 | * Close this GATT server instance. |
Matthew Xie | b30f91e | 2013-05-29 10:19:06 -0700 | [diff] [blame] | 341 | * |
| 342 | * Application should call this method as early as possible after it is done with |
| 343 | * this GATT server. |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 344 | */ |
Andre Eisenbach | 3b64f38 | 2013-04-05 09:34:11 -0700 | [diff] [blame] | 345 | public void close() { |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 346 | if (DBG) Log.d(TAG, "close()"); |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 347 | unregisterCallback(); |
| 348 | } |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 349 | |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 350 | /** |
| 351 | * Register an application callback to start using GattServer. |
| 352 | * |
| 353 | * <p>This is an asynchronous call. The callback is used to notify |
| 354 | * success or failure if the function returns true. |
| 355 | * |
| 356 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 357 | * |
| 358 | * @param callback GATT callback handler that will receive asynchronous |
| 359 | * callbacks. |
| 360 | * @return true, the callback will be called to notify success or failure, |
| 361 | * false on immediate error |
| 362 | */ |
| 363 | /*package*/ boolean registerCallback(BluetoothGattServerCallback callback) { |
| 364 | if (DBG) Log.d(TAG, "registerCallback()"); |
| 365 | if (mService == null) { |
| 366 | Log.e(TAG, "GATT service not available"); |
| 367 | return false; |
| 368 | } |
| 369 | UUID uuid = UUID.randomUUID(); |
| 370 | if (DBG) Log.d(TAG, "registerCallback() - UUID=" + uuid); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 371 | |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 372 | synchronized(mServerIfLock) { |
| 373 | if (mCallback != null) { |
| 374 | Log.e(TAG, "App can register callback only once"); |
| 375 | return false; |
| 376 | } |
| 377 | |
| 378 | mCallback = callback; |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 379 | try { |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 380 | mService.registerServer(new ParcelUuid(uuid), mBluetoothGattServerCallback); |
| 381 | } catch (RemoteException e) { |
| 382 | Log.e(TAG,"",e); |
| 383 | mCallback = null; |
| 384 | return false; |
| 385 | } |
| 386 | |
| 387 | try { |
| 388 | mServerIfLock.wait(CALLBACK_REG_TIMEOUT); |
| 389 | } catch (InterruptedException e) { |
| 390 | Log.e(TAG, "" + e); |
| 391 | mCallback = null; |
| 392 | } |
| 393 | |
| 394 | if (mServerIf == 0) { |
| 395 | mCallback = null; |
| 396 | return false; |
| 397 | } else { |
| 398 | return true; |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 399 | } |
| 400 | } |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 401 | } |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 402 | |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 403 | /** |
| 404 | * Unregister the current application and callbacks. |
| 405 | */ |
| 406 | private void unregisterCallback() { |
| 407 | if (DBG) Log.d(TAG, "unregisterCallback() - mServerIf=" + mServerIf); |
| 408 | if (mService == null || mServerIf == 0) return; |
| 409 | |
| 410 | try { |
| 411 | mCallback = null; |
| 412 | mService.unregisterServer(mServerIf); |
| 413 | mServerIf = 0; |
| 414 | } catch (RemoteException e) { |
| 415 | Log.e(TAG,"",e); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 416 | } |
| 417 | } |
| 418 | |
| 419 | /** |
| 420 | * Returns a service by UUID, instance and type. |
| 421 | * @hide |
| 422 | */ |
| 423 | /*package*/ BluetoothGattService getService(UUID uuid, int instanceId, int type) { |
| 424 | for(BluetoothGattService svc : mServices) { |
| 425 | if (svc.getType() == type && |
| 426 | svc.getInstanceId() == instanceId && |
| 427 | svc.getUuid().equals(uuid)) { |
| 428 | return svc; |
| 429 | } |
| 430 | } |
| 431 | return null; |
| 432 | } |
| 433 | |
| 434 | /** |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 435 | * Initiate a connection to a Bluetooth GATT capable device. |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 436 | * |
| 437 | * <p>The connection may not be established right away, but will be |
| 438 | * completed when the remote device is available. A |
Andre Eisenbach | 3f36660 | 2013-03-08 18:42:24 -0800 | [diff] [blame] | 439 | * {@link BluetoothGattServerCallback#onConnectionStateChange} callback will be |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 440 | * invoked when the connection state changes as a result of this function. |
| 441 | * |
| 442 | * <p>The autoConnect paramter determines whether to actively connect to |
| 443 | * the remote device, or rather passively scan and finalize the connection |
| 444 | * when the remote device is in range/available. Generally, the first ever |
| 445 | * connection to a device should be direct (autoConnect set to false) and |
| 446 | * subsequent connections to known devices should be invoked with the |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 447 | * autoConnect parameter set to true. |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 448 | * |
| 449 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 450 | * |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 451 | * @param autoConnect Whether to directly connect to the remote device (false) |
| 452 | * or to automatically connect as soon as the remote |
| 453 | * device becomes available (true). |
| 454 | * @return true, if the connection attempt was initiated successfully |
| 455 | */ |
| 456 | public boolean connect(BluetoothDevice device, boolean autoConnect) { |
Andre Eisenbach | 3f36660 | 2013-03-08 18:42:24 -0800 | [diff] [blame] | 457 | if (DBG) Log.d(TAG, "connect() - device: " + device.getAddress() + ", auto: " + autoConnect); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 458 | if (mService == null || mServerIf == 0) return false; |
| 459 | |
| 460 | try { |
| 461 | mService.serverConnect(mServerIf, device.getAddress(), |
Ganesh Ganapathi Batta | b88fa82 | 2014-04-18 10:00:40 -0700 | [diff] [blame] | 462 | autoConnect ? false : true,mTransport); // autoConnect is inverse of "isDirect" |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 463 | } catch (RemoteException e) { |
| 464 | Log.e(TAG,"",e); |
| 465 | return false; |
| 466 | } |
| 467 | |
| 468 | return true; |
| 469 | } |
| 470 | |
| 471 | /** |
| 472 | * Disconnects an established connection, or cancels a connection attempt |
| 473 | * currently in progress. |
| 474 | * |
| 475 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 476 | * |
| 477 | * @param device Remote device |
| 478 | */ |
| 479 | public void cancelConnection(BluetoothDevice device) { |
| 480 | if (DBG) Log.d(TAG, "cancelConnection() - device: " + device.getAddress()); |
| 481 | if (mService == null || mServerIf == 0) return; |
| 482 | |
| 483 | try { |
| 484 | mService.serverDisconnect(mServerIf, device.getAddress()); |
| 485 | } catch (RemoteException e) { |
| 486 | Log.e(TAG,"",e); |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | /** |
| 491 | * Send a response to a read or write request to a remote device. |
| 492 | * |
| 493 | * <p>This function must be invoked in when a remote read/write request |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 494 | * is received by one of these callback methods: |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 495 | * |
| 496 | * <ul> |
| 497 | * <li>{@link BluetoothGattServerCallback#onCharacteristicReadRequest} |
| 498 | * <li>{@link BluetoothGattServerCallback#onCharacteristicWriteRequest} |
| 499 | * <li>{@link BluetoothGattServerCallback#onDescriptorReadRequest} |
| 500 | * <li>{@link BluetoothGattServerCallback#onDescriptorWriteRequest} |
| 501 | * </ul> |
| 502 | * |
| 503 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 504 | * |
| 505 | * @param device The remote device to send this response to |
| 506 | * @param requestId The ID of the request that was received with the callback |
| 507 | * @param status The status of the request to be sent to the remote devices |
| 508 | * @param offset Value offset for partial read/write response |
| 509 | * @param value The value of the attribute that was read/written (optional) |
| 510 | */ |
| 511 | public boolean sendResponse(BluetoothDevice device, int requestId, |
| 512 | int status, int offset, byte[] value) { |
Andre Eisenbach | 55d19e4 | 2014-07-18 14:38:36 -0700 | [diff] [blame] | 513 | if (VDBG) Log.d(TAG, "sendResponse() - device: " + device.getAddress()); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 514 | if (mService == null || mServerIf == 0) return false; |
| 515 | |
| 516 | try { |
| 517 | mService.sendResponse(mServerIf, device.getAddress(), requestId, |
| 518 | status, offset, value); |
| 519 | } catch (RemoteException e) { |
| 520 | Log.e(TAG,"",e); |
| 521 | return false; |
| 522 | } |
| 523 | return true; |
| 524 | } |
| 525 | |
| 526 | /** |
| 527 | * Send a notification or indication that a local characteristic has been |
| 528 | * updated. |
| 529 | * |
| 530 | * <p>A notification or indication is sent to the remote device to signal |
| 531 | * that the characteristic has been updated. This function should be invoked |
| 532 | * for every client that requests notifications/indications by writing |
| 533 | * to the "Client Configuration" descriptor for the given characteristic. |
| 534 | * |
| 535 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 536 | * |
| 537 | * @param device The remote device to receive the notification/indication |
| 538 | * @param characteristic The local characteristic that has been updated |
| 539 | * @param confirm true to request confirmation from the client (indication), |
| 540 | * false to send a notification |
Prerepa Viswanadham | ee3cc8b | 2014-08-13 14:46:58 -0700 | [diff] [blame] | 541 | * @throws IllegalArgumentException |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 542 | * @return true, if the notification has been triggered successfully |
| 543 | */ |
| 544 | public boolean notifyCharacteristicChanged(BluetoothDevice device, |
| 545 | BluetoothGattCharacteristic characteristic, boolean confirm) { |
Andre Eisenbach | 55d19e4 | 2014-07-18 14:38:36 -0700 | [diff] [blame] | 546 | if (VDBG) Log.d(TAG, "notifyCharacteristicChanged() - device: " + device.getAddress()); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 547 | if (mService == null || mServerIf == 0) return false; |
| 548 | |
| 549 | BluetoothGattService service = characteristic.getService(); |
| 550 | if (service == null) return false; |
| 551 | |
Prerepa Viswanadham | ee3cc8b | 2014-08-13 14:46:58 -0700 | [diff] [blame] | 552 | if (characteristic.getValue() == null) { |
| 553 | throw new IllegalArgumentException("Chracteristic value is empty. Use " |
| 554 | + "BluetoothGattCharacteristic#setvalue to update"); |
| 555 | } |
| 556 | |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 557 | try { |
| 558 | mService.sendNotification(mServerIf, device.getAddress(), |
Jakub Pawlowski | d75f512 | 2016-04-01 07:51:45 -0700 | [diff] [blame] | 559 | characteristic.getInstanceId(), confirm, |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 560 | characteristic.getValue()); |
| 561 | } catch (RemoteException e) { |
| 562 | Log.e(TAG,"",e); |
| 563 | return false; |
| 564 | } |
| 565 | |
| 566 | return true; |
| 567 | } |
| 568 | |
| 569 | /** |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 570 | * Add a service to the list of services to be hosted. |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 571 | * |
Andre Eisenbach | 061cfd0 | 2014-06-27 14:31:37 -0700 | [diff] [blame] | 572 | * <p>Once a service has been addded to the the list, the service and its |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 573 | * included characteristics will be provided by the local device. |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 574 | * |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 575 | * <p>If the local device has already exposed services when this function |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 576 | * is called, a service update notification will be sent to all clients. |
| 577 | * |
| 578 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 579 | * |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 580 | * @param service Service to be added to the list of services provided |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 581 | * by this device. |
| 582 | * @return true, if the service has been added successfully |
| 583 | */ |
| 584 | public boolean addService(BluetoothGattService service) { |
| 585 | if (DBG) Log.d(TAG, "addService() - service: " + service.getUuid()); |
| 586 | if (mService == null || mServerIf == 0) return false; |
| 587 | |
Jakub Pawlowski | d75f512 | 2016-04-01 07:51:45 -0700 | [diff] [blame] | 588 | mPendingService = service; |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 589 | |
| 590 | try { |
Jakub Pawlowski | d75f512 | 2016-04-01 07:51:45 -0700 | [diff] [blame] | 591 | mService.addService(mServerIf, service); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 592 | } catch (RemoteException e) { |
| 593 | Log.e(TAG,"",e); |
| 594 | return false; |
| 595 | } |
| 596 | |
| 597 | return true; |
| 598 | } |
| 599 | |
| 600 | /** |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 601 | * Removes a service from the list of services to be provided. |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 602 | * |
| 603 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 604 | * |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 605 | * @param service Service to be removed. |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 606 | * @return true, if the service has been removed |
| 607 | */ |
| 608 | public boolean removeService(BluetoothGattService service) { |
| 609 | if (DBG) Log.d(TAG, "removeService() - service: " + service.getUuid()); |
| 610 | if (mService == null || mServerIf == 0) return false; |
| 611 | |
| 612 | BluetoothGattService intService = getService(service.getUuid(), |
| 613 | service.getInstanceId(), service.getType()); |
| 614 | if (intService == null) return false; |
| 615 | |
| 616 | try { |
Jakub Pawlowski | d75f512 | 2016-04-01 07:51:45 -0700 | [diff] [blame] | 617 | mService.removeService(mServerIf, service.getInstanceId()); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 618 | mServices.remove(intService); |
| 619 | } catch (RemoteException e) { |
| 620 | Log.e(TAG,"",e); |
| 621 | return false; |
| 622 | } |
| 623 | |
| 624 | return true; |
| 625 | } |
| 626 | |
| 627 | /** |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 628 | * Remove all services from the list of provided services. |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 629 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 630 | */ |
| 631 | public void clearServices() { |
| 632 | if (DBG) Log.d(TAG, "clearServices()"); |
| 633 | if (mService == null || mServerIf == 0) return; |
| 634 | |
| 635 | try { |
| 636 | mService.clearServices(mServerIf); |
| 637 | mServices.clear(); |
| 638 | } catch (RemoteException e) { |
| 639 | Log.e(TAG,"",e); |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | /** |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 644 | * Returns a list of GATT services offered by this device. |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 645 | * |
| 646 | * <p>An application must call {@link #addService} to add a serice to the |
| 647 | * list of services offered by this device. |
| 648 | * |
| 649 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 650 | * |
| 651 | * @return List of services. Returns an empty list |
| 652 | * if no services have been added yet. |
| 653 | */ |
| 654 | public List<BluetoothGattService> getServices() { |
| 655 | return mServices; |
| 656 | } |
| 657 | |
| 658 | /** |
| 659 | * Returns a {@link BluetoothGattService} from the list of services offered |
| 660 | * by this device. |
| 661 | * |
| 662 | * <p>If multiple instances of the same service (as identified by UUID) |
| 663 | * exist, the first instance of the service is returned. |
| 664 | * |
| 665 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 666 | * |
| 667 | * @param uuid UUID of the requested service |
| 668 | * @return BluetoothGattService if supported, or null if the requested |
| 669 | * service is not offered by this device. |
| 670 | */ |
| 671 | public BluetoothGattService getService(UUID uuid) { |
| 672 | for (BluetoothGattService service : mServices) { |
| 673 | if (service.getUuid().equals(uuid)) { |
| 674 | return service; |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | return null; |
| 679 | } |
| 680 | |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 681 | |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 682 | /** |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 683 | * Not supported - please use {@link BluetoothManager#getConnectedDevices(int)} |
| 684 | * with {@link BluetoothProfile#GATT} as argument |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 685 | * |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 686 | * @throws UnsupportedOperationException |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 687 | */ |
| 688 | @Override |
| 689 | public int getConnectionState(BluetoothDevice device) { |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 690 | throw new UnsupportedOperationException("Use BluetoothManager#getConnectionState instead."); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | /** |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 694 | * Not supported - please use {@link BluetoothManager#getConnectedDevices(int)} |
| 695 | * with {@link BluetoothProfile#GATT} as argument |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 696 | * |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 697 | * @throws UnsupportedOperationException |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 698 | */ |
| 699 | @Override |
| 700 | public List<BluetoothDevice> getConnectedDevices() { |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 701 | throw new UnsupportedOperationException |
| 702 | ("Use BluetoothManager#getConnectedDevices instead."); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | /** |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 706 | * Not supported - please use |
| 707 | * {@link BluetoothManager#getDevicesMatchingConnectionStates(int, int[])} |
| 708 | * with {@link BluetoothProfile#GATT} as first argument |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 709 | * |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 710 | * @throws UnsupportedOperationException |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 711 | */ |
| 712 | @Override |
| 713 | public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 714 | throw new UnsupportedOperationException |
| 715 | ("Use BluetoothManager#getDevicesMatchingConnectionStates instead."); |
Ganesh Ganapathi Batta | 9908112 | 2013-02-05 15:28:33 -0800 | [diff] [blame] | 716 | } |
| 717 | } |