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

Formatting MAC address ble_gap_addr_t.addr

I'm using this code to log the BLE MAC address on my peripheral device, an nRF51822.

void ble_log_mac_address(void)
{
	// Log our BLE address (6 bytes).
	ble_gap_addr_t addr;
	uint32_t err_code = sd_ble_gap_address_get(&addr);
	APP_ERROR_CHECK(err_code);

	NRF_LOG_RAW_INFO("\n%s\n", (int)addr.addr);
}

The output looks like this:

≴?A?b`

How do I get this into a more familiar format, such as this (and what is this format called, btw)?

D8:02:5C:D0:AC:77

Can I do this purely with a format string for NRF_LOG_RAW_INFO()?

Parents
  • Hi,

    I would have done it like this:

    void ble_log_mac_address(void)
    {
        // Log our BLE address (6 bytes).
        ble_gap_addr_t addr;
        // Use sd_ble_gap_addr_get() for NRF_SD_BLE_API_VERSION=3
        uint32_t err_code = sd_ble_gap_address_get(&addr);
        APP_ERROR_CHECK(err_code);
    
        NRF_LOG_RAW_INFO("\n%02X:%02X:%02X:%02X:%02X:%02X\n",
                         addr.addr[0], addr.addr[1],
                         addr.addr[2], addr.addr[3],
                         addr.addr[4], addr.addr[5]);
    }
    
  • Like I said, there's a limit to the number of varargs you can pass these macros and you don't get any error of you go over it. This works:

    // Because the NRF_LOG macros only support up to five varargs after the format string, we need to break this into two calls.
    NRF_LOG_RAW_INFO("\n%02X:%02X:%02X:", addr.addr[0], addr.addr[1], addr.addr[2]);
    NRF_LOG_RAW_INFO("%02X:%02X:%02X\n", addr.addr[3], addr.addr[4], addr.addr[5]);
    
Reply
  • Like I said, there's a limit to the number of varargs you can pass these macros and you don't get any error of you go over it. This works:

    // Because the NRF_LOG macros only support up to five varargs after the format string, we need to break this into two calls.
    NRF_LOG_RAW_INFO("\n%02X:%02X:%02X:", addr.addr[0], addr.addr[1], addr.addr[2]);
    NRF_LOG_RAW_INFO("%02X:%02X:%02X\n", addr.addr[3], addr.addr[4], addr.addr[5]);
    
Children
No Data
Related