This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Switching between random and normal mac address?

I'm using sd_ble_gap_address_set() to randomize mac address and it's working fine. But I'm confused about how to change it back to normal(initial value I see) once I wan't to stop randomizing my mac address.

I'm on nrf51-DK, softdevice 7.0 and sdk 6.1. Here is the condition snippet I'm using to randomize and normalize the mac address, only the random part works. I'm calling it right after ble_stack_init() and expecting it to work after stopping/starting advertising again as stated in the docs.

if(<condition_is_satisfied>) {
		// set interval for changing mac address
		opt.gap.privacy.interval_s = 10;
		err_code = sd_ble_opt_set(BLE_GAP_OPT_PRIVACY, &opt);
		APP_ERROR_CHECK(err_code);

		gap_address.addr_type = BLE_GAP_ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE;
		err_code = sd_ble_gap_address_set(BLE_GAP_ADDR_CYCLE_MODE_AUTO, &gap_address);
		APP_ERROR_CHECK(err_code);
	} else {
            // there is a problem here
		gap_address.addr_type = BLE_GAP_ADDR_TYPE_PUBLIC;
		err_code = sd_ble_gap_address_set(BLE_GAP_ADDR_CYCLE_MODE_NONE, &gap_address);
		APP_ERROR_CHECK(err_code);
	}

Once the second part gets called with cycle mode none, I get a stable mac address but it's not the initial one I see. I don't understand what I'm doing wrong, any idea how can I go back to the initial mac address I've seen? (not a static one, but the very first mac address I see when I don't do any operations regarding the mac address).

EDIT:

Editing my question after Aryan's answer.

Changing the code and logic as below fixed my problem.

Prior to randomizing mac address, I save it's initial value using;

sd_ble_gap_address_get(&gap_address);

Then I save it inside non-volatile storage and load it back once I want to go back to normal mac address:

gap_address.addr_type = BLE_GAP_ADDR_TYPE_PUBLIC;
err_code = sd_ble_gap_address_set(BLE_GAP_ADDR_CYCLE_MODE_NONE, &initial_address);
APP_ERROR_CHECK(err_code);
Parents Reply Children
No Data
Related