@Override public BluetoothGattDescriptor apply(BluetoothGattCharacteristic bluetoothGattCharacteristic) { final BluetoothGattDescriptor descriptor = bluetoothGattCharacteristic.getDescriptor(descriptorUuid); if (descriptor == null) { throw new BleDescriptorNotFoundException(descriptorUuid); } return descriptor; } });
@Override public BluetoothGattDescriptor apply(BluetoothGattCharacteristic bluetoothGattCharacteristic) { BluetoothGattDescriptor bluetoothGattDescriptor = bluetoothGattCharacteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG_UUID); if (bluetoothGattDescriptor == null) { //adding notification descriptor if not present bluetoothGattDescriptor = new BluetoothGattDescriptor(CLIENT_CHARACTERISTIC_CONFIG_UUID, 0); bluetoothGattCharacteristic.addDescriptor(bluetoothGattDescriptor); } return bluetoothGattDescriptor; } });
protected static final UUID CHARACTERISTIC_UPDATE_NOTIFICATION_DESCRIPTOR_UUID = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"); public boolean setCharacteristicNotification(BluetoothDevice device, UUID serviceUuid, UUID characteristicUuid, boolean enable) { if (IS_DEBUG) Log.d(TAG, "setCharacteristicNotification(device=" + device.getName() + device.getAddress() + ", UUID=" + characteristicUuid + ", enable=" + enable + " )"); BluetoothGatt gatt = mGattInstances.get(device.getAddress()); //I just hold the gatt instances I got from connect in this HashMap BluetoothGattCharacteristic characteristic = gatt.getService(serviceUuid).getCharacteristic(characteristicUuid); gatt.setCharacteristicNotification(characteristic, enable); BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CHARACTERISTIC_UPDATE_NOTIFICATION_DESCRIPTOR_UUID); descriptor.setValue(enable ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : new byte[] { 0x00, 0x00 }); return gatt.writeDescriptor(descriptor); //descriptor write operation successfully started? }
@NonNull private static Completable writeClientCharacteristicConfig( final BluetoothGattCharacteristic bluetoothGattCharacteristic, final DescriptorWriter descriptorWriter, final byte[] value ) { final BluetoothGattDescriptor descriptor = bluetoothGattCharacteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG_UUID); if (descriptor == null) { return Completable.error(new BleCannotSetCharacteristicNotificationException( bluetoothGattCharacteristic, BleCannotSetCharacteristicNotificationException.CANNOT_FIND_CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR, null )); } return descriptorWriter.writeDescriptor(descriptor, value) .onErrorResumeNext(new Function<Throwable, CompletableSource>() { @Override public CompletableSource apply(Throwable throwable) { return Completable.error(new BleCannotSetCharacteristicNotificationException( bluetoothGattCharacteristic, BleCannotSetCharacteristicNotificationException.CANNOT_WRITE_CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR, throwable )); } }); } }
characteristic.getDescriptor(clientCharacteristicConfigDescriptorUuid) == null ? NotificationSetupMode.COMPAT : NotificationSetupMode.DEFAULT;
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) { if (mBluetoothAdapter == null || mBluetoothGatt == null) { Log.w(TAG, "BluetoothAdapter not initialized"); return; } mBluetoothGatt.setCharacteristicNotification(characteristic, enabled); BluetoothGattDescriptor descriptor = characteristic.getDescriptor( UUID.fromString("00002902-0000-1000-8000-00805f9b34fb")); descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); mBluetoothGatt.writeDescriptor(descriptor); }
@Override public boolean isClientCompatible(final Intent intent, final BluetoothGatt gatt) { final BluetoothGattService dfuService = gatt.getService(BUTTONLESS_DFU_SERVICE_UUID); if (dfuService == null) return false; final BluetoothGattCharacteristic characteristic = dfuService.getCharacteristic(BUTTONLESS_DFU_UUID); if (characteristic == null || characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG) == null) return false; mButtonlessDfuCharacteristic = characteristic; return true; }
@Override public boolean isClientCompatible(final Intent intent, final BluetoothGatt gatt) { final BluetoothGattService dfuService = gatt.getService(BUTTONLESS_DFU_SERVICE_UUID); if (dfuService == null) return false; final BluetoothGattCharacteristic characteristic = dfuService.getCharacteristic(BUTTONLESS_DFU_UUID); if (characteristic == null || characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG) == null) return false; mButtonlessDfuCharacteristic = characteristic; return true; }
@Override public boolean isClientCompatible(final Intent intent, final BluetoothGatt gatt) { final BluetoothGattService dfuService = gatt.getService(EXPERIMENTAL_BUTTONLESS_DFU_SERVICE_UUID); if (dfuService == null) return false; final BluetoothGattCharacteristic characteristic = dfuService.getCharacteristic(EXPERIMENTAL_BUTTONLESS_DFU_UUID); if (characteristic == null || characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG) == null) return false; mButtonlessDfuCharacteristic = characteristic; return true; }
@Override public boolean isClientCompatible(final Intent intent, final BluetoothGatt gatt) { final BluetoothGattService dfuService = gatt.getService(DFU_SERVICE_UUID); if (dfuService == null) return false; final BluetoothGattCharacteristic characteristic = dfuService.getCharacteristic(DFU_CONTROL_POINT_UUID); if (characteristic == null || characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG) == null) return false; mControlPointCharacteristic = characteristic; mPacketCharacteristic = dfuService.getCharacteristic(DFU_PACKET_UUID); return mPacketCharacteristic != null; }
private void setCharacteristicNotification(BluetoothGatt bluetoothgatt, BluetoothGattCharacteristic bluetoothgattcharacteristic, boolean flag) { bluetoothgatt.setCharacteristicNotification(bluetoothgattcharacteristic, flag); BluetoothGattDescriptor descriptor = bluetoothgattcharacteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG); if (descriptor != null) { descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); bluetoothgatt.writeDescriptor(descriptor); } }
@Override public boolean isClientCompatible(final Intent intent, final BluetoothGatt gatt) { final BluetoothGattService dfuService = gatt.getService(DFU_SERVICE_UUID); if (dfuService == null) return false; final BluetoothGattCharacteristic characteristic = dfuService.getCharacteristic(DFU_CONTROL_POINT_UUID); if (characteristic == null || characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG) == null) return false; mControlPointCharacteristic = characteristic; mPacketCharacteristic = dfuService.getCharacteristic(DFU_PACKET_UUID); return mPacketCharacteristic != null; }
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) { Log.i(TAG, "Characteristic setting notification"); if (mBluetoothGatt != null) { mBluetoothGatt.setCharacteristicNotification(characteristic, enabled); BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(HM10Attributes.CLIENT_CHARACTERISTIC_CONFIG)); Log.i(TAG, "Descriptor found: " + descriptor.getUuid()); descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); mBluetoothGatt.writeDescriptor(descriptor); } }
public void setCharacteristicIndication(BluetoothGattCharacteristic characteristic, boolean enabled) { Log.i(TAG, "Characteristic setting indication"); if (mBluetoothGatt != null) { mBluetoothGatt.setCharacteristicNotification(characteristic, enabled); BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(HM10Attributes.CLIENT_CHARACTERISTIC_CONFIG)); Log.i(TAG, "Descriptor found: " + descriptor.getUuid()); descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE); mBluetoothGatt.writeDescriptor(descriptor); } }
public void setCharacteristicIndication(BluetoothGattCharacteristic characteristic, boolean enabled) { Log.i(TAG, "Characteristic setting indication"); if (mBluetoothGatt != null) { mBluetoothGatt.setCharacteristicNotification(characteristic, enabled); BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(HM10Attributes.CLIENT_CHARACTERISTIC_CONFIG)); Log.i(TAG, "Descriptor found: " + descriptor.getUuid()); descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE); mBluetoothGatt.writeDescriptor(descriptor); } }
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) { Log.i(TAG, "Characteristic setting notification"); if (mBluetoothGatt != null) { mBluetoothGatt.setCharacteristicNotification(characteristic, enabled); BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(HM10Attributes.CLIENT_CHARACTERISTIC_CONFIG)); Log.i(TAG, "Descriptor found: " + descriptor.getUuid()); descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); mBluetoothGatt.writeDescriptor(descriptor); } }
@Test public void testCharacteristicNotificationFail() { when(gatt.getService(eq(serviceUUID))).thenReturn(gattService); when(gattService.getCharacteristic(characteristicUUID)).thenReturn(gattCharacteristic); when(gattCharacteristic.getDescriptor(eq(clientCharacteristicConfig))).thenReturn(gattDescriptor); when(gatt.setCharacteristicNotification(Mockito.eq(gattCharacteristic), Mockito.anyBoolean())).thenReturn(false); subscribeCommand.execute(device, operationCommandObserver, gatt); verifyCommandFail(subscribeCommand, BluetoothGatt.GATT_FAILURE); }
public void setCharacteristicIndication(BluetoothGattCharacteristic characteristic, boolean enabled) { Log.i(TAG, "Characteristic setting notification"); mBluetoothGatt.setCharacteristicNotification(characteristic, enabled); Log.i(TAG, "UUID FOUND: " + characteristic.getUuid()); BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(HM10Attributes.CLIENT_CHARACTERISTIC_CONFIG)); Log.i(TAG, "Descriptor found: " + descriptor.getUuid()); descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE); mBluetoothGatt.writeDescriptor(descriptor); }
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) { Log.i(TAG, "Characteristic setting notification"); mBluetoothGatt.setCharacteristicNotification(characteristic, enabled); Log.i(TAG, "UUID FOUND: " + characteristic.getUuid()); BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(HM10Attributes.CLIENT_CHARACTERISTIC_CONFIG)); Log.i(TAG, "Descriptor found: " + descriptor.getUuid()); descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); mBluetoothGatt.writeDescriptor(descriptor); }
public void setCharacteristicIndication(BluetoothGattCharacteristic characteristic, boolean enabled) { Log.i(TAG, "Characteristic setting notification"); mBluetoothGatt.setCharacteristicNotification(characteristic, enabled); Log.i(TAG, "UUID FOUND: " + characteristic.getUuid()); BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(HM10Attributes.CLIENT_CHARACTERISTIC_CONFIG)); Log.i(TAG, "Descriptor found: " + descriptor.getUuid()); descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE); mBluetoothGatt.writeDescriptor(descriptor); }