This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How to know if the Notification/Indication of a Characteristic is enabled?

I would like to know if the Notification/Indication of a Characteristic is enabled. I suppose this data is stored in a CCCD on the stack. I have not found any SVC instruction to read this info from the BLE stack.

Where can I access this info?

Thanks for your help.

Parents
  • There are basically 3 ways you can handle this:

    1. Don't care, just try to send a notification/indication and check the error code you get back from sd_ble_gatts_hvx(). If it is NRF_ERROR_INVALID_STATE, notifications/indications are not enabled.
    2. Make a handler for the BLE_GATTS_EVT_WRITE. When the CCCD is written, you'll receive such event, with the handle set to the CCCD handle and the value either 0x0001 (Notification) or 0x0002 (Indication). Beware that you will not receive such event when reconnecting to a previously bonded Central, since CCCD values are retained over the lifetime of a bond.
    3. Read the value of the CCCD using sd_ble_gatts_value_get(), and check whether it is 0x0001 (Notification) or 0x0002 (Indication).

    Of these, I'd recommend using method 1 or method 2. If you only need to send a notification once in a while, it might be ok to just ignore the error with method 1, but you should most definitely not busy-loop on this error. If you need to transfer lots of data, you should be certain that the CCCD has been enabled before starting the transfer.

    Edit: Clarify recommendations, and when they don't apply.

Reply
  • There are basically 3 ways you can handle this:

    1. Don't care, just try to send a notification/indication and check the error code you get back from sd_ble_gatts_hvx(). If it is NRF_ERROR_INVALID_STATE, notifications/indications are not enabled.
    2. Make a handler for the BLE_GATTS_EVT_WRITE. When the CCCD is written, you'll receive such event, with the handle set to the CCCD handle and the value either 0x0001 (Notification) or 0x0002 (Indication). Beware that you will not receive such event when reconnecting to a previously bonded Central, since CCCD values are retained over the lifetime of a bond.
    3. Read the value of the CCCD using sd_ble_gatts_value_get(), and check whether it is 0x0001 (Notification) or 0x0002 (Indication).

    Of these, I'd recommend using method 1 or method 2. If you only need to send a notification once in a while, it might be ok to just ignore the error with method 1, but you should most definitely not busy-loop on this error. If you need to transfer lots of data, you should be certain that the CCCD has been enabled before starting the transfer.

    Edit: Clarify recommendations, and when they don't apply.

Children
No Data
Related