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

sd_ble_gap_addr_set is the correct way to change device real MAC ?

Hi, I try to change my MAC address, but return error code 8.

API void set_dev_mac(uint8_t * mac){
    ble_gap_addr_t p_addr;
    ret_code_t err_code = sd_ble_gap_addr_get(&p_addr);
    p_addr.addr_type = BLE_GAP_ADDR_TYPE_RANDOM_STATIC;
    for(int i=0; i<6;i++)
        p_addr.addr[5-i]=mac[i];
    p_addr.addr[5] = 0xfd;// 0xC0; //ensure MSB 2BIT is 11 for BT MAC format

    char sname[24];
    sprintf(sname,  "Set mac:%02X%02X%02X%02X%02X%02X", p_addr.addr[5],p_addr.addr[4],p_addr.addr[3],p_addr.addr[2],p_addr.addr[1],p_addr.addr[0]);
    NRF_LOG_INFO("MAC :%s",sname);  
    NRF_LOG_FLUSH();
    
    err_code = sd_ble_gap_addr_set(&p_addr);
    if( err_code != NRF_SUCCESS ){
        NRF_LOG_INFO("Set MAC FAILE:%d",err_code);
    }
}

00> <info> app: MAC :Set mac:FD3456ABCDEF
00> 
00> <info> app: Set MAC FAILE:8
 

check error code 8 is : 

#define NRF_ERROR_INVALID_STATE               (NRF_ERROR_BASE_NUM + 8)  ///< Invalid state, operation disallowed in this state

So I try set the mac address after ble connected. and the function sd_ble_gap_addr_set retrun success, and try use sd_ble_gap_addr_get is what my last set.

But after device reboot ,  MAC change back to the origin Nordic random MAC address!!!

The usage of sd_ble_gap_addr_set is the correct way to change device real MAC ?

BR.

  • Hi,

    The address must be set with sd_ble_gap_addr_set() before you start start advertising or enter a connection, otherwise it will return error 8 (NRF_ERROR_INVALID_STATE) as you experienced.

    But after device reboot ,  MAC change back to the origin Nordic random MAC address!!!

     This is as excepted. The Softdevice will not store the address you provide in persistent memory, so you have to call sd_ble_gap_addr_set() with your address on every reboot (unless you want to use the device's random address)

    Best regards,

    Vidar

Related