Nordic nRF5 SDK 学习笔记之十一, 获取 Peripheral 蓝牙 MAC 地址及重新设定

软件: nRF SKD Ver 15.2, S132 SoftDevice API


获取 Peripheral 蓝牙 MAC 地址

void get_MAC_address_with_HEX(char * str_MAC)
{
    ble_gap_addr_t		addr;
    uint32_t err_code = sd_ble_gap_addr_get( &addr);
    APP_ERROR_CHECK(err_code);
    sprintf(str_MAC,"%02X%02X%02X%02X%02X%02X",addr.addr[5],addr.addr[4],addr.addr[3],addr.addr[2],addr.addr[1],addr.addr[0]);

    NRF_LOG_INFO("MAC ADDRESS: %02X,%02X,%02X,%02X,%02X,%02X!",addr.addr[0],addr.addr[1],addr.addr[2],addr.addr[3],addr.addr[4],addr.addr[5]);
}

设定 Peripheral 蓝牙 MAC 地址


void change_MAC_address( ble_gap_addr_t	addr)
{
    uint32_t err_code = sd_ble_gap_addr_set( &addr );
    APP_ERROR_CHECK(err_code);
}

官方函数原文

uint32_t sd_ble_gap_addr_get ( ble_gap_addr_t *  p_addr )  

Get local Bluetooth identity address.

Note

This will always return the identity address irrespective of the privacy settings, i.e. the address type will always be either BLE_GAP_ADDR_TYPE_PUBLICor BLE_GAP_ADDR_TYPE_RANDOM_STATIC.

Parameters

[out] p_addr Pointer to address structure to be filled in.

Return values

NRF_SUCCESS Address successfully retrieved.
NRF_ERROR_INVALID_ADDR Invalid or NULL pointer supplied.

uint32_t sd_ble_gap_addr_set ( ble_gap_addr_t const *  p_addr )  

Set the local Bluetooth identity address.

   The local Bluetooth identity address is the address that identifies this device to other peers.
   The address type must be either @ref BLE_GAP_ADDR_TYPE_PUBLIC or @ref BLE_GAP_ADDR_TYPE_RANDOM_STATIC.

Note

The identity address cannot be changed while advertising, scanning or creating a connection.

This address will be distributed to the peer during bonding. If the address changes, the address stored in the peer device will not be valid and the ability to reconnect using the old address will be lost.

By default the SoftDevice will set an address of type BLE_GAP_ADDR_TYPE_RANDOM_STATIC upon being enabled. The address is a random number populated during the IC manufacturing process and remains unchanged for the lifetime of each IC.

Relevant Message Sequence Charts

Advertising

Parameters

[in] p_addr Pointer to address structure.

Return values

NRF_SUCCESS Address successfully set.
NRF_ERROR_INVALID_ADDR Invalid pointer supplied.
BLE_ERROR_GAP_INVALID_BLE_ADDR Invalid address.
NRF_ERROR_BUSY The stack is busy, process pending events and retry.
NRF_ERROR_INVALID_STATE The identity address cannot be changed while advertising, scanning or creating a connection.

猜你喜欢

转载自blog.csdn.net/weixin_42396877/article/details/86172544