diff options
author | Jakub Pawlowski <jpawlowski@google.com> | 2016-04-01 07:51:45 -0700 |
---|---|---|
committer | Andre Eisenbach <eisenbach@google.com> | 2016-08-02 20:45:06 +0000 |
commit | 8d7adf284bab6292ef6f54435a726bf7293e2998 (patch) | |
tree | 9750d0316010b07ef65ffee63af7aa1b3f0f234a /framework/java/android/bluetooth/BluetoothGattServer.java | |
parent | f242df2f7ac42405d7d4d83bb745c2bc40088c13 (diff) |
GATT Server refactoring (2/4)
Bug: 27999121
Change-Id: Ia5f91298a4b01b62adebc8adc30f27f757259588
Diffstat (limited to 'framework/java/android/bluetooth/BluetoothGattServer.java')
-rw-r--r-- | framework/java/android/bluetooth/BluetoothGattServer.java | 195 |
1 files changed, 92 insertions, 103 deletions
diff --git a/framework/java/android/bluetooth/BluetoothGattServer.java b/framework/java/android/bluetooth/BluetoothGattServer.java index f4513405b1..d3c6444b1f 100644 --- a/framework/java/android/bluetooth/BluetoothGattServer.java +++ b/framework/java/android/bluetooth/BluetoothGattServer.java @@ -52,6 +52,7 @@ public final class BluetoothGattServer implements BluetoothProfile { private Object mServerIfLock = new Object(); private int mServerIf; private int mTransport; + private BluetoothGattService mPendingService; private List<BluetoothGattService> mServices; private static final int CALLBACK_REG_TIMEOUT = 10000; @@ -109,17 +110,37 @@ public final class BluetoothGattServer implements BluetoothProfile { * Service has been added * @hide */ - public void onServiceAdded(int status, int srvcType, - int srvcInstId, ParcelUuid srvcId) { - UUID srvcUuid = srvcId.getUuid(); - if (DBG) Log.d(TAG, "onServiceAdded() - service=" + srvcUuid - + "status=" + status); + public void onServiceAdded(int status, BluetoothGattService service) { + if (DBG) Log.d(TAG, "onServiceAdded() - handle=" + service.getInstanceId() + + " uuid=" + service.getUuid() + " status=" + status); + + if (mPendingService == null) + return; + + BluetoothGattService tmp = mPendingService; + mPendingService = null; + + // Rewrite newly assigned handles to existing service. + tmp.setInstanceId(service.getInstanceId()); + List<BluetoothGattCharacteristic> temp_chars = tmp.getCharacteristics(); + List<BluetoothGattCharacteristic> svc_chars = service.getCharacteristics(); + for (int i=0; i<svc_chars.size(); i++) { + BluetoothGattCharacteristic temp_char = temp_chars.get(i); + BluetoothGattCharacteristic svc_char = svc_chars.get(i); + + temp_char.setInstanceId(svc_char.getInstanceId()); + + List<BluetoothGattDescriptor> temp_descs = temp_char.getDescriptors(); + List<BluetoothGattDescriptor> svc_descs = svc_char.getDescriptors(); + for (int j=0; j<svc_descs.size(); j++) { + temp_descs.get(i).setInstanceId(svc_descs.get(i).getInstanceId()); + } + } - BluetoothGattService service = getService(srvcUuid, srvcInstId, srvcType); - if (service == null) return; + mServices.add(tmp); try { - mCallback.onServiceAdded((int)status, service); + mCallback.onServiceAdded((int)status, tmp); } catch (Exception ex) { Log.w(TAG, "Unhandled exception in callback", ex); } @@ -130,19 +151,15 @@ public final class BluetoothGattServer implements BluetoothProfile { * @hide */ public void onCharacteristicReadRequest(String address, int transId, - int offset, boolean isLong, int srvcType, int srvcInstId, - ParcelUuid srvcId, int charInstId, ParcelUuid charId) { - UUID srvcUuid = srvcId.getUuid(); - UUID charUuid = charId.getUuid(); - if (VDBG) Log.d(TAG, "onCharacteristicReadRequest() - " - + "service=" + srvcUuid + ", characteristic=" + charUuid); + int offset, boolean isLong, int handle) { + if (VDBG) Log.d(TAG, "onCharacteristicReadRequest() - handle=" + handle); BluetoothDevice device = mAdapter.getRemoteDevice(address); - BluetoothGattService service = getService(srvcUuid, srvcInstId, srvcType); - if (service == null) return; - - BluetoothGattCharacteristic characteristic = service.getCharacteristic(charUuid); - if (characteristic == null) return; + BluetoothGattCharacteristic characteristic = getCharacteristicByHandle(handle); + if (characteristic == null) { + Log.w(TAG, "onCharacteristicReadRequest() no char for handle " + handle); + return; + } try { mCallback.onCharacteristicReadRequest(device, transId, offset, characteristic); @@ -156,25 +173,15 @@ public final class BluetoothGattServer implements BluetoothProfile { * @hide */ public void onDescriptorReadRequest(String address, int transId, - int offset, boolean isLong, int srvcType, int srvcInstId, - ParcelUuid srvcId, int charInstId, ParcelUuid charId, - ParcelUuid descrId) { - UUID srvcUuid = srvcId.getUuid(); - UUID charUuid = charId.getUuid(); - UUID descrUuid = descrId.getUuid(); - if (VDBG) Log.d(TAG, "onCharacteristicReadRequest() - " - + "service=" + srvcUuid + ", characteristic=" + charUuid - + "descriptor=" + descrUuid); + int offset, boolean isLong, int handle) { + if (VDBG) Log.d(TAG, "onCharacteristicReadRequest() - handle=" + handle); BluetoothDevice device = mAdapter.getRemoteDevice(address); - BluetoothGattService service = getService(srvcUuid, srvcInstId, srvcType); - if (service == null) return; - - BluetoothGattCharacteristic characteristic = service.getCharacteristic(charUuid); - if (characteristic == null) return; - - BluetoothGattDescriptor descriptor = characteristic.getDescriptor(descrUuid); - if (descriptor == null) return; + BluetoothGattDescriptor descriptor = getDescriptorByHandle(handle); + if (descriptor == null) { + Log.w(TAG, "onDescriptorReadRequest() no desc for handle " + handle); + return; + } try { mCallback.onDescriptorReadRequest(device, transId, offset, descriptor); @@ -189,19 +196,15 @@ public final class BluetoothGattServer implements BluetoothProfile { */ public void onCharacteristicWriteRequest(String address, int transId, int offset, int length, boolean isPrep, boolean needRsp, - int srvcType, int srvcInstId, ParcelUuid srvcId, - int charInstId, ParcelUuid charId, byte[] value) { - UUID srvcUuid = srvcId.getUuid(); - UUID charUuid = charId.getUuid(); - if (VDBG) Log.d(TAG, "onCharacteristicWriteRequest() - " - + "service=" + srvcUuid + ", characteristic=" + charUuid); + int handle, byte[] value) { + if (VDBG) Log.d(TAG, "onCharacteristicWriteRequest() - handle=" + handle); BluetoothDevice device = mAdapter.getRemoteDevice(address); - BluetoothGattService service = getService(srvcUuid, srvcInstId, srvcType); - if (service == null) return; - - BluetoothGattCharacteristic characteristic = service.getCharacteristic(charUuid); - if (characteristic == null) return; + BluetoothGattCharacteristic characteristic = getCharacteristicByHandle(handle); + if (characteristic == null) { + Log.w(TAG, "onCharacteristicWriteRequest() no char for handle " + handle); + return; + } try { mCallback.onCharacteristicWriteRequest(device, transId, characteristic, @@ -216,28 +219,16 @@ public final class BluetoothGattServer implements BluetoothProfile { * Remote client descriptor write request. * @hide */ - public void onDescriptorWriteRequest(String address, int transId, - int offset, int length, boolean isPrep, boolean needRsp, - int srvcType, int srvcInstId, ParcelUuid srvcId, - int charInstId, ParcelUuid charId, ParcelUuid descrId, - byte[] value) { - UUID srvcUuid = srvcId.getUuid(); - UUID charUuid = charId.getUuid(); - UUID descrUuid = descrId.getUuid(); - if (VDBG) Log.d(TAG, "onDescriptorWriteRequest() - " - + "service=" + srvcUuid + ", characteristic=" + charUuid - + "descriptor=" + descrUuid); + public void onDescriptorWriteRequest(String address, int transId, int offset, + int length, boolean isPrep, boolean needRsp, int handle, byte[] value) { + if (VDBG) Log.d(TAG, "onDescriptorWriteRequest() - handle=" + handle); BluetoothDevice device = mAdapter.getRemoteDevice(address); - - BluetoothGattService service = getService(srvcUuid, srvcInstId, srvcType); - if (service == null) return; - - BluetoothGattCharacteristic characteristic = service.getCharacteristic(charUuid); - if (characteristic == null) return; - - BluetoothGattDescriptor descriptor = characteristic.getDescriptor(descrUuid); - if (descriptor == null) return; + BluetoothGattDescriptor descriptor = getDescriptorByHandle(handle); + if (descriptor == null) { + Log.w(TAG, "onDescriptorWriteRequest() no desc for handle " + handle); + return; + } try { mCallback.onDescriptorWriteRequest(device, transId, descriptor, @@ -318,6 +309,36 @@ public final class BluetoothGattServer implements BluetoothProfile { } /** + * Returns a characteristic with given handle. + * @hide + */ + /*package*/ BluetoothGattCharacteristic getCharacteristicByHandle(int handle) { + for(BluetoothGattService svc : mServices) { + for(BluetoothGattCharacteristic charac : svc.getCharacteristics()) { + if (charac.getInstanceId() == handle) + return charac; + } + } + return null; + } + + /** + * Returns a descriptor with given handle. + * @hide + */ + /*package*/ BluetoothGattDescriptor getDescriptorByHandle(int handle) { + for(BluetoothGattService svc : mServices) { + for(BluetoothGattCharacteristic charac : svc.getCharacteristics()) { + for(BluetoothGattDescriptor desc : charac.getDescriptors()) { + if (desc.getInstanceId() == handle) + return desc; + } + } + } + return null; + } + + /** * Close this GATT server instance. * * Application should call this method as early as possible after it is done with @@ -537,9 +558,7 @@ public final class BluetoothGattServer implements BluetoothProfile { try { mService.sendNotification(mServerIf, device.getAddress(), - service.getType(), service.getInstanceId(), - new ParcelUuid(service.getUuid()), characteristic.getInstanceId(), - new ParcelUuid(characteristic.getUuid()), confirm, + characteristic.getInstanceId(), confirm, characteristic.getValue()); } catch (RemoteException e) { Log.e(TAG,"",e); @@ -568,39 +587,10 @@ public final class BluetoothGattServer implements BluetoothProfile { if (DBG) Log.d(TAG, "addService() - service: " + service.getUuid()); if (mService == null || mServerIf == 0) return false; - mServices.add(service); + mPendingService = service; try { - mService.beginServiceDeclaration(mServerIf, service.getType(), - service.getInstanceId(), service.getHandles(), - new ParcelUuid(service.getUuid()), service.isAdvertisePreferred()); - - List<BluetoothGattService> includedServices = service.getIncludedServices(); - for (BluetoothGattService includedService : includedServices) { - mService.addIncludedService(mServerIf, - includedService.getType(), - includedService.getInstanceId(), - new ParcelUuid(includedService.getUuid())); - } - - List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics(); - for (BluetoothGattCharacteristic characteristic : characteristics) { - int permission = ((characteristic.getKeySize() - 7) << 12) - + characteristic.getPermissions(); - mService.addCharacteristic(mServerIf, - new ParcelUuid(characteristic.getUuid()), - characteristic.getProperties(), permission); - - List<BluetoothGattDescriptor> descriptors = characteristic.getDescriptors(); - for (BluetoothGattDescriptor descriptor: descriptors) { - permission = ((characteristic.getKeySize() - 7) << 12) - + descriptor.getPermissions(); - mService.addDescriptor(mServerIf, - new ParcelUuid(descriptor.getUuid()), permission); - } - } - - mService.endServiceDeclaration(mServerIf); + mService.addService(mServerIf, service); } catch (RemoteException e) { Log.e(TAG,"",e); return false; @@ -626,8 +616,7 @@ public final class BluetoothGattServer implements BluetoothProfile { if (intService == null) return false; try { - mService.removeService(mServerIf, service.getType(), - service.getInstanceId(), new ParcelUuid(service.getUuid())); + mService.removeService(mServerIf, service.getInstanceId()); mServices.remove(intService); } catch (RemoteException e) { Log.e(TAG,"",e); |