This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

how to dynamically change device_name on NCS?

Hi all:

I want to dynamically change device_name at peripheral_hids_mouse,

I perform two tests, but they are all not successful.

(1)

add below command into prj.conf

CONFIG_BT_DEVICE_NAME_DYNAMIC=y

add below code before bt_le_adv_start()

adv_param.options |= BT_LE_ADV_OPT_USE_NAME;
bt_set_name("test_name");

static void advertising_continue(void)
{
	struct bt_le_adv_param adv_param;

#if CONFIG_BT_DIRECTED_ADVERTISING
	bt_addr_le_t addr;

	if (!k_msgq_get(&bonds_queue, &addr, K_NO_WAIT)) {
		char addr_buf[BT_ADDR_LE_STR_LEN];

		adv_param = *BT_LE_ADV_CONN_DIR(&addr);
		adv_param.options |= BT_LE_ADV_OPT_DIR_ADDR_RPA;
               
		int err = bt_le_adv_start(&adv_param, NULL, 0, NULL, 0);

		if (err) {
			printk("Directed advertising failed to start\n");
			return;
		}

		bt_addr_le_to_str(&addr, addr_buf, BT_ADDR_LE_STR_LEN);
		printk("Direct advertising to %s started\n", addr_buf);
	} else
#endif
	{
		int err;

		adv_param = *BT_LE_ADV_CONN;
		adv_param.options |= BT_LE_ADV_OPT_ONE_TIME;
        adv_param.options |= BT_LE_ADV_OPT_USE_NAME; //modify
              

        bt_set_name("test_name");  //modify

		err = bt_le_adv_start(&adv_param, ad, ARRAY_SIZE(ad),
				  sd, ARRAY_SIZE(sd));
		if (err) {
			printk("Advertising failed to start (err %d)\n", err);
			return;
		}

		printk("Regular advertising started\n");
	}
}

the log shows "Advertising failed to start (err -22)"

(2)

use BT_LE_ADV_CONN_NAME instead of BT_LE_ADV_CONN 

adv_param = *BT_LE_ADV_CONN_NAME;

it still show "Advertising failed to start (err -22)"

best regards,

Joe

Parents
  • Hi Joe, 

    I assume you are not trying to put the name into the Directed Advertising packet but it's the normal advertising you are asking about. 

    Please show what's in your ad[] array. 
    The following code worked for me: 

    	static const struct bt_data ad[] = {
    	BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
    	//BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN),
    };
    
    static const struct bt_data sd[] = {
    	BT_DATA_BYTES(BT_DATA_UUID128_ALL, BT_UUID_NUS_VAL),
    };
    
    	err=bt_set_name("test_name");
    	if (err) {
    		LOG_ERR("Setname failed(err %d)", err);
    		return;
    	}
    	err = bt_le_adv_start(BT_LE_ADV_CONN_NAME, ad, ARRAY_SIZE(ad), sd,
    			      ARRAY_SIZE(sd));
    	if (err) {
    		LOG_ERR("Advertising failed to start (err %d)", err);
    		return;
    	}

    Note that I have to remove the 

        BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN),
    inside the ad[] packet because it's already covered in the advertising option to use name. 
    Also if it's already advertising, and you change the name, you need to call 
            err = bt_le_adv_update_data( ad, ARRAY_SIZE(ad), sd,
                      ARRAY_SIZE(sd));
    To update the device name. 
  • Hi Hung:

    I follow your code,it can update the device name successfully on peripheral_uart example.

    for peripheral_hids_mouse example,

    There are normal advertising and directed advertising,

    I have the error at normal advertising  (Advertising failed to start (err -22)")

    I am not sure the error related to ad[]  /sd[] or not. 

    if (!k_msgq_get(&bonds_queue, &addr, K_NO_WAIT)) {

       ...     


        int err = bt_le_adv_start(&adv_param, NULL, 0, NULL, 0);  //Directed Advertising


    } else

    {
       ...

    adv_param = *BT_LE_ADV_CONN;
    adv_param.options |= BT_LE_ADV_OPT_ONE_TIME;
    adv_param.options |= BT_LE_ADV_OPT_USE_NAME; //modify

    bt_set_name("test_name"); //modify

    err = bt_le_adv_start(&adv_param, ad, ARRAY_SIZE(ad),      //normal Advertising
    sd, ARRAY_SIZE(sd));

    }

    for peripheral_hids_mouse example ,its  ad[] array and sd[] array are not same as peripheral_uart.

    so I don't modify ad[] and sd[] .

     

    my ad[] array as below

    peripheral_hids_mouse:

    static const struct bt_data ad[] = {
    BT_DATA_BYTES(BT_DATA_GAP_APPEARANCE,
                                     (CONFIG_BT_DEVICE_APPEARANCE >> 0) & 0xff,
                                     (CONFIG_BT_DEVICE_APPEARANCE >> 8) & 0xff),
    BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
    BT_DATA_BYTES(BT_DATA_UUID16_ALL,
                                      0x12, 0x18, /* HID Service */
                                       0x0f, 0x18), /* Battery Service */
    };

    static const struct bt_data sd[] = {
    BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN),
    };

    best regards,

    Joe

Reply
  • Hi Hung:

    I follow your code,it can update the device name successfully on peripheral_uart example.

    for peripheral_hids_mouse example,

    There are normal advertising and directed advertising,

    I have the error at normal advertising  (Advertising failed to start (err -22)")

    I am not sure the error related to ad[]  /sd[] or not. 

    if (!k_msgq_get(&bonds_queue, &addr, K_NO_WAIT)) {

       ...     


        int err = bt_le_adv_start(&adv_param, NULL, 0, NULL, 0);  //Directed Advertising


    } else

    {
       ...

    adv_param = *BT_LE_ADV_CONN;
    adv_param.options |= BT_LE_ADV_OPT_ONE_TIME;
    adv_param.options |= BT_LE_ADV_OPT_USE_NAME; //modify

    bt_set_name("test_name"); //modify

    err = bt_le_adv_start(&adv_param, ad, ARRAY_SIZE(ad),      //normal Advertising
    sd, ARRAY_SIZE(sd));

    }

    for peripheral_hids_mouse example ,its  ad[] array and sd[] array are not same as peripheral_uart.

    so I don't modify ad[] and sd[] .

     

    my ad[] array as below

    peripheral_hids_mouse:

    static const struct bt_data ad[] = {
    BT_DATA_BYTES(BT_DATA_GAP_APPEARANCE,
                                     (CONFIG_BT_DEVICE_APPEARANCE >> 0) & 0xff,
                                     (CONFIG_BT_DEVICE_APPEARANCE >> 8) & 0xff),
    BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
    BT_DATA_BYTES(BT_DATA_UUID16_ALL,
                                      0x12, 0x18, /* HID Service */
                                       0x0f, 0x18), /* Battery Service */
    };

    static const struct bt_data sd[] = {
    BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN),
    };

    best regards,

    Joe

Children
Related