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

sd_ble_gatts_hvx returns unexpected value when indication not enabled

The API for sd_ble_gatts_hvx says that NRF_ERROR_INVALID_STATE is returned when no indications are enabled.

 * @retval ::NRF_ERROR_INVALID_STATE One or more of the following is true:
 *                                   - Invalid Connection State
 *                                   - Notifications and/or indications not enabled in the CCCD
 *                                   - An ATT_MTU exchange is ongoing

However, when I call sd_ble_gatts_hvx in this scenario I receive BLE_ERROR_GATTS_SYS_ATTR_MISSING

Is this a bug or inaccurate documentation? Something else?

Parents Reply Children
  • Thank you, that explains what is going on.

    Is there a best practice for this scenario? Should sd_ble_gatts_sys_attr_set() be called on connection to ensure attribute tables are in a defined state or is handling both scenarios of an undefined table (where BLE_ERROR_GATTS_SYS_ATTR_MISSING is returned) and a defined table (where NRF_ERROR_INVALID_STATE is returned) preferred?

  • Hi, 

    There is a BLE_GATTS_EVT_SYS_ATTR_MISSING event that is generated when you try to read the CCCD and this event is usually handled inside the Peer Manager. So when you do not initialize the Peer Manager, then this event will not be handled correctly. If you do not use the Peer Manager in your code, then you can simply add the following snippet to on_ble_evt() in main.c. You also can take a look at the main.c in the ble_app_blinky example of how to handle that. 

            case BLE_GATTS_EVT_SYS_ATTR_MISSING:
                // No system attributes have been stored.
                err_code = sd_ble_gatts_sys_attr_set(m_conn_handle, NULL, 0, 0);
                APP_ERROR_CHECK(err_code);
                break;
    

    -Amanda H.

  • Thank you, I think I have my head wrapped around how this works now. I also used the ble_app_hrs example to understand how an app using the Peer Manager deals with this. I've modeled my app after the HRS since I am using the PM. As I can't guarantee a CCCD is read first, I will plan to expect either BLE_GATTS_EVT_SYS_ATTR_MISSING or NRF_ERROR_INVALID_STATE.

Related