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

Disconnect from Peripheral and stop Advertising

Hello ,

I'm working on custom board, I'm trying to disconnect from the Central (Andriod app) when the battery is below certain level and stop re-advertising. Below is the piece of code i used:

/* Disconnect and then disable advertising */

if (m_conn_handle != BLE_CONN_HANDLE_INVALID) { sd_ble_gap_disconnect(m_conn_handle,BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION); }

uint32_t err_code = sd_ble_gap_adv_stop();

APP_ERROR_CHECK(err_code);

I'm trying to disconnect and disable advertising but it doesn't work in some cases, I don't really know why.

I use SDK v11 with S132 v2.0.0 (nRF52)

  • Hi,

    If you call sd_ble_gap_adv_stop() when you are not advertising, the function will return NRF_ERROR_INVALID_STATE. The APP_ERROR_CHECK() will therefore call the error handling function. Don't call the sd_ble_gap_adv_stop() when you are not advertising, or ignore the invalid state error message. The sd_ble_gap_disconnect() function will also return NRF_ERROR_INVALID_STATE if you try to call it when e.g. a disconnection is already in progress. Try this code instead, and see if it solves your issues:

    uint32_t err_code;
    
    if (m_conn_handle != BLE_CONN_HANDLE_INVALID)
    {
        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);
        }
    }
    
    err_code = sd_ble_gap_adv_stop();
    if (err_code != NRF_ERROR_INVALID_STATE)
    {
        APP_ERROR_CHECK(err_code);
    }
    
Related