0

我希望你能帮助我弄清楚如何使用 Bluez 从 HM-10 BLE 模块中读取数据。为什么这对我来说是个问题?我无法读取或写入以正常工作,我的最终目标是使用建立在顶部 Bluez 堆栈上的 Ian Harvey 的 bluepy 库。任何帮助表示赞赏。谢谢!

连接到 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 显示我正在接收“Foobar”和“Barfoo”

事实上,使用 btmon ($sudo btmon在另一个终端中),我可以看到我的树莓派零 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

@|...x当我使用不同的 nano/HM-10 组合时,对于信息/制造数据和 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) (有四个方块,我无法正确显示。这些与四个句点对齐)

4

2 回答 2

2

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
于 2018-06-16T04:09:09.927 回答
1

我对 Bluez 一无所知,但你收到的是广告数据,而不是来自 HM10 的 Tx 数据。要读取数据,您必须使用服务:“0000 ffe0 -0000-1000-8000-00805f9b34fb”和特征:“0000 ffe1 -0000-1000-8000-00805f9b34fb”。
您必须将通知描述符写入此特征。

要获取您不必读取的数据,数据位于通知有效负载中,您可以在特性更改事件中读取它,或者在 Bleuez 中调用它。

于 2018-06-11T18:06:53.510 回答