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

How to change the device name?

Hi all,

I'm using the PCA10028 and S130 based on SDK9.0。Now I have save the device name(such as "My device") in the flash,so the 51822 can read the device name and update the name when powered up or reset everytime.I have use sd_ble_gap_device_name_set function to set the name in the gap_params_init function reference to the project in SDK.Also I can see the value of err_code variable is zero when call the sd_ble_gap_device_name_set function.The gap_params_init is called in front of advertising_init function.But the device name cann't be updated.

Could anyone tell me how to make it?Thanks.

  • Hi. You will need to call ble_advdata_set() after you have changed the name with sd_ble_gap_device_name_set(). See this thread for more info.

    I have attached an example that uses a timer to change the name of a device every other second. It uses SDK V9 and Softdevice S110 V8. Search for // NORDIC in the example for comments.

    Example.

  • Hi.Thank you for your hellp.May be you have not understood what I mean.I mean the device name(eg My_Device) has been save in the flash.When the chip is powered up or done reset,the code will run to the gap_params_init() function.In the function,I have done some modification.I will check whether there is a name saved in the flash,or it will use the default name defined in the Macro DEVICE_NAME.

    The code as below:

            uint32_t                err_code;
            ble_gap_conn_params_t   gap_conn_params;
            ble_gap_conn_sec_mode_t sec_mode;
        
            BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode);
            
        		if(at_com_data.device_name[0]=='N')				// Device name has been saved in the flash.
        		{
        //			printf("change device name: ");
        			printf("change device name: %s",(uint8_t *)&at_com_data.device_name[1]);
        			printf("\r\n");
        
        			err_code = sd_ble_gap_device_name_set(&sec_mode,
                                                  (const uint8_t *)&at_com_data.device_name[1],
                                                  DeviceNameLen);	
    APP_ERROR_CHECK(err_code);
        			memset(&advdata, 0, sizeof(advdata));
        			advdata.name_type          = BLE_ADVDATA_FULL_NAME;
        			advdata.include_appearance = false;
        			advdata.flags              = BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE;
    
        			err_code = ble_advdata_set(&advdata, NULL);
        			APP_ERROR_CHECK(err_code);	
        //			printf("err_code=0x%08x\r\n",err_code);
        		}	
        		else
    // Device name isn't in the flash.
        		{
        			printf("defaule name\r\n");
        			err_code = sd_ble_gap_device_name_set(&sec_mode,
                                                  (const uint8_t *) DEVICE_NAME,
                                                  strlen(DEVICE_NAME));
    APP_ERROR_CHECK(err_code);
        		}
    

    Since the chip has been powered up or reset,and can read the device name from the flash successfully,why cann't change the device name?What I use is SDK9.0,S130 V1.0,PCA10028 where there is nRF51422 on the board.

    I also refer to your demo code and use a timer to change the name of a device every 10 second,most of time it is success.If the time less than 5 second,the device name canm't be changed mostly.

  • So this line prints the correct device name:

    printf("change device name: %s",(uint8_t *)&at_com_data.device_name[1]);
    

    ?

    And you are 100% sure that the first character in the device name is 'N'?

    Also in the else-statement you don't call ble_advdata_set() so you will not be able to set the name to DEVICE_NAME.

    Could you post your complete main.c file?

  • Hi,

    Yes,I'm sure.Now I have founf the problem.It is my fault that I have not write the length of device name to the flash,so DeviceNameLen is zero when the chip is powered up or reset.Thank you all the same.

Related