Search code examples
androidbluetooth-lowenergyandroid-5.0-lollipopandroid-bluetooth

How do you add Characteristics using BluetoothLeAdvertiser?


Upon inspection of the Android BluetoothLE Peripheral API I was wondering how you could do the following things?

  • How do you add Characterisitics (with Permissions like Notify, Read, Write,...)
  • How do you react to write requests?
  • How do you detect reads of your data?
  • Can you handle the subscription of characteristics?
  • How do you send updates to a central that subscribed to one of your characterisitics?

  • Why has google not given out any extensive documentation of these features?


Solution

  • You are looking for Bluetooth Gatt Server role, BluetoothLeAdvertiser can not include characteristics. read for BluetoothGattServer Android

    also tried answering all of your questions.

    • How do you add Characteristics (with Permissions like Notify, Read, Write,...)

    You do not add characteristics directly, you have to add characteristic into Service, you can give above permissions(read,write,notify) to chars. read this BluetoothGattCharacteristic

    • How do you react to write requests?

    you gets your callback method(onWriteRequest something) called by system, then you change your data and you should call sendResponse method, read the link.

    • How do you detect reads of your data?

    onReadRequest method gets called by system. read about gatt server callbacks on the android docs(link shared already)

    • Can you handle the subscription of characteristics?

    Yes, You can. add chars into service, register them with gattServer(read above), and if monitor role asks for notification registration, you send notification, each time your data changes.

    • How do you send updates to a central that subscribed to one of your characterisitics?

    you call notifyCharacteristicChanged

    • Why has google not given out any extensive documentation of these features?

    BLE is new, android is still struggling to have stable solution first, no comments on why Google not given.