步骤1:更新 “RW007” 模块的固件
由于默认RW007
模块的固件不支持BLE
主机功能,所以需要使用相关的升级工具进行升级。具体操作请参考附件中对应的文档进行操作。
步骤2:更新主机的 “RW007” 软件驱动包
由于v2.0.1
版本的RW007
软件驱动包中,不包括BLE
功能,所以需要手动更新RW007
的软件驱动包,请使用提供的软件驱动包替换到主机软件包的目录下。
步骤3:测试 “RW007” 模块的 “BLE” 功能
根据前面的操作步骤,确保RW007
模块自身的固件已经更新和RW007
驱动软件包已更新替换到使用的工程中,在提供的软件驱动包中,已经包含了部分 BLE
功能的测试命令,可以在串口调试中通过msh
命令,进行调试。
1msh />rw007_ble 2[rw007_ble command] 3 4rw007_ble help 5rw007_ble init central/peripheral Note: init ble mode 6rw007_ble get_addr Note: get ble address 7rw007_ble update_params Note: update connect parameters(no support) 8rw007_ble scan Note: scan ble slave 9rw007_ble stop_scan Note: stop scan10rw007_ble connect xx:xx:xx:xx:xx:xx Note: use slave addr to connect11rw007_ble disconnect [conn_handle] Note: disconnect slave12rw007_ble get_server Note: discover all server(no support)13rw007_ble get_char Note: discover all description(no support)14rw007_ble get_disc Note: discover all characteristic(no support)15rw007_ble mtu_exch Note: ble mtu exchange(no support)16rw007_ble notify Note: enable ble notify(no support)17rw007_ble write Note: ble write data(no support)18rw007_ble read Note: ble read data(no support)19rw007_ble notify_change [conn_handle] [char_value] [UUID] Note: ble notify configure by uuid20rw007_ble write_uuid [conn_handle] [UUID] [data] Note: ble write data by uuid21rw007_ble read_uuid Note: ble read data by uuid2223msh />
如上可知,有部分BLE
功能暂时还没支持的,下面将详细说一说BLE
测试命令的使用。
BLE 功能初始化
BLE
功能初始化函数,使用 BLE
功能必须调用。
1.RW007
BLE 设备初始化为主机设备。
1rw007_ble init central
测试示例
1msh />rw007_ble init central2122 - ble_cmd_init3start ble central4msh />
2.RW007
BLE 设备初始化为从机设备
注意:目前不支持使用从机模式。
1rw007_ble init peripheral
BLE 功能获取本机的BLE设备地址
由于BLE设备的地址分为public
公共地址和random
随机地址。
1rw007_ble get_addr
测试示例:
1msh />rw007_ble get_addr2273 - ble_cmd_get_addr3msh />resp_type: 0, len: 124RW007_BLE_RSP_TYPE_ADDR_GET5public_id_addr = 48:00:42:8c:47:c96random_id_addr = 00:04:5c:43:00:00
BLE 功能扫描设备
目前RW007
BLE 功能支持通过 scan
命令,扫描周围的BLE
设备。
1.执行扫描周围BLE
设备
1rw007_ble scan
测试示例:
1msh />rw007_ble scan2176 - ble_cmd_scan3msh />resp_type: 1, len: 654RW007_BLE_NTF_TYPE_DISCOVER5received advertisement; event_type=0 rssi=-60 addr_type=0 addr=4c:ed:fb:00:04:b16resp_type: 1, len: 56
这里的 addr
地址内容可以为后面连接设备使用,由于目前没有直接显示对应 BLE 设备的名称,需要先人工确定 BLE 设备的地址。
2.停止扫描周围BLE
设备
该指令用于 RW007
模块正在执行scan
扫描操作过程中,马上停止扫描的动作,可以执行此命令。
1rw007_ble stop_scan
BLE 功能设备连接
目前 RW007
BLE 功能设备支持通过 BLE
设备的类mac
地址来连接设备,该命令仅支持主机模式去连接从机设备的类mac
地址,连接成功后会有对应的conn_handle
值,这个conn_handle
值在其他功能沿用。
1rw007_ble connect xx:xx:xx:xx:xx:xx
测试示例:
1msh />rw007_ble connect 4c:ed:fb:00:04:b1 2198 - ble_cmd_connect 3str_addr: 4c:ed:fb:00:04:b1 4mac addr: b1: 4: 0:fb:ed:4c 5msh />resp_type: 1, len: 52 6RW007_BLE_NTF_TYPE_CONNECT 7type: 0, status: 0, conn_handle: 1 8 9resp_type: 1, len: 4410RW007_BLE_NTF_TYPE_CONNECT_DESC11our_id_addr: c9:47:8c:42:00:4812peer_id_addr: b1:04:00:fb:ed:4c
BLE 功能设备断开连接
目前RW007
BLE 功能设备支持主动断开从设备的连接。
1rw007_ble disconnect [conn_handle]
其中 conn_handle
与 connect
连接时的需要一致。
1msh />rw007_ble disconnect 12341 - ble_cmd_disconnect3msh />resp_type: 1, len: 524RW007_BLE_NTF_TYPE_DISCONN
BLE 功能写数据
目前RW007
BLE 功能写数据,支持通过指定UUID
来写对应的数据。
1rw007_ble write_uuid [conn_handle] [UUID] [data]
指令说明:[conn_handle]
为 RW007
连接设备时生成的对应值,[UUID]
为对应 BLE
设备的特征参数,[data]
为需要发送的数据。
测试示例:
1msh />rw007_ble write_uuid 1 ffe1 Hello,RT-Thread....2402 - ble_cmd_gattc_write_by_uuid3write conn_hanle:1 uuid:ffe1 data:Hello,RT-Thread....4write rc:05msh />
figure1
注意:目前新的RW007 驱动中已经支持通过 128 位的 UUID
进行写数据操作
测试示例:
1msh />rw007_ble write_uuid 1 ebe0ccbe7a0a4b0c8a1a6ff2997da3a6 002data: 003write conn_hanle:1 uuid:ebe0ccbe7a0a4b0c8a1a6ff2997da3a64write rc:0
说明:测试示例中的 [conn_handle]
为 1,[UUID]
为ebe0ccbe7a0a4b0c8a1a6ff2997da3a6
,[data]
为 0x00
(这里是使用 hex 值)
BLE 功能更新notify 参数
目前 RW007
BLE 功能支持修改接收 notify
参数。
1rw007_ble notify_change [conn_handle] [char_value] [UUID]
参数说明:
[conn_handle]
为 RW007
连接设备时生成的对应值
[char_value]
为配置参数,具体如下
10:disable indication¬ification21:enable notification,disable indication32:enable indication, disable notification
[UUID]
为对应 BLE
设备的特征参数值。
BLE 功能关于广播包类型的说明
目前 RW007
BLE 功能中可以关于广播包 event
的类型可以分为下面几种。
12#define BLE_HCI_ADV_RPT_EVTYPE_ADV_IND (0)3#define BLE_HCI_ADV_RPT_EVTYPE_DIR_IND (1)4#define BLE_HCI_ADV_RPT_EVTYPE_SCAN_IND (2)5#define BLE_HCI_ADV_RPT_EVTYPE_NONCONN_IND (3)6#define BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP (4)
广播包结构如下:
1广播包结构描述: 2struct ble_gap_disc_desc { 3 uint8_t event_type; 4 uint8_t length_data; 5 ble_addr_t addr; 6 int8_t rssi; 7 uint8_t *data; 8 9 /***10 * LE direct advertising report fields; direct_addr is BLE_ADDR_ANY if11 * direct address fields are not present.12 */13 ble_addr_t direct_addr;14};1516通过event_type区分出不同的广播包类型,所有类型分为如下几种:17/* Advertising report */180 : 普通广播数据包191 : 直接广播包 202 : 扫描请求包 213 : 不可连接广播指示224 : 扫描响应数据包
因此,在设备的scan
扫描回应中可以通过对应的event_type
来判定是哪种广播包的类型。
在 RW007
的驱动代码中,可以通过下面的event_type
来判断是哪种类型的广播包,可以让用户自行实现对应功能。
1case RW007_BLE_NTF_TYPE_DISC: 2 { 3 rt_kprintf("RW007_BLE_NTF_TYPE_DISCOVER\n"); 4 .... 5 switch(event_type) 6 { 7 case BLE_HCI_ADV_RPT_EVTYPE_ADV_IND: 8 9 break;10 case BLE_HCI_ADV_RPT_EVTYPE_DIR_IND:1112 break;13 case BLE_HCI_ADV_RPT_EVTYPE_SCAN_IND:1415 break;16 case BLE_HCI_ADV_RPT_EVTYPE_NONCONN_IND:1718 break;19 case BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP:2021 break;22 }23 ....24 }
测试示例:
1.初始化 RW007
BLE 功能为主机模式。
1msh /> rw007_ble init central2263 - ble_cmd_init3start ble central4msh />
2.执行BLE
扫描指令
1msh />rw007_ble scan 2318 - ble_cmd_scan 3msh />resp_type: 1, len: 83 4RW007_BLE_NTF_TYPE_DISCOVER 5received advertisement; event_type=3 rssi=-56 addr_type=1 addr=41:ef:77:50:2b:29 6mfg_data = 1e ff 06 00 01 09 20 02 5b 5d cd 33 a4 8c c7 c5 36 ac de 12 ab 17 87 89 e1 84 e9 19 ec c4 ad 7 8resp_type: 1, len: 83 9RW007_BLE_NTF_TYPE_DISCOVER10received advertisement; event_type=3 rssi=-65 addr_type=1 addr=3c:2e:33:ea:8d:aa11mfg_data = 1e ff 06 00 01 09 20 02 28 4c cf 9b a7 f2 7d f6 c7 7e d9 54 ac dd 91 a6 ee 0d 22 43 e1 8f e3 1213resp_type: 1, len: 8314RW007_BLE_NTF_TYPE_DISCOVER15received advertisement; event_type=3 rssi=-59 addr_type=1 addr=42:18:ab:80:5c:1116mfg_data = 1e ff 06 00 01 09 20 02 eb bf cf 15 97 aa 50 7f a4 57 25 d9 21 35 71 eb 6f aa a1 56 5c d5 e2
3.观察event_type=
可以确定是哪种广播包类型。
1RW007_BLE_NTF_TYPE_DISCOVER 2received advertisement; event_type=0 rssi=-64 addr_type=1 addr=61:63:46:c3:49:99 3mfg_data = 02 01 1a 02 0a 0c 0b ff 4c 00 10 06 00 19 1d 6d 63 18 4 5resp_type: 1, len: 52 6RW007_BLE_NTF_TYPE_DISCOVER 7received advertisement; event_type=4 rssi=-64 addr_type=1 addr=61:63:46:c3:49:99 8mfg_data = 910resp_type: 1, len: 7311RW007_BLE_NTF_TYPE_DISCOVER12received advertisement; event_type=0 rssi=-73 addr_type=0 addr=91:21:52:00:4e:b813mfg_data = 14 ff 4c 00 07 0f 00 02 20 91 21 52 00 4e b8 f5 58 58 3c 39 00
BLE 功能读数据
目前 RW007
支持通过 UUID
读取数据的功能
1rw007_ble read_uuid [conn_handle] [UUID]
参数说明:
[conn_handle]
为 RW007
连接设备时生成的对应值
[UUID]
为对应 BLE
设备的特征参数。
测试示例:
1msh />rw007_ble read_uuid 1 2a242read conn_hanle:1 uuid:2a243read rc:04msh />ble data input packet resp_type: 0, len: 185RW007_BLE_RSP_TYPE_READ6connect:1 attr_handle:14 uuid: 24 2a read data:700000000: 4C 59 57 53 44 30 33 4D 4D 43 00 LYWSD03MMC.
说明:这里的 [conn_handle]
为 1,[UUID]
为 2a24
,读取到的数据为 BLE 设备的名称。
目前 RW007 模块已经支持读取 128 位的 UUID 的功能。
测试示例:
1msh />rw007_ble read_uuid 1 ebe0ccbe7a0a4b0c8a1a6ff2997da3a62read conn_hanle:1 uuid:ebe0ccbe7a0a4b0c8a1a6ff2997da3a63read rc:04msh />ble data input packet resp_type: 0, len: 225RW007_BLE_RSP_TYPE_READ6connect:1 attr_handle:51 uuid: a6 a3 7d 99 f2 6f 1a 8a 0c 4b 0a 7a be cc e0 eb read data:700000000: 00
说明:
这里的 [conn_handle]
为 1,[UUID]
为128位的 ebe0ccbe7a0a4b0c8a1a6ff2997da3a6
,读取到的数据值为 00
以上的代码可以在RW007
的软件驱动包中找到对应的位置,想深入了解的可以仔细阅读。
总结
经过前面的学习,算是摸了一遍 RW007 的 BLE 功能,个人觉得 RW007 的 BLE 主机功能还是挺有意思的,使用也比较简单,像我这种没有任何 BLE 相关知识的也能基本知道怎样通过 RW007 的 BLE 主机功能去扫描附近的蓝牙BLE 设备,如何去连接扫描到的蓝牙 BLE 设备,如何通过蓝牙 BLE 设备的 UUID 去写数据。总得来说,使用算是比较方便的,但是目前貌似仅支持BLE 的主机功能,既然是作为主机后面再继续测试一下BLE 透传数据的功能。
注意事项:
1.请确保使用的RW007
模块带BLE
功能。
2.RW007
BLE 功能是基于SPI
模式的,请确保RW007
模块自身的版本为 2.0.1
以上的版本。
原作者:yingjian