1

Reading the documentation, one would think that setCharacteristicNotification enables notifications for a BLE characteristic:

Enable or disable notifications/indications for a given characteristic.

But this method doesn't seem to do this? Reading the BLE documentation on receiving BLE notifications, it turns out to be a multi-step process where you have to call this method and then write a file into a descriptor.

If this is the case, then what does setCharacteristicNotification by itself do?

rityzmon
  • 1,945
  • 16
  • 26

2 Answers2

3

The descriptor write is needed in order to tell the remote device to send notifications. setCharactersticNotification only tells the Bluetooth stack that it should forward any received notification to the app.

Emil
  • 16,784
  • 2
  • 41
  • 52
  • 1
    Well, that is definitely not what the documentation says. I suppose this is a case of extremely poor documentation that no one has bothered to fix. – rityzmon Jan 01 '17 at 16:12
-1

Interesting read at Why does setCharacteristicNotification() not actually enable notifications? . The answerers there dig through the source code and docs to find that:

"setCharacteristicNotification only prepares the local service to receive notifications."

I would suggest a wrapper function for set notifications, because, in addition to the documentation being not-so-clear, it is a confusing concept to enable notification reception locally as well as enable notification sending on the peripheral. I would suggest something like what the kind answerers at Enabling Bluetooth characteristic Notification in Android (Bluetooth Low Energy ) Not Working use:

public boolean setCharacteristicNotification(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic characteristic,boolean enable) {
    Logger.d("setCharacteristicNotification");
    bluetoothGatt.setCharacteristicNotification(characteristic, enable);
    BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CHARACTERISTIC_UPDATE_NOTIFICATION_DESCRIPTOR_UUID);
    descriptor.setValue(enable ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : new byte[]{0x00, 0x00});
    return bluetoothGatt.writeDescriptor(descriptor); //descriptor write operation successfully started?

}
John Doe
  • 165
  • 1
  • 4
  • 15