Changing device name dynamically

Hi, sorry for adding more posts on the same issue, but I don't see any of the posts that speaks to my current situation. I'm using nRF51822 and SDK 12.3.0, and I'm trying to implement the functionality of changing device name while the device is still connected to the phone. I'm using NUS service to send the command to the device. Here is my snippet of my code in nus_data_handler:

uint32_t err_code = sd_ble_gap_device_name_set(&sec_mode, device_name, length);
APP_ERROR_CHECK(err_code);

advertising_init();
err_code = ble_advertising_start(BLE_ADV_MODE_FAST);
APP_ERROR_CHECK(err_code);

And here is the snippet of the function advertising_init():

static void advertising_init(void){
	ble_advdata_t          advdata;
	ble_advdata_t          scanrsp;
	ble_adv_modes_config_t options;

	// Build advertising data struct to pass into @ref ble_advertising_init.
	memset(&advdata, 0, sizeof(advdata));
	advdata.name_type          = BLE_ADVDATA_FULL_NAME;
	advdata.include_appearance = false;
	advdata.flags              = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;

	memset(&scanrsp, 0, sizeof(scanrsp));
	scanrsp.uuids_complete.uuid_cnt = sizeof(m_adv_uuids) / sizeof(m_adv_uuids[0]);
	scanrsp.uuids_complete.p_uuids  = m_adv_uuids;

	memset(&options, 0, sizeof(options));
    options.ble_adv_whitelist_enabled      = true;
    options.ble_adv_directed_enabled       = true;
    options.ble_adv_directed_slow_enabled  = false;
    options.ble_adv_directed_slow_interval = 0;
    options.ble_adv_directed_slow_timeout  = 0;
    options.ble_adv_fast_enabled           = true;
    options.ble_adv_fast_interval          = APP_ADV_FAST_INTERVAL;
    options.ble_adv_fast_timeout           = APP_ADV_FAST_TIMEOUT;

	uint32_t err_code = ble_advertising_init(&advdata, &scanrsp, &options, on_adv_evt, ble_advertising_error_handler);
	APP_ERROR_CHECK(err_code);
}

adversiting_init() works fine so far in my code, but ble_advertising_start() is returning error code of 18, which means NRF_ERROR_CONN_COUNT: The limit of available connections has been reached; connectable advertiser cannot be started. Then my device will be disconnected with status code of 0x8. I can reconnect back to the device, but the advertised device name is still the same, and even the value in device name characteristic is not changed. Does anyone know what happened here? 

Related