sandeepmistry / arduino-BLEPeripheral

An Arduino library for creating custom BLE peripherals with Nordic Semiconductor's nRF8001 or nR51822.
MIT License
463 stars 180 forks source link

NRF5: CCCDs aren't persisted for bonded peers #279

Open jfedor2 opened 3 years ago

jfedor2 commented 3 years ago

Client Characteristic Configuration Descriptors are the "subscription bits" for notifications and indications. When a client wants to be notified of a characteristic change, it writes a 1 to the CCCD, therefore subscribing to the changes on the characteristic.

BLE spec says the values of these CCCDs have to be persisted (on a per-client basis) for peers that are bonded. For example Windows 10 only sets the CCCDs the first time the devices connect and so if they are not cached on the device, the client thinks it's subscribed, but it's not getting any notifications. This results in a situation where, say, a bonded (paired) mouse connects for the second time and the code sends some HID reports, but they are never actually delivered to the other side, because the BLE stack thinks the client is not subscribed to the relevant characteristic. The effect is that the mouse only works the first time it's connected and then the bonding state needs to be cleared (and the devices need to bond again) before it can work again.

macOS writes the CCCD every time the device is connected so the problem doesn't show up on mac.