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

NRF_ERROR_INVALID_STATE cases

Hello,

I am using nrf5 sdk 12, and I have read the documentation in ble_gap.h for NRF_ERROR_INVALID_STATE and I have seen a few questions on this forum, and what I understand is NRF_ERROR_INVALID_STATE is an error which needs to be handled according which API resulted in this error.

But I have seen this code in the SDK:

err_code = ble_nus_string_send(&m_nus, data_array, index);
            if (err_code != NRF_ERROR_INVALID_STATE)
            {
                APP_ERROR_CHECK(err_code);
            }

and this:

  case BSP_EVENT_DISCONNECT:
        err_code = sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
        if (err_code != NRF_ERROR_INVALID_STATE)
        {
            APP_ERROR_CHECK(err_code);
        }
        break;

in main.c

My question is, why aren't we checking if the function is SUCCESS over here, why are we checking if it returns NRF_ERROR_INVALID_STATE? It seems we wish to ignore this error, why is this so?

Parents
  • FormerMember
    0 FormerMember

    NRF_ERROR_INVALID_STATE means that in this current state, the requested operation is not allowed. However, at a later point in time, for instance, the operation may be allowed. Therefore, it makes sense to treat that error separately, without APP_ERROR_CHECK(), and avoid an assert. NRF_ERROR_INVALID_STATE is therefore an error code that can occur without "anything" being wrong.

    For sd_ble_gatts_hvx(..), transmission of notifications/indications, NRF_ERROR_INVALID_STATE occurs when 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

  • FormerMember
    0 FormerMember in reply to FormerMember

    You should keep track of the connection state (connected or not) and if notifications/indications are enabled or not. You will then know what the reason for the error code is.

Reply Children
No Data
Related