0

On Android when you want to receive BLE notifications for characteristic changes you use something like the following:

BluetoothGattCharacteristic characteristic = ...
gatt.setCharacteristicNotification(characteristic, true);
BluetoothGattDescriptor descriptor = 
   characteristic.getDescriptor(CHARACTERISTIC_UPDATE_NOTIFICATION_DESCRIPTOR_UUID);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
gatt.writeDescriptor(descriptor);

My question is: what is the gatt.setCharacteristicNotification() doing?

My assumption is that the above code represents:

  1. Tell Android to pay attention for notifications from characteristic X
  2. Tell the BLE device to start broadcasting change notifications for X

It seems logical, but I haven't seen it documented anywhere - instead the docs simply tell you to do what I wrote above. I'd like to confirm the purpose of setCharacteristicNotification() in this process.

SuperDeclarative
  • 1,609
  • 4
  • 18
  • 32
  • Yup your assumptions are correct. Notice if you don't do both it will not work :) One is reserving resources in the stack and one is actually sending a gatt registration for notifications on a characteristic. – Greg Giacovelli Sep 25 '15 at 06:46
  • What exactly do you mean by "reserving resources in the stack"? I figured the purpose of setCharacteristicNotification() was to tell the OS where to route incoming notifications? – SuperDeclarative Sep 25 '15 at 08:25
  • So while you can write the descriptor it will only tell the remote device to that you want to receive notifications on that characteristic (assuming it is notifiable/indication ready). And then within the OS you need to reserve some resources to, as you said, setup routing. The reason I state it as reserving resources is that it seems that some devices and implementations have a maximum number of notifications on characteristics that the OS will route within a connection. So by setting up to receive the callbacks you are reserving a spot for your callback – Greg Giacovelli Sep 26 '15 at 03:26
  • Do you happen to have any doc references or implementation code samples or anything else official that you can post as an answer so that I can mark it is as accepted? – SuperDeclarative Sep 26 '15 at 08:12
  • 1
    you can refer to the Android API Guide: http://developer.android.com/guide/topics/connectivity/bluetooth-le.html#notification – smertrios Sep 26 '15 at 20:49
  • I guess we'll just go without an accepted answer then, because I don't consider those docs to address the question at all. In fact, it was the failure of that documentation that lead me to ask it in the first place. In fact that documentation doesn't provide any greater detail than the code example in my question. Hopefully someone will be able to post references to material that directly verifies these assumptions. – SuperDeclarative Sep 27 '15 at 08:18
  • 1
    Just to clarify, you are asking what the above example code does? You have assumptions and others confirm your assumption. Are you asking for another example? I don't know what else an example would show other than the code you submitted. At this point it might be beneficial to read the source code of bluedroid and the bluetooth apis in aosp. – Greg Giacovelli Sep 28 '15 at 21:29
  • Do you happen to know where I should start in the bluedroid source to see notification management? I've already gone through the Android APIs on grepcode but it hits stubs pretty quickly. – SuperDeclarative Sep 29 '15 at 23:33

0 Answers0