3

This question is not about Android notificatinos, but BLE notifications (as the title may hint)

I have got basic BLE peripheral mode working on Android-L

Is there any way to implement BLE notifications in Android-L preview. I could do some thing like the following to make a charecteritic be able to notify, but trying to listen for

BluetoothGattCharacteristic firstServiceChar = new BluetoothGattCharacteristic(
        UUID.fromString(serviceOneCharUuid),
                BluetoothGattCharacteristic.PROPERTY_NOTIFY, BluetoothGattCharacteristic.PERMISSION_READ );

But in LightBlue app on iOS I cannot subscribe to this characteristic. Apprently there is no API that could be use to respond to the calls when a char is subscribed (like there are in iOS)

Kindly share your code if you have successfully enabled BLE notifications on Android-L

geoaxis
  • 1,480
  • 6
  • 25
  • 46
  • I made a BluetoothGattDescriptor `BluetoothGattDescriptor gD = new BluetoothGattDescriptor(UUID.fromString("00002902-0000-1000-8000-00805F9B34FB"), BluetoothGattDescriptor.PERMISSION_WRITE | BluetoothGattDescriptor.PERMISSION_READ);` and added it to my characteristic `myNotifyCharacteristic.addDescriptor(gD);`. This allowed me to try and subscribe but it's not working when I call `myGattServer.notifyCharacteristicChanged(sendToThisGuy, myNotifyCharacteristic, false);`. I'll keep plugging at it. – ludwigmace Aug 26 '14 at 13:38

1 Answers1

22

On top of what OP has done:

BluetoothGattCharacteristic firstServiceChar = new BluetoothGattCharacteristic(UUID.fromString(serviceOneCharUuid), BluetoothGattCharacteristic.PROPERTY_NOTIFY, BluetoothGattCharacteristic.PERMISSION_READ )

The next thing is to add a Client Characteristic Configuration descriptor (UUID is the 128 bit version of the 16 bit 0x2902 using the Bluetooth base UUID), so that the connected device can tell yours that it wants notifications (or indications), and then add that descriptor to your characteristic:

BluetoothGattDescriptor gD = new BluetoothGattDescriptor(UUID.fromString("00002902-0000-1000-8000-00805F9B34FB"), BluetoothGattDescriptor.PERMISSION_WRITE | BluetoothGattDescriptor.PERMISSION_READ);

firstServiceChar.addDescriptor(gD);

That UUID is from the Bluetooth spec. Apparently a device subscribes to notifications by updating this descriptor, so you've got to handle this in your BluetoothGattServerCallback by overriding onDescriptorWriteRequest:

@Override
public void onDescriptorWriteRequest (BluetoothDevice device, int requestId, BluetoothGattDescriptor descriptor, boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) {

    btClient = device;  // perhaps add to some kind of collection of devices to update?

    // now tell the connected device that this was all successfull
    btGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, value);

}

Now update your value and notify the connectee:

firstServiceChar.setValue("HI");
btGattServer.notifyCharacteristicChanged(btClient, firstServiceChar, false);

Hopefully this quick and dirty code will help, because it was OP's code that I used in the first place to even get basic peripheral mode working :)

ludwigmace
  • 692
  • 4
  • 10
  • thanks, will have to try it out next week. Don't have access to any Android device till then. If it works out then there was no need for a feature request I posted. https://code.google.com/p/android-developer-preview/issues/detail?id=979 – geoaxis Aug 27 '14 at 20:10
  • 1
    That's a good request though - it'd be nice if they abstracted out the whole Descriptor deal for notifications like iOS does. – ludwigmace Aug 27 '14 at 21:08
  • 2
    This answer is pure gold. This requirement is completely undocumented. My iOS device was reporting 'attribute not found' before, and now does not return an error. – Ying Mar 15 '17 at 12:57
  • 2
    This should be the correct answer. I was working on the iOS central _and_ the Android peripheral. Was seeing the CB error: `Error Domain=CBATTErrorDomain Code=10 "The attribute could not be found."` in the `didUpdateNotificationStateForCharacteristic` delegate method. Adding the `0x2902` CCCD to my notify characteristic _and_ sending the response from `onDescriptorWriteRequest` are both necessary. – Daniel J Feb 03 '21 at 23:56