8762D如何在BLE设备名后面添加mac地址
比如蓝牙设备名显示为: Name(55 : AB)
- 先在广播参数中设置蓝牙设备名
uint8_t adv_data[31] =
{
/* Flags */
0x02, /* length */
GAP_ADTYPE_FLAGS, /* type="Flags" */
GAP_ADTYPE_FLAGS_LIMITED | GAP_ADTYPE_FLAGS_BREDR_NOT_SUPPORTED,
0x0C, //长度 = type + 蓝牙设备名(需要添加MAC地址的部分先设置为0x0)
GAP_ADTYPE_LOCAL_NAME_COMPLETE,
'N', 'a', 'm', 'e', 0x0, 0x0, 0x0, 0X0, 0x0, 0x0, 0x0,
};
- 获取本地的MAC地址
uint8_t get_mac_addr[6] = {0};
gap_get_param(GAP_PARAM_BD_ADDR, get_mac_addr); //获取蓝牙本地MAC地址
- 在设备名添加MAC地址
注意事项:由于获取的MAC地址是十六进制的, 手机上显示的MAC地址是UTF-8(ACSII),故要把十六进制转换为ACSII码
//获取高4位和低4位
#define HIGH_FOUR_BIT(x) ((uint8_t)((x) & 0xF0) >> 4)
#define LOW_FOUR_BIT(x) ((uint8_t)((x) & 0x0F))
//十六进制转换为ACSII显示
uint8_t hex_switch_to_acsii(uint8_t date_hex)
{
uint8_t temp_acsii = 0;
if(date_hex < 10)
{
temp_acsii = date_hex + '0';
}
else
{
temp_acsii = date_hex - 10 + 'A';
}
return temp_acsii;
}
//在广播数据中修改设备名,
adv_data[13] = '(';
adv_data[14] = hex_switch_to_acsii( HIGH_FOUR_BIT(get_mac_addr[1]));
adv_data[15] = hex_switch_to_acsii( LOW_FOUR_BIT(get_mac_addr[1]));
adv_data[16] = ':';
adv_data[17] = hex_switch_to_acsii( HIGH_FOUR_BIT(get_mac_addr[0]));
adv_data[18] = hex_switch_to_acsii( LOW_FOUR_BIT(get_mac_addr[0]));
adv_data[19] = ')';
//为了连接后可以通过Generic Access服务 Device Name获取设置名与广播中的设备名一致
uint8_t device_name[GAP_DEVICE_NAME_LEN] = "Name0000000";
device_name[4] = '(';
device_name[5] = hex_switch_to_acsii( HIGH_FOUR_BIT(get_mac_addr[1]));
device_name[6] = hex_switch_to_acsii( LOW_FOUR_BIT(get_mac_addr[1]));
device_name[7] = ':';
device_name[8] = hex_switch_to_acsii( HIGH_FOUR_BIT(get_mac_addr[0]));
device_name[9] = hex_switch_to_acsii( LOW_FOUR_BIT(get_mac_addr[0]));
device_name[10] = ')';
//设置参数
le_set_gap_param(GAP_PARAM_DEVICE_NAME, GAP_DEVICE_NAME_LEN, device_name);
le_adv_set_param(GAP_PARAM_ADV_DATA, sizeof(adv_data), (void *)adv_data);
``