advertising the MAC address with sd_ble_gap_adv_set_configure -> missing 7th byte

Hi

I implemented in the function call in the SDK bootloader on a nRF52840 the BLE MAC address with the following function call:

static uint32_t advertising_init(uint8_t adv_flags, ble_gap_adv_params_t const * const p_adv_params)
{
...

    // Get MAC address
	err_code = sd_ble_gap_addr_get(&p_addr);
	memcpy((uint8_t*)&serial_number_lsb, p_addr.addr, 4);
	memcpy((uint8_t*)&serial_number_msb, &p_addr.addr[4], 2);

    m_enc_advdata[m_adv_data.adv_data.len] = 0x08; // length
    m_enc_advdata[m_adv_data.adv_data.len+1] = BLE_GAP_AD_TYPE_LE_BLUETOOTH_DEVICE_ADDRESS;
    
    m_enc_advdata[m_adv_data.adv_data.len+2] = (uint8_t)((serial_number_msb >>  8) & 0xFF);
    m_enc_advdata[m_adv_data.adv_data.len+3] = (uint8_t)((serial_number_msb >>  0) & 0xFF);
    m_enc_advdata[m_adv_data.adv_data.len+4] = (uint8_t)((serial_number_lsb >> 24) & 0xFF);
    m_enc_advdata[m_adv_data.adv_data.len+5] = (uint8_t)((serial_number_lsb >> 16) & 0xFF);
    m_enc_advdata[m_adv_data.adv_data.len+6] = (uint8_t)((serial_number_lsb >>  8) & 0xFF);
    m_enc_advdata[m_adv_data.adv_data.len+7] = (uint8_t)((serial_number_lsb >>  0) & 0xFF);
    m_enc_advdata[m_adv_data.adv_data.len+8] = 0xFF; //dummy byte

    m_adv_data.adv_data.len += (0x08 + 1); // (actual_length + ADV_AD_TYPE_FIELD_SIZE(1))

    return sd_ble_gap_adv_set_configure(&m_adv_handle, &m_adv_data, p_adv_params);

The type BLE_GAP_AD_TYPE_LE_BLUETOOTH_DEVICE_ADDRESS is only working with a length of 8 bytes.

6 Bytes MAC, 1 byte BLE_GAP_AD_TYPE_LE_BLUETOOTH_DEVICE_ADDRESS, 1 byte ?!?

This entry is needed, otherwise the function call will get a length error in the SDK.

 m_enc_advdata[m_adv_data.adv_data.len+8] = 0xFF;

but the local ble address with this function only returns 6 bytes, which I see in the nordic BLE app.

sd_ble_gap_addr_get

Question: What is this 7th Byte for and from where can I get it, what is the function and meaning of this "dummy" byte?

  • Hi,

    The last bit set the address type. This is how we encode the address in the nRF5-SDK:

    static ret_code_t ble_device_addr_encode(uint8_t  * p_encoded_data,
                                             uint16_t * p_offset,
                                             uint16_t   max_size)
    {
        ret_code_t     err_code;
        ble_gap_addr_t device_addr;
    
        // Check for buffer overflow.
        if (((*p_offset) + AD_TYPE_BLE_DEVICE_ADDR_SIZE) > max_size)
        {
            return NRF_ERROR_DATA_SIZE;
        }
    
        // Get BLE address.
        err_code = sd_ble_gap_addr_get(&device_addr);
        VERIFY_SUCCESS(err_code);
    
        // Encode LE Bluetooth Device Address.
        p_encoded_data[*p_offset]  = (uint8_t)(AD_TYPE_FIELD_SIZE +
                                                   AD_TYPE_BLE_DEVICE_ADDR_DATA_SIZE);
        *p_offset                 += AD_LENGTH_FIELD_SIZE;
        p_encoded_data[*p_offset]  = BLE_GAP_AD_TYPE_LE_BLUETOOTH_DEVICE_ADDRESS;
        *p_offset                 += AD_TYPE_FIELD_SIZE;
        memcpy(&p_encoded_data[*p_offset], &device_addr.addr[0], BLE_GAP_ADDR_LEN);
        *p_offset                 += BLE_GAP_ADDR_LEN;
        if (BLE_GAP_ADDR_TYPE_PUBLIC == device_addr.addr_type)
        {
            p_encoded_data[*p_offset] = AD_TYPE_BLE_DEVICE_ADDR_TYPE_PUBLIC;
        }
        else
        {
            p_encoded_data[*p_offset] = AD_TYPE_BLE_DEVICE_ADDR_TYPE_RANDOM;
        }
        *p_offset += AD_TYPE_BLE_DEVICE_ADDR_TYPE_SIZE;
    
        return NRF_SUCCESS;
    }

Related