我希望你能帮我弄清楚如何使用Bluez从HM-10 BLE模块中读取数据。为什么这对我来说是个问题?我无法读取或写入正常工作,我的最终目标是使用Ian Harvey的bluepy库,它建立在Bluez堆栈上。任何帮助表示赞赏。谢谢!
连接到arduino nano的HM-10会说“Foobar”,等一秒钟,说“Barfoo”,等一秒,然后重复。 我的iOS蓝牙串行应用程序(在应用程序商店中名为“Serial”)可以正确选择它。硬件:Raspberry Pi ZeroW 内核:4.9.68+ Bluez:5。50(2018年6月3日发布)
我无法使用bluetoothctl来阅读“Foobar”和“Barfoo”。
pi@raspberrypi:~ $ bluetoothctl
[bluetooth]# power on
[bluetooth]# connect 34:15:13:87:98:37
[DSDTECH HM-10]# menu gatt
[DSDTECH HM-10]# select-attribute 0000ffe1-0000-1000-8000-00805f9b34fb
[DSDTECH HM-10:/service0010/char0011]# read
[CHG] Attribute /org/bluez/hci0/dev_34_15_13_87_98_37/service0010/char0011 Value:
34 15 13 87 98 37 4....7
34 15 13 87 98 37 4....7
事实上,在另一个终端中使用btmon($sudo btmon
),我可以看到我的覆盆子pi零W正在看到这些值。以下条目每隔一秒重复一次。
> ACL Data RX: Handle 64 flags 0x02 dlen 15 #278 [hci0] 339.880027
ATT: Handle Value Notification (0x1b) len 10
Handle: 0x0012
Data: 426172666f6f0d0a
> ACL Data RX: Handle 64 flags 0x02 dlen 15 #279 [hci0] 340.292455
ATT: Handle Value Notification (0x1b) len 10
Handle: 0x0012
Data: 466f6f6261720d0a
426172666f6f0d0a = Barfoo(hex2ascii)
466f6f6261720d0a = Foobar(hex2ascii)
如果您了解HM-10,您知道ffe1是您应该用于uart数据传输的。我在我的自制的Android应用程序中编码了ffe1,它作为主人与HM-10配对。但是,我想检查其他属性给我的东西。
[DSDTECH HM-10:/service0010/char0011]# select-attribute 00002902-0000-1000-8000-00805f9b34fb
[DSDTECH HM-10:/service0010/char0011/desc0013]# read
[CHG] Attribute /org/bluez/hci0/dev_34_15_13_87_98_37/service0010/char0011/desc0013 Value:
00 00 ..
00 00 ..
[DSDTECH HM-10:/service0010/char0011/desc0013]# list-attributes
Primary Service
/org/bluez/hci0/dev_34_15_13_87_98_37/service000c
00001801-0000-1000-8000-00805f9b34fb
Generic Attribute Profile
Characteristic
/org/bluez/hci0/dev_34_15_13_87_98_37/service000c/char000d
00002a05-0000-1000-8000-00805f9b34fb
Service Changed
Descriptor
/org/bluez/hci0/dev_34_15_13_87_98_37/service000c/char000d/desc000f
00002902-0000-1000-8000-00805f9b34fb
Client Characteristic Configuration
Primary Service
/org/bluez/hci0/dev_34_15_13_87_98_37/service0010
0000ffe0-0000-1000-8000-00805f9b34fb
Unknown
Characteristic
/org/bluez/hci0/dev_34_15_13_87_98_37/service0010/char0011
0000ffe1-0000-1000-8000-00805f9b34fb
Unknown
Descriptor
/org/bluez/hci0/dev_34_15_13_87_98_37/service0010/char0011/desc0013
00002902-0000-1000-8000-00805f9b34fb
Client Characteristic Configuration
Descriptor
/org/bluez/hci0/dev_34_15_13_87_98_37/service0010/char0011/desc0014
00002901-0000-1000-8000-00805f9b34fb
[DSDTECH HM-10:/service0010/char0011/desc0013]# select-attribute /org/bluez/hci0/dev_34_15_13_87_98_37/service0010/char0011/desc0014
# I could not get the uuid to work after selecting 00002902, and do not know how to exit out of an attribute.
# This corresponds to uuid 00002901
[DSDTECH HM-10:/service0010/char0011/desc0014]# read
[CHG] Attribute /org/bluez/hci0/dev_34_15_13_87_98_37/service0010/char0011/desc0014 Value:
77 77 77 2e 6a 6e 68 75 61 6d 61 6f 2e 63 6e www.jnhuamao.cn
77 77 77 2e 6a 6e 68 75 61 6d 61 6f 2e 63 6e www.jnhuamao.cn
[DSDTECH HM-10:/service0010/char0011/desc0014]# select-attribute /org/bluez/hci0/dev_34_15_13_87_98_37/service0010
[DSDTECH HM-10:/service0010]# read
Unable to read attribute /org/bluez/hci0/dev_34_15_13_87_98_37/service0010
[DSDTECH HM-10:/service0010]# select-attribute /org/bluez/hci0/dev_34_15_13_87_98_37/service000c/char000d
# This corresponds to uuid 00002a05-0000-1000-8000-00805f9b34fb
[DSDTECH HM-10:/service000c/char000d]# read
Failed to read: org.bluez.Error.NotPermitted
[DSDTECH HM-10:/service000c/char000d]# select-attribute /org/bluez/hci0/dev_34_15_13_87_98_37/service000c/char000d/desc000f
[CHG] Attribute /org/bluez/hci0/dev_34_15_13_87_98_37/service000c/char000d/desc000f Value:
02 00 ..
02 00 ..
有趣的是,这与制造商数据的响应相同,如info命令所示。
[bluetooth]# info 34:15:13:87:98:37
Device 34:15:13:87:98:37 (public)
Name: DSDTECH HM-10
Alias: DSDTECH HM-10
Paired: no
Trusted: yes
Blocked: no
Connected: no
LegacyPairing: no
UUID: Unknown (0000ffe0-0000-1000-8000-00805f9b34fb)
ManufacturerData Key: 0x4d48
ManufacturerData Value:
34 15 13 87 98 37 4....7
ServiceData Key: 0000b000-0000-1000-8000-00805f9b34fb
ServiceData Value:
00 00 00 00 ....
RSSI: -56
TxPower: 0
当我使用不同的nano / HM-10组合时,这个值与@|...x
不同,对于信息/制造数据和uuid ffe1的读取都是如此。
此外,当我最初连接到设备时,btmon显示
@ MGMT Event: Device Found (0x0012) plen 57 {0x0001} [hci0] 67.415544
LE Address: 34:15:13:87:98:37 (OUI 34-15-13)
RSSI: -62 dBm (0xc2)
Flags: 0x00000000
Data length: 43
Flags: 0x06
LE General Discoverable Mode
BR/EDR Not Supported
Company: not assigned (19784)
Data: 341513879837
Service Data (UUID 0xb000): 00000000
16-bit Service UUIDs (partial): 1 entry
Unknown (0xffe0)
TX power: 0 dBm
Name (complete): DSDTECH HM-10
341513879837 = 47(hex2ascii)(有四个方块,我无法正常显示。这些与四个时段对齐)
答案 0 :(得分:1)
我对Bluez一无所知,但你收到的是广告数据,而不是来自HM10的Tx数据。
要阅读数据,您必须使用以下服务:" 0000 ffe0 -0000-1000-8000-00805f9b34fb"和
特征:" 0000 ffe1 -0000-1000-8000-00805f9b34fb"。
您必须将通知描述符写入此特征。
要获取您不必读取的数据,数据位于通知有效负载中,您可以在特性更改事件中读取数据, 或者在Bleuez中称之为什么。
答案 1 :(得分:1)
btmon日志显示来自HM-10设备的数据采用通知形式。在BLE中,您有三种数据传输方式;读,写和通知。读取是指GATT客户端(在您的情况下为Bluez)从服务特征或GATT服务器的服务描述符读取数据,前提是允许对那些相应的特征或描述符进行读操作。写操作是GATT客户端通过写入其服务特征或描述符将数据发送到GATT服务器(HM-10)。
GATT服务器自己向GATT客户端发送数据的唯一方法是通知。但GATT客户端需要在建立连接后在GATT服务器上启用通知。可以通过写入GATT服务器的客户端特性配置描述符(CCC)来完成启用通知。 CCC是蓝牙核心规范中定义的特殊GATT服务。
启用通知后,您将看到来自GATT服务器的数据。使用Bluez,您可以使用gatttool执行所有BLE操作。以下是一个例子:
例如,如果HM-10的蓝牙设备地址为03:0F:45:65:43:FF且您的设备hci接口地址为hci0,则下面的命令序列将启用通知:
gatttool -i hci0 -b 03:0F:45:65:43:FF -I
[03:0F:45:65:43:FF][LE]>connect
Attempting to connect to 03:0F:45:65:43:FF
Connection successful
# lists all other primary services
[03:0F:45:65:43:FF][LE]> primary
attr handle: 0x0001, end grp handle: 0x0005 uuid: 00001800-0000-1000-8000-00805f9b34fb
attr handle: 0x0006, end grp handle: 0x0009 uuid: 00001801-0000-1000-8000-00805f9b34fb
# lists all characteristics
[03:0F:45:65:43:FF][LE]> characteristics
handle: 0x0002, char properties: 0x02, char value handle: 0x0003, uuid: 00002a00-0000-1000-8000-00805f9b34fb
handle: 0x0004, char properties: 0x02, char value handle: 0x0005, uuid: 00002a01-0000-1000-8000-00805f9b34fb
# 2902 is UUID of CCC service
[03:0F:45:65:43:FF][LE]> char-read-uuid 2902
handle: 0x0009 value: 00 00
handle: 0x0019 value: 00 00
# Enable notifications
[03:0F:45:65:43:FF][LE]> char-write-req 0x0009 0100
Characteristic value was written successfully
[03:0F:45:65:43:FF][LE]> char-write-req 0x0019 0100
Characteristic value was written successfully