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

how to handle NRF_ERROR_INVALID_STATE of sd_ble_gap_scan_start?

Hi all,

I develop a central application, which manages multiple peripheral connections. The application often has to stop and start scanning again. In rare cases the function sd_ble_gap_scan_start() returns with “NRF_ERROR_INVALID_STATE” so the following APP_ERROR_CHECK() causes a software reset. In which cases does it come to an “invalid state”? How should I react to this? Shall I ignore this error and try to start scanning again later?

  • Hi

    You will get return value of NRF_ERROR_INVALID_STATE if you start scanning and are still in a "connecting phase" with a peripheral and are not connected yet. As in the nRF51 SDK ble_app_multilink_central example, the scan_start is only issued as soon as the DM_EVT_CONNECTION is received, which states that you are connected, so make sure you do the same.

    Another possible reason for NRF_ERROR_INVALID_STATE return value if you try to start scanning when there is an ongoing flash operation. The scan_start function prevents this by checking the pstorage status before starting the scan. If there is an ongoing flash operation, a flag is set in order to start scanning when the flash operation is finished. If you look at the on_sys_evt function, scan_start is performed if a flash error occurs and if the flag is set, but you really should start scanning both when flash error occurs and when flash operation completes, i.e. for case NRF_EVT_FLASH_OPERATION_ERROR and for case NRF_EVT_FLASH_OPERATION_SUCCESS

Related