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

Restart Advertising after changing device name

Hi,

I have read a few other related tickets but still have not found a fix

We want to be able to change device name in the field to show a meaningful name when scanning.

We set the name at runtime after reading out of flash.

During a boot, the device first starts all ble services. Then the flash is read back and the device name is updated, as needed.

The device name is being set with the following

void set_device_name(const uint8_t* device_name, size_t device_name_len)
{
    ble_gap_conn_sec_mode_t sec_mode;
    bool erase_bonds = true;

    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode);
    sd_ble_gap_device_name_set(&sec_mode, device_name, device_name_len);
    // Restart advertising
    ble_app_restart_advertising();
}

Restart advertising like this (from advice here)

void ble_app_restart_advertising(void)
{
    // Stop
    uint32_t err_code = sd_ble_gap_adv_stop(m_advertising.adv_handle);
    NRF_LOG_INFO("Stopping advertising");

    // Start
    err_code = sd_ble_gap_adv_start(m_advertising.adv_handle, APP_BLE_CONN_CFG_TAG);
    APP_ERROR_CHECK(err_code);
    NRF_LOG_INFO("Starting advertising");
}

This seems to only change the device name shown after a connection. When scanning - the default device name is still shown

What else needs to happen to update the device name shown when scanning?

  • Hi,

    You will also need to update the advertisement data. This can be done using the Softdevice call: sd_ble_gap_adv_set_configure

    Or if you use the sdk advertisement module by using the SDK function ble_advertising_advdata_update

  • Thankyou, calling sd_ble_gap_adv_set_configure with advdata non-null was the key.

    I was using a mix of example code and sdk calls to reset / restart.

    The example code initialise function called set_configure without the adv data. Calling ble_advertising_start, after initialising again, makes another call to sd_ble_gap_adv_set_configure with the advdata pointer passed correctly

    void ble_app_restart_advertising(void)
    {
        // Stop
        bool erase_bonds = false;
        uint32_t err_code = sd_ble_gap_adv_stop(m_advertising.adv_handle);
        NRF_LOG_INFO("Stopping advertising");
    
        // Re-init
        advertising_init();
    
        ble_app_advertising_start(&erase_bonds);
    
        NRF_LOG_INFO("Starting advertising");
    }

Related