【问题标题】:reading data from bluetooth thermometer with gatttool使用 gatttool 从蓝牙温度计读取数据
【发布时间】:2020-11-27 16:04:40
【问题描述】:

我偶然发现了这个问题:Reading Thermometer Data with Bluez Bluetooth Low Energy 并跟随它试图从我得到的蓝牙温度计中读取数据。

我能够使用此命令提取和读取所有句柄:

gatttool -b 00:11:22:33:44:55 -I
[00:11:22:33:44:55][LE]> connect
Connection successful
[00:11:22:33:44:55][LE]> primary
attr handle: 0x0001, end grp handle: 0x0007 uuid: 00001800-0000-1000-8000-00805f9b34fb
attr handle: 0x0008, end grp handle: 0x000b uuid: 00001801-0000-1000-8000-00805f9b34fb
attr handle: 0x000c, end grp handle: 0x001e uuid: 0000180a-0000-1000-8000-00805f9b34fb
attr handle: 0x001f, end grp handle: 0xffff uuid: 0000ffe0-0000-1000-8000-00805f9b34fb
[00:11:22:33:44:55][LE]> char-desc 0x0001 0x0001
handle: 0x0001, uuid: 00002800-0000-1000-8000-00805f9b34fb
[00:11:22:33:44:55][LE]> char-desc 0x0001 0x0007
handle: 0x0001, uuid: 00002800-0000-1000-8000-00805f9b34fb
handle: 0x0002, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0003, uuid: 00002a00-0000-1000-8000-00805f9b34fb
handle: 0x0004, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0005, uuid: 00002a01-0000-1000-8000-00805f9b34fb
handle: 0x0006, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0007, uuid: 00002a04-0000-1000-8000-00805f9b34fb
[00:11:22:33:44:55][LE]> char-read-hnd 01
Characteristic value/descriptor: 00 18 
[00:11:22:33:44:55][LE]> char-read-hnd 02
Characteristic value/descriptor: 02 03 00 00 2a 
[00:11:22:33:44:55][LE]> char-read-hnd 03
Characteristic value/descriptor: 54 68 65 72 6d 6f 42 65 61 63 6f 6e 
[00:11:22:33:44:55][LE]> char-read-hnd 04
Characteristic value/descriptor: 02 05 00 01 2a 
[00:11:22:33:44:55][LE]> char-read-hnd 05
Characteristic value/descriptor: 00 00 
[00:11:22:33:44:55][LE]> char-read-hnd 06
Characteristic value/descriptor: 02 07 00 04 2a 
[00:11:22:33:44:55][LE]> char-read-hnd 07
Characteristic value/descriptor: 50 00 a0 00 00 00 e8 03 
[00:11:22:33:44:55][LE]> char-desc 0x0008 0x000b
handle: 0x0008, uuid: 00002800-0000-1000-8000-00805f9b34fb
handle: 0x0009, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x000a, uuid: 00002a05-0000-1000-8000-00805f9b34fb
handle: 0x000b, uuid: 00002902-0000-1000-8000-00805f9b34fb
[00:11:22:33:44:55][LE]> char-read-hnd 08
Characteristic value/descriptor: 01 18 
[00:11:22:33:44:55][LE]> char-read-hnd 09
Characteristic value/descriptor: 20 0a 00 05 2a 
[00:11:22:33:44:55][LE]> char-read-hnd 0a
Error: Characteristic value/descriptor read failed: Attribute can't be read
[00:11:22:33:44:55][LE]> char-read-hnd 0b
Characteristic value/descriptor: 00 00 
[00:11:22:33:44:55][LE]> char-desc 0x000c 0x001e
handle: 0x000c, uuid: 00002800-0000-1000-8000-00805f9b34fb
handle: 0x000d, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x000e, uuid: 00002a23-0000-1000-8000-00805f9b34fb
handle: 0x000f, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0010, uuid: 00002a24-0000-1000-8000-00805f9b34fb
handle: 0x0011, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0012, uuid: 00002a25-0000-1000-8000-00805f9b34fb
handle: 0x0013, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0014, uuid: 00002a26-0000-1000-8000-00805f9b34fb
handle: 0x0015, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0016, uuid: 00002a27-0000-1000-8000-00805f9b34fb
handle: 0x0017, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0018, uuid: 00002a28-0000-1000-8000-00805f9b34fb
handle: 0x0019, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x001a, uuid: 00002a29-0000-1000-8000-00805f9b34fb
handle: 0x001b, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x001c, uuid: 00002a2a-0000-1000-8000-00805f9b34fb
handle: 0x001d, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x001e, uuid: 00002a50-0000-1000-8000-00805f9b34fb
[00:11:22:33:44:55][LE]> char-read-hnd 0c
Characteristic value/descriptor: 0a 18 
[00:11:22:33:44:55][LE]> char-read-hnd 0d
Characteristic value/descriptor: 02 0e 00 23 2a 
[00:11:22:33:44:55][LE]> char-read-hnd 0e
Characteristic value/descriptor: 5e 0b 00 00 00 00 f4 00 
[00:11:22:33:44:55][LE]> char-read-hnd 0f
Characteristic value/descriptor: 02 10 00 24 2a 
[00:11:22:33:44:55][LE]> char-read-hnd 10
Characteristic value/descriptor: 4d 6f 64 65 6c 20 4e 75 6d 62 65 72 
[00:11:22:33:44:55][LE]> char-read-hnd 11
Characteristic value/descriptor: 02 12 00 25 2a
[00:11:22:33:44:55][LE]> char-read-hnd 12
Characteristic value/descriptor: 53 65 72 69 61 6c 20 4e 75 6d 62 65 72 
[00:11:22:33:44:55][LE]> char-read-hnd 13
Characteristic value/descriptor: 02 14 00 26 2a 
[00:11:22:33:44:55][LE]> char-read-hnd 14
Characteristic value/descriptor: 46 69 72 6d 77 61 72 65 20 52 65 76 69 73 69 6f 6e 
[00:11:22:33:44:55][LE]> char-read-hnd 15
Characteristic value/descriptor: 02 16 00 27 2a 
[00:11:22:33:44:55][LE]> char-read-hnd 16

