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

Reply Children
No Data
Related