Renaming connected device

Hello Nordic

I am developing a sensor application using nRF52833. The user must be able to connect to the nRF52833 and give it a new name. I have a name characteristic that the user will write the new name to. When the nRF52833 receives the new name, it should disconnect and start advertising the new name. Then the user will reconnect using the new name.

I have read a lot of post on this subject. But none of them seems to work for me. I get the general idea about how to do it:

  • Set the new name using sd_ble_gap_device_name_set
  • Restart the advertising, several ways to do it are described in posts

I am using: SDK 17.1.0

Code starting point: examples\ble_peripheral\ble_app_template

I can rename the nRF52833, but nothing happens until a power cycling takes place.

This is my code:

In the main file I check whether the user has requested a new name. Then I set the new name and run the advertisement initialization again (suggested in this forum)

 if (is_new_device_name_requested()) // if the user has requested a new name for this sensor, then go ahead and set it

    {

        NRF_LOG_INFO("setting new device name");

        set_device_name(DEVICE_NAME);

        advertising_init();

  }

The set_device_name function is shown below:

void set_device_name(uint8_t* first_name)
{
ret_code_t err_code;
ble_gap_conn_sec_mode_t sec_mode;

BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode);

memset(full_device_name, 0, sizeof full_device_name);
strcpy(full_device_name, first_name);
strcat(full_device_name, general_par.last_device_name);

err_code = sd_ble_gap_device_name_set(&sec_mode,
(const uint8_t *)full_device_name,
strlen(full_device_name));
APP_ERROR_CHECK(err_code);
general_par.is_new_device_name_wanted = false;
}

The advertising_init function is shown below:

static void advertising_init(void)
{
ret_code_t err_code;
ble_advertising_init_t init;

memset(&init, 0, sizeof(init));

init.advdata.name_type = BLE_ADVDATA_FULL_NAME;
init.advdata.include_appearance = true;
init.advdata.flags = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;
init.advdata.uuids_complete.uuid_cnt = sizeof(m_adv_uuids) / sizeof(m_adv_uuids[0]);
init.advdata.uuids_complete.p_uuids = m_adv_uuids;

init.config.ble_adv_fast_enabled = true;
init.config.ble_adv_fast_interval = APP_ADV_INTERVAL;
init.config.ble_adv_fast_timeout = APP_ADV_DURATION;

init.evt_handler = on_adv_evt;

err_code = ble_advertising_init(&m_advertising, &init);
APP_ERROR_CHECK(err_code);

ble_advertising_conn_cfg_tag_set(&m_advertising, APP_BLE_CONN_CFG_TAG);
}

Parents Reply Children
Related