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

Device Name Change

Hi all ,

I'm using SDK 15.0, working on ble_uart.c

I need to change the DEVICE  NAME of the  frequently.

Can you please tell me how to change the the device name and store it in a buffer.

If you provide a example code, it will be a great help.

Thanks and Regards,

 Venu

Parents
  • Hi

    I found myself in the situation to implement a dynamically updating device name. Here some working, user-friendly code. The idea is to reuse the ble_advertising_init_t struct from initialization:

    #include "nrf_sdh_ble.h"
    
    
    BLE_ADVERTISING_DEF(m_advertising);
    static ble_advertising_init_t adv_init;
    
    /* This function will update the device name and advertising data accordingly. */
    void bluetooth_update_name(uint8_t* name) {
    	ret_code_t err_code;
    	ble_gap_conn_sec_mode_t sec_mode;
    
    	BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(&sec_mode);
    
    	if (current_config.name[0] != '\0') {
    		err_code = sd_ble_gap_device_name_set(&sec_mode, name, strnlen(name, 10));
    		APP_ERROR_CHECK(err_code);
    	}
    	else {
    		err_code = sd_ble_gap_device_name_set(&sec_mode, (const uint8_t *)NUS_DEVICE_NAME, strlen(NUS_DEVICE_NAME));
    		APP_ERROR_CHECK(err_code);
    	}
    
    
      //sd_ble_gap_adv_stop(&m_advertising.adv_handle);
    
    	err_code = ble_advdata_encode(&adv_init.advdata, m_advertising.enc_advdata, &m_advertising.adv_data.adv_data.len);
    	VERIFY_SUCCESS(err_code);
    
    	if (m_advertising.adv_modes_config.ble_adv_extended_enabled == true)
    	{
    #ifdef BLE_GAP_ADV_SET_DATA_SIZE_EXTENDED_CONNECTABLE_MAX_SUPPORTED
    		m_advertising.adv_data.scan_rsp_data.len = BLE_GAP_ADV_SET_DATA_SIZE_EXTENDED_CONNECTABLE_MAX_SUPPORTED;
    #else
    		m_advertising.adv_data.scan_rsp_data.len = BLE_GAP_ADV_SET_DATA_SIZE_MAX;
    #endif // BLE_GAP_ADV_SET_DATA_SIZE_EXTENDED_CONNECTABLE_MAX_SUPPORTED
    	}
    	else
    	{
    		m_advertising.adv_data.scan_rsp_data.len = BLE_GAP_ADV_SET_DATA_SIZE_MAX;
    	}
    
    	err_code = ble_advdata_encode(&adv_init.srdata,
    		m_advertising.adv_data.scan_rsp_data.p_data,
    		&m_advertising.adv_data.scan_rsp_data.len);
    
    	sd_ble_gap_adv_set_configure(&m_advertising.adv_handle,
    		m_advertising.p_adv_data,
    		NULL);
    }
    
    
    /* This is my initialization routine for advertising */
    static void advertising_init(void) {
    	uint32_t err_code;
    
    	memset(&adv_init, 0, sizeof(adv_init));
    
    	adv_init.advdata.name_type = BLE_ADVDATA_FULL_NAME;
    	adv_init.advdata.include_appearance = false;
    	adv_init.advdata.flags = BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE;
    
    	adv_init.srdata.uuids_complete.uuid_cnt = sizeof(m_adv_uuids) / sizeof(m_adv_uuids[0]);
    	adv_init.srdata.uuids_complete.p_uuids = m_adv_uuids;
    
    	adv_init.config.ble_adv_fast_enabled = true;
    	adv_init.config.ble_adv_fast_interval = APP_ADV_INTERVAL;
    	adv_init.config.ble_adv_fast_timeout = APP_ADV_DURATION;
    
    	adv_init.evt_handler = on_adv_evt;
    
    	err_code = ble_advertising_init(&m_advertising, &adv_init);
    	APP_ERROR_CHECK(err_code);
    
    	ble_advertising_conn_cfg_tag_set(&m_advertising, APP_BLE_CONN_CFG_TAG);
    }
    

    If you have some questions, please ask right away Slight smile

    Best regards

    Markus, Technokrat GmbH

