Skip to content

Characteristic Notifying

Dariusz Seweryn edited this page Dec 21, 2020 · 4 revisions

Bluetooth standard defines two variants of peripheral induced notifications about characteristic value changes:

  1. Notifications
    The standard way is simply broadcasting a value change of a specific characteristic. The Bluetooth specifies that only 20 bytes can be send as a notification.
  2. Indications
    Works the same as Notifications with exception that each broadcast is confirmed by the central.

It is possible that a particular characteristic supports both notifications and indications—in this situation notifications are picked.

Usage:

characteristic.monitor(
  listener: (error: ?Error, characteristic: ?Characteristic) => void,
  transactionId: ?TransactionId
): Subscription

or

device.monitorCharacteristicForService(
  serviceUUID: UUID,
  characteristicUUID: UUID,
  listener: (error: ?Error, characteristic: ?Characteristic) => void,
  transactionId: ?TransactionId
): Subscription

or

bleManager.monitorCharacteristicForDevice(
  deviceIdentifier: DeviceId,
  serviceUUID: UUID,
  characteristicUUID: UUID,
  listener: (error: ?Error, characteristic: ?Characteristic) => void,
  transactionId: ?TransactionId
): Subscription
  • deviceIdentifier: DeviceId—is obtained from device.id
  • serviceUUID: UUID—the UUID of service that contains the characteristic to setup notification/indication
  • characteristicUUID: UUID—the UUID of characteristic to setup notification/indication
  • listener: (error: ?Error, characteristic: ?Characteristic) => void—the listener for characteristic value changes. Will receive either error or characteristic with updated value but not both.
  • transactionId: TransactionId—optional TransactionId which can be used in bleManager.cancelTransaction() function.

Note:

  • Android:
    • Theoretically if the notification/indication is 20 bytes long the client is responsible to issue a characteristic read afterwards to read any data that could not fit into the limit. Android does not perform the read by default
    • On vanilla Android BLE API it is possible to choose wether notification or indication is setup if a characteristic supports both but the iOS does not allow to choose—that is why it is not possible to do so in this library
  • There is no need to write Client Characteristic Configuration descriptor at all. It is handled by the library/OS.
  • Client Characteristic Configuration descriptor should be present for the characteristic that you want to get notifications/indications from. More info about CCCD from Nordic Semiconductor Forums and ESP32 documentation