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

Enabling and disabling Bluetooth

Hi there,

I'm currently working on a project in which I need to be able to deactivate Bluetooth advertising at the press of a button. I am using SDK 15.3 and have built a custom service from the ble_app_template example, following this tutorial. I have been able to get Bluetooth communication up and running, but am having difficulty deactivating it without causing the script to fail. To clarify, all I need it to do is to disable communication, not necessarily disable the entire module.

Here is the way I am currently enabling Bluetooth (working):

/**@brief Function for starting advertising.
 */
static void advertising_start(bool erase_bonds)
{
    if (erase_bonds == true)
    {
        delete_bonds();
        // Advertising is started by PM_EVT_PEERS_DELETED_SUCEEDED event
    }
    else
    {
        ret_code_t err_code = ble_advertising_start(&m_advertising, BLE_ADV_MODE_FAST);

        APP_ERROR_CHECK(err_code);
    }
}

And this is the function I'm trying to use to disable it:

static void advertising_disconnect(void)
{
    ret_code_t err_code;

    // If connected, disconnect
    if (m_conn_handle != BLE_CONN_HANDLE_INVALID)
    {
        err_code = sd_ble_gap_disconnect(m_conn_handle,
                                         BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
        APP_ERROR_CHECK(err_code);
    }
    // Stop advertising
    err_code = sd_ble_gap_adv_stop(m_advertising.adv_handle);
    APP_ERROR_CHECK(err_code);
}

When the button is pressed while no connected has been established, it hangs on sd_ble_gap_adv_stop() and will not reach the APP_ERROR_CHECK.

When the button is pressed while connected to a device, sd_ble_gap_disconnect() will work, but an error is generated at sd_ble_gap_adv_stop().

Is there a specific file I need to include in order to use sd_ble_gap_adv_stop()? Or maybe another way to achieve this?

*I have also tried using ble_advertising_start() to enter IDLE mode as a way of disabling communication but have not been able to get this to work either.

Any help would be much appreciated.

Cheers,

Charlie

Parents Reply Children
No Data
Related