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

Periodically changing the MAC address on nRF51822 Beacon Kit

Hi,

I am able to change the MAC address of the beacon using the call sd_ble_gap_address_set(BLE_GAP_ADDR_CYCLE_MODE_NONE, &gap_addr), where gap_addr is of type BLE_GAP_ADDR_TYPE_RANDOM_STATIC.

Now what I want to do is to change the MAC address periodically, while the beacon is still advertising. From what I understand, I need to use sd_ble_gap_address_set(BLE_GAP_ADDR_CYCLE_MODE_AUTO, &gap_addr), with an address of type BLE_GAP_ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE. However, the MAC address seems to change only during the boot up and when the SW2 button is pressed (changes one when beacon goes into config mode and once when beacon switches back to advertising). I set the BLE_GAP_DEFAULT_PRIVATE_ADDR_CYCLE_INTERVAL_S to 5 to be able to observe the change. I am calling the sd_ble_gap_address_set function after ble_stack_init() call in the main function of the sample beacon app.

What should I do to achieve periodic change of MAC address? Can this be achieved with LE_GAP_ADDR_CYCLE_MODE_AUTO mode at all, or should I simply set up a timer, create random MAC address and call sd_ble_gap_address_set(BLE_GAP_ADDR_CYCLE_MODE_NONE, &gap_addr) every time timer expires?

Thanks for your help in advance.

  • Hi Sarah,

    The address should change even while advertising, so there is no need to set up any timers for this. Just to make sure: Are you calling sd_ble_opt_set() with gap_opt.privacy.interval_s set to 5, or just changing the define?

    To change the default address rotation time (15 minutes) you have to do something like this:

    ble_opt_t opt = {0};
    opt.gap_opt.privacy.interval_s = TIME_IN_SECONDS_HERE;
    err_code = sd_ble_opt_set(BLE_GAP_OPT_PRIVACY, &opt);
    APP_ERROR_CHECK(err_code);
    
  • I just changed this default line #define BLE_GAP_DEFAULT_PRIVATE_ADDR_CYCLE_INTERVAL_S (60 * 15) to #define BLE_GAP_DEFAULT_PRIVATE_ADDR_CYCLE_INTERVAL_S (5). I did not use sd_ble_opt_set() function. It looks like this is where the problem is, changing the above default value in the code has no effect. If I wait for 15 minutes, then the MAC address changes. How do I fix this?

    Below is my code for the periodic change. Like I said before, I observe a change in MAC address only when I press SW2 button.

    void periodically_change_mac_address() {

    ble_gap_addr_t gap_address; uint8_t 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);
    

    }

  • Okay, that will not work as expected. The define is not picked up by the SoftDevice in any way, and is just meant to provide the default (initial) parameter that is used. To change the default, you have to call sd_ble_opt_set() like in the edited answer. If not, it will just change every 15 minutes.

  • Thanks so much for your answer, Ulrich. In SDK 7.1, sd_ble_opt_set() expects two arguments. One is an option id, the other one is the ble_opt_t structure. In my case, the opt id needs to be opt.gap_opt.privacy.interval_s . Is that right? I tried your code with that, and if I don't comment out APP_ERROR_CHECK(err_code) line, the beacon doesn't work. When I comment it out, it does work, but it doesn't seem to change the MAC id for the given interval. It seems to be changing randomly. Do you have any idea why? Below is how my code looks like now:

    void periodically_change_mac_address(){ ble_gap_addr_t gap_address; uint8_t err_code;

        // Change MAC update interval
    ble_opt_t opt = {0};
    opt.gap_opt.privacy.interval_s = 5;
    err_code = sd_ble_opt_set(opt.gap_opt.privacy.interval_s, &opt);
    //APP_ERROR_CHECK(err_code);
    
    // Change MAC address
    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);
    

    }

  • Looks like it always needed a parameter. Not the most beautifully documented unusually, the clue is in the definition of ble_opt_t here. BLE_GAP_OPT_PRIVACY appears to the one you want for this.

Related