I have a problem understanding notifications/subscriptions in the BLE context.
I have two devices, Central (nRF5340) and Peripheral (nRF52832). My peripheral provides a GATT profile. The Central establishes a connection to the Peripheral.
I have implemented the following sequence:
- Central establishes connection to Peripheral
- Central changes security level to L4 (cause I know that the characteristics of my peripheral expect this security level)
- If successful, Central performs GATT discovery
- If successful, the Central subscribes to a specific characteristic
- The Peripheral sends a one-time notification update as soon as the subscription is received (value 1 written to CCC --> received in BT_GATT_CCC cfg_changed callback)
Problem:
- If both devices are already bonded and the security level is changed to L4, in the Peripheral, the value of CCC is automatically retrieved from the bonding information and applied (even before the Central executes a bt_gatt_subscribe). This happens in the 'bt_gatt_encrypt_change' function.
- This will call the BT_GATT_CCC cfg_changed callback directly.
- This means that the notification sent once during the first subscribe cannot be interpreted by the Central, as on Central side the callback has not yet been registered (which happens during bt_gatt_subscribe).
It appears to me that this behavior, whereby the CCC value is loaded from the bonding information, is not configurable. However, it is unclear to me how the central should perform the subscription without a GATT discovery and a corresponding security level in time, since the handle (characteristic) is required for this.
Can anyone assist me with this?