当前文档版本为 v0.7.0,您可以访问当前页面的 开发中 版本以获取最近可能的更新。

Bluetooth: Multi-roles

1 功能概述

此项目演示了蓝牙多角色的功能。可以通过 zephyr shell 控制设备执行广播、扫描、连接(主和从)等。

2 环境要求

  • board: pan108xxb5_evb

  • uart: 显示串口log和shell交互

  • 测试软件:SecureCRT,用于 shell 交互

  • 测试软件 (option): nRF Connect

执行多连接时,需要多个支持蓝牙的设备。

3 编译和烧录

例程位置:zephyr\samples_panchip\bluetooth\mult_roles

使用 ZAL 工具可以对其进行编译、烧录、打开 VS Code 调试等操作。关于 ZAL 工具的详细介绍请参考:Zephyr APP Launcher 工具介绍

4 演示说明

4.1 启动广播

uart:~$ ble adv conn_ind_start
Advertising successfully started

4.2 启动扫描

uart:~$ ble scan passive_start
Scanning successfully started
uart:~$ 00 > 37:09:A7:33:53:B6 (random) (RSSI -98) (ADV_NONCONN_IND)
01 > 39:E9:F4:88:25:21 (random) (RSSI -95) (ADV_NONCONN_IND)
02 > 3B:2C:F8:A6:0D:85 (random) (RSSI -95) (ADV_NONCONN_IND)
03 > 25:A0:8B:A9:CE:CB (random) (RSSI -98) (ADV_NONCONN_IND)
04 > 1A:E1:B2:2D:BC:1A (random) (RSSI -94) (ADV_NONCONN_IND)
05 > 74:AC:D5:CF:97:3A (random) (RSSI -92) (ADV_IND)
06 > 0A:6F:3B:F4:FC:77 (random) (RSSI -93) (ADV_NONCONN_IND)
07 > 00:20:21:05:27:FF (public) (RSSI -92) (ADV_IND)
08 > BB:CA:11:20:00:06 (public) (RSSI -91) (ADV_IND)
09 > 01:95:C8:38:2E:C1 (random) (RSSI -81) (ADV_NONCONN_IND)
10 > 7B:7B:2B:BD:A7:BA (random) (RSSI -81) (ADV_NONCONN_IND)
11 > 3E:18:64:B8:8A:B4 (random) (RSSI -92) (ADV_NONCONN_IND)
12 > 26:D1:B3:49:78:A5 (random) (RSSI -92) (ADV_NONCONN_IND)
13 > 12:1D:7C:92:53:B1 (random) (RSSI -85) (ADV_NONCONN_IND)
14 > 64:D6:45:C8:ED:3C (random) (RSSI -86) (ADV_IND)
15 > FF:FF:FF:FF:FF:FF (public) (RSSI -75) (ADV_IND)
16 > 21:88:3B:73:56:62 (random) (RSSI -94) (ADV_NONCONN_IND)
17 > 2B:46:04:9D:A9:08 (random) (RSSI -88) (ADV_NONCONN_IND)
18 > 1A:A0:5E:42:34:6E (random) (RSSI -88) (ADV_NONCONN_IND)
19 > 4E:E5:97:61:EB:3B (random) (RSSI -88) (ADV_IND)

需要注意的是:

  • 当前可以存储20条设备信息,当扫描到的设备信息存储满时,将自动终止扫描

  • 发起连接(ble conn create)时如果当前处于扫描状态,则会终止扫描后启动连接

4.3 显示设备信息

显示扫描、连接设备列表信息。

uart:~$ ble devs show
Detected List:
00 > 37:09:A7:33:53:B6 (random) (RSSI -98) (ADV_NONCONN_IND)
01 > 39:E9:F4:88:25:21 (random) (RSSI -95) (ADV_NONCONN_IND)
02 > 3B:2C:F8:A6:0D:85 (random) (RSSI -95) (ADV_NONCONN_IND)
03 > 25:A0:8B:A9:CE:CB (random) (RSSI -98) (ADV_NONCONN_IND)
04 > 1A:E1:B2:2D:BC:1A (random) (RSSI -94) (ADV_NONCONN_IND)
05 > 74:AC:D5:CF:97:3A (random) (RSSI -92) (ADV_IND)
06 > 0A:6F:3B:F4:FC:77 (random) (RSSI -93) (ADV_NONCONN_IND)
07 > 00:20:21:05:27:FF (public) (RSSI -92) (ADV_IND)
08 > BB:CA:11:20:00:06 (public) (RSSI -91) (ADV_IND)
09 > 01:95:C8:38:2E:C1 (random) (RSSI -81) (ADV_NONCONN_IND)
10 > 7B:7B:2B:BD:A7:BA (random) (RSSI -81) (ADV_NONCONN_IND)
11 > 3E:18:64:B8:8A:B4 (random) (RSSI -92) (ADV_NONCONN_IND)
12 > 26:D1:B3:49:78:A5 (random) (RSSI -92) (ADV_NONCONN_IND)
13 > 12:1D:7C:92:53:B1 (random) (RSSI -85) (ADV_NONCONN_IND)
14 > 64:D6:45:C8:ED:3C (random) (RSSI -86) (ADV_IND)
15 > FF:FF:FF:FF:FF:FF (public) (RSSI -75) (ADV_IND)
16 > 21:88:3B:73:56:62 (random) (RSSI -94) (ADV_NONCONN_IND)
17 > 2B:46:04:9D:A9:08 (random) (RSSI -88) (ADV_NONCONN_IND)
18 > 1A:A0:5E:42:34:6E (random) (RSSI -88) (ADV_NONCONN_IND)
19 > 4E:E5:97:61:EB:3B (random) (RSSI -88) (ADV_IND)
Connected List:
- Empty

4.4 与设备建立连接

在与设备建立连接时,需要先获取待连接设备的编号,可以通过 ble devs show 获取。

如下命令,表示将与 Detected List列表中编号为05的设备建立连接:

uart:~$ ble conn create 5
Connecting ...
uart:~$ Connect to 74:AC:D5:CF:97:3A (random) successful

5 其它说明

5.1 配置

项目配置文件: prj.conf,其中包含了 Bluetoothshelldebug等的一些配置。

其中支持的最大连接数量由如下参数配置:

CONFIG_BT_MAX_CONN=8

5.2 shell 命令

shell 支持的 ble 命令

ble - BLE commands
Subcommands:
  adv    :LE Advertising commands
  Subcommands:
    conn_ind_start     :LE Start Advertising with ADV_IND command
    nonconn_ind_start  :LE Start Advertising with ADV_NONCONN_IND command
    stop               :LE Stop Advertising command
  scan   :LE Scanning commands
  Subcommands:
    passive_start  :LE Start Passive Scanning command
    active_start   :LE Start Active Scanning command
    stop           :LE Stop Scanning command
  conn   :LE Connection commands
  Subcommands:
    create   :LE Create Connection command
    disconn  :Disconnect command
  devs   :LE Device Status commands
  Subcommands:
    show   :Display BLE Devices command
    clear  :Clear Detected List command
  reset  :Reset commands

6 RAM/Flash资源使用情况

Memory region         Used Size  Region Size  %age Used
FLASH:      114872 B       256 KB     43.82%
SRAM:       48648 B        50 KB     95.02%