Modifying the Bluetooth name in the program did not take effect.

I set CONFIG_BT_DEVICE_NAME_DYNAMIC=y in prj.conf, and then called it after the system started.

bt_le_adv_stop();

bt_set_name(name); 

bt_le_adv_update_data(ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));

bt_le_adv_start(BT_LE_ADV_CONN, ad, ARRAY_SIZE(ad), sd,ARRAY_SIZE(sd));

These functions modify the Bluetooth name, but the new Bluetooth name has not taken effect.

Parents
  • Hello,

    The problem is that the ad array (or sd if the name is placed in the scan response) still points to the original device name. I've created an example for how you can update the advertisement name based on the peripheral_lbs sample which I have included below.

    peripheral_lbs_udate_adv_name_3.1.1.zip

    static void device_name_update(void)
    {
    	int err;
    	static int count;
    	static char new_name[BT_GAP_ADV_MAX_ADV_DATA_LEN];
    	uint16_t total_payload = 0;
    
    	memset(new_name, 0, sizeof(new_name));
    
    	err = snprintk(new_name, sizeof(new_name), "%s_%d", DEVICE_NAME, ++count);
    	if (err < 0) {
    		printk("snprintk() failed. (err: %d)\n", err);
    		return;
    	}
    
    	printk("New device name: %s\n", new_name);
    
    	/* Update device name. Can be read from attribue table */
    	err = bt_set_name(new_name);
    	if (err) {
    		printk("bt_set_name() failed. (err: %d)\n", err);
    		return;
    	}
    
    	/* Update the advertisment name field in advertisment payload */
    	for (int i = 0; i < ARRAY_SIZE(ad); i++) {
    		if (ad[i].type == BT_DATA_NAME_COMPLETE) {
    			ad[i].data = new_name;
    			ad[i].data_len = strlen(new_name);
    		}
    
    		total_payload += (ad[i].data_len + 2);
    	}
    
    	err = bt_le_adv_update_data(ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));
    	if (err && err != -EAGAIN) {
    		printk("bt_le_adv_update_data() failed. (err: %d)\n", err);
    	}
    
    	if (total_payload > BT_GAP_ADV_MAX_ADV_DATA_LEN) {
    		printk("Name is too long and will be truncated\n");
    	}
    
    	printk("Primary advertisment payload size: %d\n", total_payload);
    }

    Best regards,

    Vidar

Reply
  • Hello,

    The problem is that the ad array (or sd if the name is placed in the scan response) still points to the original device name. I've created an example for how you can update the advertisement name based on the peripheral_lbs sample which I have included below.

    peripheral_lbs_udate_adv_name_3.1.1.zip

    static void device_name_update(void)
    {
    	int err;
    	static int count;
    	static char new_name[BT_GAP_ADV_MAX_ADV_DATA_LEN];
    	uint16_t total_payload = 0;
    
    	memset(new_name, 0, sizeof(new_name));
    
    	err = snprintk(new_name, sizeof(new_name), "%s_%d", DEVICE_NAME, ++count);
    	if (err < 0) {
    		printk("snprintk() failed. (err: %d)\n", err);
    		return;
    	}
    
    	printk("New device name: %s\n", new_name);
    
    	/* Update device name. Can be read from attribue table */
    	err = bt_set_name(new_name);
    	if (err) {
    		printk("bt_set_name() failed. (err: %d)\n", err);
    		return;
    	}
    
    	/* Update the advertisment name field in advertisment payload */
    	for (int i = 0; i < ARRAY_SIZE(ad); i++) {
    		if (ad[i].type == BT_DATA_NAME_COMPLETE) {
    			ad[i].data = new_name;
    			ad[i].data_len = strlen(new_name);
    		}
    
    		total_payload += (ad[i].data_len + 2);
    	}
    
    	err = bt_le_adv_update_data(ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));
    	if (err && err != -EAGAIN) {
    		printk("bt_le_adv_update_data() failed. (err: %d)\n", err);
    	}
    
    	if (total_payload > BT_GAP_ADV_MAX_ADV_DATA_LEN) {
    		printk("Name is too long and will be truncated\n");
    	}
    
    	printk("Primary advertisment payload size: %d\n", total_payload);
    }

    Best regards,

    Vidar

Children
Related