Setting static random address more than once

Hello,

I am using bt_id_create() to set static random address.

bt_id_create() is called before bt_enable().

This works and sets the device address to a random address I specify.

But how can I change that address? For example - I have everything stopped (no connections, no advertisement), and I call bt_disable(). Then at some point later, I want to re-start BT but with different static random address. How can I do that?

I do not use settings, neither BT privacy. I just need to be able and use different static random address, when I enable BT.

Thank you,

D.

Parents
  • Hi D0023, 

    In stead of calling bt_id_create() you can call bt_id_reset() to change the address of the current identity. You would need to stop advertising before changing the address. 

    Note that identity BT_ID_DEFAULT (which is 0) is not possible to reset. You can only reset identity >0. 

    Please see the following code that I modified from the lbs example. The code will advertise with different addresses every 1 second: 

    void main(void)
    {
    	int blink_status = 0;
    	int err;
    	struct bt_le_adv_param adv_param = {
    		.id = BT_ID_DEFAULT,
    		.sid = 0,
    		.secondary_max_skip = 0,
    		.options = (BT_LE_ADV_OPT_CONNECTABLE),
    		.interval_min = 0x0020, /* 20 ms */
    		.interval_max = 0x0020, /* 20 ms */
    		.peer = NULL,
    	};
    
    	printk("Starting Bluetooth Peripheral LBS example\n");
    
    	err = dk_leds_init();
    	if (err) {
    		printk("LEDs init failed (err %d)\n", err);
    		return;
    	}
    
    	err = init_button();
    	if (err) {
    		printk("Button init failed (err %d)\n", err);
    		return;
    	}
    
    	if (IS_ENABLED(CONFIG_BT_LBS_SECURITY_ENABLED)) {
    		err = bt_conn_auth_cb_register(&conn_auth_callbacks);
    		if (err) {
    			printk("Failed to register authorization callbacks.\n");
    			return;
    		}
    
    		err = bt_conn_auth_info_cb_register(&conn_auth_info_callbacks);
    		if (err) {
    			printk("Failed to register authorization info callbacks.\n");
    			return;
    		}
    	}
    
    	err = bt_enable(NULL);
    	if (err) {
    		printk("Bluetooth init failed (err %d)\n", err);
    		return;
    	}
    	printk("Bluetooth initialized\n");
    
    	if (IS_ENABLED(CONFIG_SETTINGS)) {
    		settings_load();
    	}
    	err = bt_lbs_init(&lbs_callbacs);
    	if (err) {
    		printk("Failed to init LBS (err:%d)\n", err);
    		return;
    	}
    	adv_param.id=1;
    	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("Advertising successfully started\n");
    
    	for (;;) {
    		dk_set_led(RUN_STATUS_LED, (++blink_status) % 2);
    		k_sleep(K_MSEC(RUN_LED_BLINK_INTERVAL));
    		bt_le_adv_stop();
    		err = bt_id_reset(1,NULL, NULL);
    		if (err < 0) {
    			printk("Reset ID failed (err %d)\n", err);
    		};
    		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;
    		}
    	}
    }

    Notice how I set the advertising identity to 1 by calling adv_param.id=1; . In the code I set address to NULL to generate random static address, but you can input your own address. 

    Also you need to configure CONFIG_BT_ID_MAX=2

  • Hi,

    I can not get this working on my nRF52840dk. I created a new application from the peripheral_lbs sample. I copied your code from above and added CONFIG_BT_ID_MAX = 2 to my .conf file.

    bt_le_adv_start() returns -22 and when debugging I found that valid_adv_ext_param() in adv.c is returning false where param->id is equal to bt_dev.id_count:

    if (param->id >= bt_dev.id_count ||
            !bt_addr_le_cmp(&bt_dev.id_addr[param->id], BT_ADDR_LE_ANY)) {
            return false;
        }

    Tested with nRF Connect SDK v2.1.0 and v2.4.2.

    Thanks.

  • Hello,

    I've got the same issue as MDL. I am using the nRF52832. I took the original version of peripheral_lbs and copied in the new version of main() as above. I also added CONFIG_BT_ID_MAX=2.

    I noticed that it said "No ID address. App must call settings_load()" so I added a check to see if the settings_load method is working. I added a line to prj.conf CONFIG_SETTINGS=y.

    Here's the output on the terminal:

    Starting Bluetooth Peripheral LBS example
    I: 6 Sectors of 4096 bytes
    I: alloc wra: 0, fa8
    I: data wra: 0, d4
    I: SoftDevice Controller build revision:
    I: 6d 90 41 2a 38 e8 ad 17 |m.A*8...
    I: 29 a5 03 38 39 27 d7 85 |)..89'..
    I: 1f 85 d8 e1             |....    
    I: No ID address. App must call settings_load()
    Bluetooth initialized
    Loading settings...
    Advertising failed to start (err -22)

    adv_param has in it:


    id:
    1 '\001'
    sid:
    0 '\000'
    secondary_max_skip:
    0 '\000'
    options:
    1
    interval_min:
    32
    interval_max:
    32
    peer:
    0x0 <cbvprintf_package>

    if that's useful?

    ad has in it an array of two entries:

    [0]
    type:1 '\001'
    data_len:1 '\001'data:0x2000075c <__compound_literal.1> "\006\002#Ѽ\352_x#\025\336\357\022\022%\025"
    [1]
    type:9 '\t'
    data_len:10 '\n'
    data:0x27ba7 "Nordic_LBS"

    I realise I've the same problem

    param->id >= bt_dev.id_count

    it makes sense this should cause an error (as it's zero-indexed), but it's not clear how I make the 'id_count' bigger?

    Should I be calling bt_id_create?

    I wonder if it has anything to do with this issue?

Reply
  • Hello,

    I've got the same issue as MDL. I am using the nRF52832. I took the original version of peripheral_lbs and copied in the new version of main() as above. I also added CONFIG_BT_ID_MAX=2.

    I noticed that it said "No ID address. App must call settings_load()" so I added a check to see if the settings_load method is working. I added a line to prj.conf CONFIG_SETTINGS=y.

    Here's the output on the terminal:

    Starting Bluetooth Peripheral LBS example
    I: 6 Sectors of 4096 bytes
    I: alloc wra: 0, fa8
    I: data wra: 0, d4
    I: SoftDevice Controller build revision:
    I: 6d 90 41 2a 38 e8 ad 17 |m.A*8...
    I: 29 a5 03 38 39 27 d7 85 |)..89'..
    I: 1f 85 d8 e1             |....    
    I: No ID address. App must call settings_load()
    Bluetooth initialized
    Loading settings...
    Advertising failed to start (err -22)

    adv_param has in it:


    id:
    1 '\001'
    sid:
    0 '\000'
    secondary_max_skip:
    0 '\000'
    options:
    1
    interval_min:
    32
    interval_max:
    32
    peer:
    0x0 <cbvprintf_package>

    if that's useful?

    ad has in it an array of two entries:

    [0]
    type:1 '\001'
    data_len:1 '\001'data:0x2000075c <__compound_literal.1> "\006\002#Ѽ\352_x#\025\336\357\022\022%\025"
    [1]
    type:9 '\t'
    data_len:10 '\n'
    data:0x27ba7 "Nordic_LBS"

    I realise I've the same problem

    param->id >= bt_dev.id_count

    it makes sense this should cause an error (as it's zero-indexed), but it's not clear how I make the 'id_count' bigger?

    Should I be calling bt_id_create?

    I wonder if it has anything to do with this issue?

Children
Related