Bluetooth:Peripheral White List¶
1 功能概述¶
通过设置白名单过滤地址对蓝牙连接请求根据设备mac地址进行过滤,在白名单中的则会进行连接请求响应。
2 环境要求¶
board: 支持 BLE 的蓝牙设备
uart(option): 用来显示串口log
测试软件: nRF Connect
3 编译和烧录¶
例程位置:zephyr\samples_panchip\bluetooth\peripheral_white_list
使用 ZAL 工具可以对其进行编译、烧录、打开 VS Code 调试等操作。关于 ZAL 工具的详细介绍请参考:Zephyr APP Launcher 工具介绍。
4 演示说明¶
准备1块板卡烧录
peripheral_white_list
Try to load HW calibration data.. DONE. - Chip Type : 0x80 - Chip CP Version : None - Chip FT Version : 8 - Chip MAC Address : D0000C0CBBF5 - Chip Flash UID : 32313334320EAC834330FFFFFFFFFFFF - Chip Flash Size : 1024 KB *** Booting Zephyr OS build zephyr-v2.7.0-1906-g5b63249e *** *** llcontroller-v1.0.0-06ff3d90 *** goon disable controller initializing.... tx power changed to 0 dBm Bluetooth initialized Add to white list success Advertising successfully started [00:00:00.001,000] <wrn> bt_hci_core: Failed to set device name (-12) [00:00:00.071,000] <inf> bt_hci_core: Identity: CA:03:C7:B6:D3:8A (random) [00:00:00.071,000] <inf> bt_hci_core: HCI: version 5.1 (0x0a) revision 0x0003, manufacturer 0x07d1 [00:00:00.071,000] <inf> bt_hci_core: LMP: version 5.1 (0x0a) subver 0x0000 [00:00:00.076,000] <inf> bt_adv: Start Advertising as CA:03:C7:B6:D3:8A (random)
此时用
nrf connect
可以扫描到设备,但是无法去连接设备,因为代码块中的设置的白名单地址默认是和手机不一致的。#if (IS_ENABLED(CONFIG_BT_FILTER_ACCEPT_LIST)) bt_addr_le_t whilte_list_addr = { .type = BT_ADDR_LE_RANDOM, .a = { { 0x47, 0xCF, 0xEE, 0xAC, 0xE6, 0xD4 } }, }; #endif #if (IS_ENABLED(CONFIG_BT_FILTER_ACCEPT_LIST)) /* adv_param.options |= BT_LE_ADV_OPT_FILTER_SCAN_REQ; */ adv_param.options |= BT_LE_ADV_OPT_FILTER_CONN; /*此处添加白名单地址*/ err = bt_le_filter_accept_list_add(&whilte_list_addr); if (err) { printk("Add to white list failed (err %d)", err); } else { printk("Add to white list success\n"); } #endif
此时我们可以先将prj.conf文件中的
CONFIG_BT_FILTER_ACCEPT_LIST=y
设置为CONFIG_BT_FILTER_ACCEPT_LIST=n
,此时白名单地址失效,手机是可以连接上设备的,此时设备串口log会打印出手机的蓝牙mac地址。Try to load HW calibration data.. DONE. - Chip Type : 0x80 - Chip CP Version : None - Chip FT Version : 8 - Chip MAC Address : D0000C0CBBF5 - Chip Flash UID : 32313334320EAC834330FFFFFFFFFFFF - Chip Flash Size : 1024 KB *** Booting Zephyr OS build zephyr-v2.7.0-1906-g5b63249e *** *** llcontroller-v1.0.0-06ff3d90 *** goon disable controller initializing.... tx power changed to 0 dBm Bluetooth initialized Advertising successfully started delta:3 = next-current[00:00:00.001,000] <wrn> bt_hci_core: Failed to set device name (-12) [00:00:00.071,000] <inf> bt_hci_core: Identity: CA:11:98:25:E4:B1 (random) [00:00:00.071,000] <inf> bt_hci_core: HCI: version 5.1 (0x0a) revision 0x0003, manufacturer 0x07d1 [00:00:00.071,000] <inf> bt_hci_core: LMP: version 5.1 (0x0a) subver 0x0000 [00:00:00.075,000] <inf> bt_adv: Start Advertising as CA:11:98:25:E4:B1 (random) Connected: [Init DEVICE]: 5A:A8:13:05:0E:8B (random) /*手机蓝牙Mac地址*/
此时我们将代码中的过滤白名单mac地址重新设置为手机蓝牙mac地址,在prj.conf文件中重新使能
CONFIG_BT_FILTER_ACCEPT_LIST=y
白名单过滤配置,然后重新下载设备即可正常接收手机蓝牙连接请求。
#if (IS_ENABLED(CONFIG_BT_FILTER_ACCEPT_LIST))
bt_addr_le_t whilte_list_addr = {
.type = BT_ADDR_LE_RANDOM,
.a = { { 0x8B, 0x0E, 0x05, 0x13, 0xA8, 0x5A} }, /*注意此处设置白名单蓝牙mac要使用倒序,因为该芯片为小端模式*/
};
#endif
5 RAM/Flash资源使用情况¶
Memory region Used Size Region Size %age Used
FLASH: 85468 B 384 KB 21.74%
SRAM: 28026 B 50 KB 54.74%