Reply
  • Hi

    I found myself in the situation to implement a dynamically updating device name. Here some working, user-friendly code. The idea is to reuse the ble_advertising_init_t struct from initialization:

    #include "nrf_sdh_ble.h"
    
    
    BLE_ADVERTISING_DEF(m_advertising);
    static ble_advertising_init_t adv_init;
    
    /* This function will update the device name and advertising data accordingly. */
    void bluetooth_update_name(uint8_t* name) {
    	ret_code_t err_code;
    	ble_gap_conn_sec_mode_t sec_mode;
    
    	BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(&sec_mode);
    
    	if (current_config.name[0] != '\0') {
    		err_code = sd_ble_gap_device_name_set(&sec_mode, name, strnlen(name, 10));
    		APP_ERROR_CHECK(err_code);
    	}
    	else {
    		err_code = sd_ble_gap_device_name_set(&sec_mode, (const uint8_t *)NUS_DEVICE_NAME, strlen(NUS_DEVICE_NAME));
    		APP_ERROR_CHECK(err_code);
    	}
    
    
      //sd_ble_gap_adv_stop(&m_advertising.adv_handle);
    
    	err_code = ble_advdata_encode(&adv_init.advdata, m_advertising.enc_advdata, &m_advertising.adv_data.adv_data.len);
    	VERIFY_SUCCESS(err_code);
    
    	if (m_advertising.adv_modes_config.ble_adv_extended_enabled == true)
    	{
    #ifdef BLE_GAP_ADV_SET_DATA_SIZE_EXTENDED_CONNECTABLE_MAX_SUPPORTED
    		m_advertising.adv_data.scan_rsp_data.len = BLE_GAP_ADV_SET_DATA_SIZE_EXTENDED_CONNECTABLE_MAX_SUPPORTED;
    #else
    		m_advertising.adv_data.scan_rsp_data.len = BLE_GAP_ADV_SET_DATA_SIZE_MAX;
    #endif // BLE_GAP_ADV_SET_DATA_SIZE_EXTENDED_CONNECTABLE_MAX_SUPPORTED
    	}
    	else
    	{
    		m_advertising.adv_data.scan_rsp_data.len = BLE_GAP_ADV_SET_DATA_SIZE_MAX;
    	}
    
    	err_code = ble_advdata_encode(&adv_init.srdata,
    		m_advertising.adv_data.scan_rsp_data.p_data,
    		&m_advertising.adv_data.scan_rsp_data.len);
    
    	sd_ble_gap_adv_set_configure(&m_advertising.adv_handle,
    		m_advertising.p_adv_data,
    		NULL);
    }
    
    
    /* This is my initialization routine for advertising */
    static void advertising_init(void) {
    	uint32_t err_code;
    
    	memset(&adv_init, 0, sizeof(adv_init));
    
    	adv_init.advdata.name_type = BLE_ADVDATA_FULL_NAME;
    	adv_init.advdata.include_appearance = false;
    	adv_init.advdata.flags = BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE;
    
    	adv_init.srdata.uuids_complete.uuid_cnt = sizeof(m_adv_uuids) / sizeof(m_adv_uuids[0]);
    	adv_init.srdata.uuids_complete.p_uuids = m_adv_uuids;
    
    	adv_init.config.ble_adv_fast_enabled = true;
    	adv_init.config.ble_adv_fast_interval = APP_ADV_INTERVAL;
    	adv_init.config.ble_adv_fast_timeout = APP_ADV_DURATION;
    
    	adv_init.evt_handler = on_adv_evt;
    
    	err_code = ble_advertising_init(&m_advertising, &adv_init);
    	APP_ERROR_CHECK(err_code);
    
    	ble_advertising_conn_cfg_tag_set(&m_advertising, APP_BLE_CONN_CFG_TAG);
    }
    

    If you have some questions, please ask right away Slight smile

    Best regards

    Markus, Technokrat GmbH

Children
  • hi atok... i need your help pls . i need to change the device name using ble app uart

  • Hi pspavi
    What do you intend to do?

    Best regards

    Markus, Technokrat GmbH

  • Hi....atok ..sorry for late , i didn't receive your notification . yes i want to change my device name after bonding  periodically via nrf toolbox ,how to do that ? i tried all the ways and read many threads , i don't know how to Re- initialize the advertising again ... can you share me sample code so that i can , it will be more helpfull

    Thanks&Regards 

    pspavi

  • Hi pspavi

    There is sadly no direct solution for the NRF Toolbox's standard firmware to do this.
    A first step is to check out whether you a ready to setup and build your own custom "Nordic UART Service" (NUS) firmware to do this.

    I have written my own multi-service application including multi-protocol support and non-volatile configuration, which I cannot share for confidentiality reasons.

    Yet the code mentioned before is the crucial part to change a device's name. My suggestion for you is to adapt your main.c code to include my logic to control the ble_advertising_init_t related parts with your own defined NUS command.

    Have a look inside \examples\ble_peripheral\ble_app_uart or \examples\ble_peripheral\experimental\ble_app_cli in the SDK to see how to scaffold your own NUS application. It gave me the crucial starting point to do more complex setups.

    Also see Command Line Interface library for how to add a custom command.


    Best regards
    Markus, Technokrat GmbH

  • thank you ,i dono know whether you understood my question, may be my communication problem . I am explaining it again, we are using custom board nrf52832 , i will connect the device using nrf toolbox app ,and then i will give command as for example '1' , when i receive '1' , it should stop advertise and then it should re advertise with the new name which i was given in the condition 

    if(command=='1')

    {

    change_name;

    stop_adv;

    start_adv;

    }

    similar to this it should work

    Thanks&Regards 

    pspavi

Related