Updating scan response packet whilst advertising

In my application, I'm using the Scan Response Packet to transmit some device related information, rather than requiring Clients to connect to get this.  I'm using NCS v2.0.0 and testing on an nRF52-DK.

I'm using the following to store the scan response information, and I update the information in there whenever it changes.

static struct bt_data sd[] = {
    BT_DATA(BT_DATA_MANUFACTURER_DATA, NULL, BT_GAP_ADV_MAX_ADV_DATA_LEN-2),
};

On first boot up, when I start BLE, I make a call to:

err = bt_le_adv_start(BT_LE_ADV_CONN_LSR_AD, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));

That successfully updates the scan response packet.  But if I subsequently attempt to update the scan response packet whilst I'm still advertising, via a call to:

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

Then the scan response packet doesn't change.  I'm checking the result of err after this function call, and its returning 0, which indicates that its been successful.

The only way I seem able to get the scan response packet to update correctly is if I:

  1. Stop advertising
  2. Update scan response packet
  3. Restart advertising

Is there a way to dynamically update the scan response packet whilst advertising?  Or do I need to follow the process of stop, update, restart?

Cheers,

Mike

Parents Reply Children
  • Hi Mike Austin,

    I notice that the data field of your Scan Response BT_DATA macro is NULL, however that is where you supply data to the API. That is most likely the problem. I am not sure how you update the Scan Response data like with that setup though?

    On my end, I set up as followed and just calling bt_le_adv_update_data() worked. As I started with a non-connectable base, I test without the BT_LE_ADV_OPT_CONNECTABLE flag. I don't think it changes anything though.

    static uint8_t mfg_data[BT_GAP_ADV_MAX_ADV_DATA_LEN-2] = { 0xff, 0xff, 0x00 };
    
    static const struct bt_data sd[] = {
    	BT_DATA(BT_DATA_MANUFACTURER_DATA, mfg_data, BT_GAP_ADV_MAX_ADV_DATA_LEN-2),
    };
    
    ...
    
    	while(1) {
    		k_sleep(K_MSEC(1000));
    
    		mfg_data[2]++;
    		printk("mfg_data = {%02x, %02x, %02x}\n", mfg_data[0], mfg_data[1], mfg_data[2]);
    
    		err = bt_le_adv_update_data(ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));
    		printk("bt_le_adv_update_data %d\n\n", err); 
    		if (err) return;
    	}

    Could you please try it and let me know if it works?

    Best regards,

    Hieu

Related