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

How do change the value of IRK

I use nrf51822 to do bluetooth keyboard, I hope the function is: I want to change the IRK value of the bluetooth keyboard each time it is paired and bound,Anybody know how to do that?My tests so far have found that the IRK value is fixed for each pair binding,I want to change IRK to a random number. How do I change it? thanks!

Parents
  • Hi,

    Which SDK are you using? Since you have an nRF51 I will assume SDK 12.3 for now.

    In that case, you can call pm_privacy_set() with the pm_privacy_params_t parameter holding a pointer to the new IRK. Note that you have to store this IRK in flash (for instance using FDS), and make this call after every reset. The IRK itself is just a random number, so you can generate it using the RNG peripheral.

    Note that changing the IRK will mean that any existing bonds will fail since the peer will not recognize the nRF as the same device. Therefore, changing the IRK should normally only be done together with erasing all existing bonds.

  • Hi,

    OK, with older SoftDevices you have to use the options API to set the BLE_GAP_OPT_PRIVACY. There is no example of this in SDK 9, but you can refer to for instance the im_privacy_set() implementation for SDK 12.3 and copy what you need. It should only need small adaptations. Note that this just shows how you set the IRK, you still have to handle what I described earlier about generating and storing the IRK.

            ret_code_t     ret;
            ble_gap_addr_t privacy_addr;
            ble_gap_irk_t  current_irk;
            ble_opt_t      privacy_options;
            ble_opt_t      current_privacy_options;
    
            NRF_PM_DEBUG_CHECK(p_privacy_params != NULL);
    
            privacy_addr.addr_type                        = p_privacy_params->private_addr_type;
            privacy_options.gap_opt.privacy.p_irk         = p_privacy_params->p_device_irk;
            privacy_options.gap_opt.privacy.interval_s    = p_privacy_params->private_addr_cycle_s;
            current_privacy_options.gap_opt.privacy.p_irk = &current_irk;
    
            // Can not fail.
            (void) sd_ble_opt_get(BLE_GAP_OPT_PRIVACY, &current_privacy_options);
            (void) sd_ble_opt_set(BLE_GAP_OPT_PRIVACY, &privacy_options);

Reply
  • Hi,

    OK, with older SoftDevices you have to use the options API to set the BLE_GAP_OPT_PRIVACY. There is no example of this in SDK 9, but you can refer to for instance the im_privacy_set() implementation for SDK 12.3 and copy what you need. It should only need small adaptations. Note that this just shows how you set the IRK, you still have to handle what I described earlier about generating and storing the IRK.

            ret_code_t     ret;
            ble_gap_addr_t privacy_addr;
            ble_gap_irk_t  current_irk;
            ble_opt_t      privacy_options;
            ble_opt_t      current_privacy_options;
    
            NRF_PM_DEBUG_CHECK(p_privacy_params != NULL);
    
            privacy_addr.addr_type                        = p_privacy_params->private_addr_type;
            privacy_options.gap_opt.privacy.p_irk         = p_privacy_params->p_device_irk;
            privacy_options.gap_opt.privacy.interval_s    = p_privacy_params->private_addr_cycle_s;
            current_privacy_options.gap_opt.privacy.p_irk = &current_irk;
    
            // Can not fail.
            (void) sd_ble_opt_get(BLE_GAP_OPT_PRIVACY, &current_privacy_options);
            (void) sd_ble_opt_set(BLE_GAP_OPT_PRIVACY, &privacy_options);

Children
Related