其中一些例如 4d 61 6e 75 66 61 63 74 75 72 65 72 20 4e 61 6d 65 在使用 HEX 到 ASCII 转换器转换时逐个字母读取 Manufacturer Name 但其中一些例如 02 1c 00 2a 2a read ** 带有一些空白方块

我还尝试将一些数字转换为十进制以尝试获得温度值,但没有成功。

每次我读取它们的值都保持不变,所以我猜这不是读取温度值的方法。

我是否必须以某种方式从这些句柄请求数据。如何从我上面的数据中找到温度值?

当我读到这个温度时,这个温度是 19.8 到 20.2°C,类似这样(如果它隐藏在我上面列出的那些值中的某个地方)

我只想从中读出温度值。

更新:

在打开scan on bluetoothctl 后,我从蓝牙温度计得到这个数据包:

[CHG] Device 00:11:22:33:44:55 RSSI: -81
[CHG] Device 00:11:22:33:44:55 TxPower: 0
[CHG] Device 00:11:22:33:44:55 ManufacturerData Key: 0x0011
[CHG] Device 00:11:22:33:44:55 ManufacturerData Value:
  00 00 55 44 33 22 11 00 24 0c 43 01 75 04 f3 5b  ..^.....$.C.u..[
  01 00                                            ..              
[CHG] Device 00:11:22:33:44:55 ManufacturerData Key: 0x0011
[CHG] Device 00:11:22:33:44:55 ManufacturerData Value:
  00 00 55 44 33 22 11 00 23 0c 43 01 79 04 02 5c  ..^.....#.C.y..\
  01 00                                            ..              
[CHG] Device 00:11:22:33:44:55 RSSI: -63
[CHG] Device 00:11:22:33:44:55 ManufacturerData Key: 0x0011
[CHG] Device 00:11:22:33:44:55 ManufacturerData Value:
  00 00 55 44 33 22 11 00 8c 01 2a 3d 00 00 34 01  ..^.......*=..4.
  c3 40 01 00                                      .@..            
[CHG] Device 00:11:22:33:44:55 RSSI: -81
[CHG] Device 00:11:22:33:44:55 ManufacturerData Key: 0x0011
[CHG] Device 00:11:22:33:44:55 ManufacturerData Value:
  00 00 55 44 33 22 11 00 24 0c 44 01 75 04 22 5c  ..^.....$.D.u."\
  01 00                                            ..              
[CHG] Device 00:11:22:33:44:55 RSSI: -54
[CHG] Device 00:11:22:33:44:55 ManufacturerData Key: 0x0011
[CHG] Device 00:11:22:33:44:55 ManufacturerData Value:
  00 00 55 44 33 22 11 00 8c 01 2a 3d 00 00 34 01  ..^.......*=..4.
  c3 40 01 00

让我们获取第一组数据:

[CHG] Device 00:11:22:33:44:55 ManufacturerData Value:
      00 00 55 44 33 22 11 00 24 0c 43 01 75 04 f3 5b  ..^.....$.C.u..[
      01 00

我注意到跳过了设备的前 2 个字节 55 44 33 22 11 00 MAC 地址,但相反。

之后24 0c 部分在其他集合中类似地重复,例如在下一个它的23 0c

接下来的 2 个字节 (43 01) 是我注意到房间温度变化时发生变化的字节,它们是代表温度的字节。这是我计算温度的方法。反转字节顺序 -> 01 43 -> 0x0143 -> 323 十进制 -> 323/16 -> 20.1875 向上取整。这是我温度计上的准确温度,我在温度越来越高时尝试过,它总是显示准确的温度。

接下来的两个 75 04: 0x0475 -> 1141 十进制 -> 1141/16 = 71.3125 向下舍入 71% -> 湿度显示在温度计上

这是正确的解释吗?

让我困惑的是第三组数据更长,并且数据包在这两者之间交替:

[CHG] Device 00:11:22:33:44:55 RSSI: -63
[CHG] Device 00:11:22:33:44:55 ManufacturerData Key: 0x0011
[CHG] Device 00:11:22:33:44:55 ManufacturerData Value:
  00 00 55 44 33 22 11 00 8c 01 2a 3d 00 00 34 01  ..^.......*=..4.
  c3 40 01 00

这是温度计发送的其他一些数据吗?

【问题讨论】:

    标签: bluetooth bluetooth-lowenergy bluez gatttool


    【解决方案1】:

    作为旁注,gatttool is deprecated 和当前支持执行此操作的工具是 bluetoothctl

    格式为 0000xxxx-0000-1000-8000-00805f9b34fb 的 GATT UUID 表示它们已被蓝牙 SIG 采用,您可以在 16-bit UUID Numbers Document 中查看它们所代表的含义

    还有通用的低功耗蓝牙扫描和探索工具,例如 nRF connect,它们可以帮助探索设备。

    从您发布的 GATT 信息中,我只能看到需要包含的通用内容,而没有看到任何关于温度的具体内容。

    广告数据包括制造商数据,它可以(正如其名称所暗示的那样)是制造商想要的任何东西。来自Core Specification Supplement

    这意味着您需要从制造商那里获取信息,或者根据您认为数据所说的内容进行逆向工作。由于您尚未分享有关设备广播的任何信息,看来您正朝着正确的方向前进。

    大多数蓝牙数据都是小端的,因此必须交换字节并不意外。像 iBeacon 和 Eddystone 这样的信标格式往往是大端,但它们是例外而不是规则。

    如果您想将数据放入某种代码中,您可能需要使用 D-Bus API。大多数语言都有 D-Bus 绑定,您需要的 BlueZ API 记录在:

    【讨论】:

    • 当我在bluetoothctl 上打开scan on 并得到这个数据时得到这个数据:00 00 55 44 33 22 11 00 30 0c 44 01 36 04 2a cb ..^.....0.D.6.*.00 00。我注意到它从第 3 个字节开始打印我的 MAC 地址,但相反。然后我观察它打印的内容,并注意到重复的部分和变化的部分。不知何故,我暴力破解了这些组合,发现44 01 代表温度,36 04 湿度。我只需要找到 0x0144 的十进制值,然后将其除以 16 并得到20.2。总是这样吗?你介意解释一下我在这里做了什么吗?
    • 如果您不介意看一下,我更新了我的帖子,其中包含我试图在评论中解释的信息。我认为您建议的c3 不正确,因为当我提取数据时,它正是20.2 而不是20.3
    猜你喜欢
    • 2018-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-13
    • 2017-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多