如何找到BLE设备不同UUID对应的句柄

How to find handles corresponding to different UUIDs of BLE devices

使用 gattool,我可以找到与我的智能手表手柄对应的 UUID,如下所示:

Device: MAC address
Name: MS1020
Alias: MS1020
Paired: yes
Trusted: yes
Blocked: no
Connected: no
LegacyPairing: no
UUID: Generic Access Profile    (00001800-0000-1000-8000-00805f9b34fb)
UUID: Unknown                   (0000cc00-0000-1000-8000-00805f9b34fb)
UUID: Tencent Holdings Limited  (0000fee7-0000-1000-8000-00805f9b34fb)
UUID: Vendor specific           (00010203-0405-0607-0809-0a0b0c0d1912)
ManufacturerData Key: 0x0211
ManufacturerData Value:
[LE]> characteristics
handle: 0x0002, char properties: 0x12, char value handle: 0x0003, uuid: 2b120008-0600-072a-0100-050200042a00
handle: 0x0004, char properties: 0x02, char value handle: 0x0005, uuid: 0708090a-0b0c-0d2b-1200-080600072a01
handle: 0x0007, char properties: 0x06, char value handle: 0x0008, uuid: 00010203-0405-0607-0809-0a0b0c0d2b12
handle: 0x000b, char properties: 0x08, char value handle: 0x000c, uuid: 0000fec7-0000-1000-8000-00805f9b34fb
handle: 0x000d, char properties: 0x20, char value handle: 0x000e, uuid: 0000fec8-0000-1000-8000-00805f9b34fb
handle: 0x0010, char properties: 0x02, char value handle: 0x0011, uuid: 0000fec9-0000-1000-8000-00805f9b34fb
handle: 0x0012, char properties: 0x32, char value handle: 0x0013, uuid: 0000fea1-0000-1000-8000-00805f9b34fb
handle: 0x0015, char properties: 0x2a, char value handle: 0x0016, uuid: 0000fea2-0000-1000-8000-00805f9b34fb
handle: 0x0019, char properties: 0x0a, char value handle: 0x001a, uuid: 0000cc02-0000-1000-8000-00805f9b34fb
handle: 0x001b, char properties: 0x12, char value handle: 0x001c, uuid: 0000cc03-0000-1000-8000-00805f9b34fb
handle: 0x001e, char properties: 0x12, char value handle: 0x001f, uuid: 0000cc04-0000-1000-8000-00805f9b34fb
handle: 0x0021, char properties: 0x1a, char value handle: 0x0022, uuid: 0000cc05-0000-1000-8000-00805f9b34fb
handle: 0x0024, char properties: 0x08, char value handle: 0x0025, uuid: 0000cc06-0000-1000-8000-00805f9b34fb

[LE]> primary
attr handle: 0x0001, end grp handle: 0x0005 uuid: 00001800-0000-1000-8000-00805f9b34fb
attr handle: 0x0006, end grp handle: 0x0009 uuid: 00010203-0405-0607-0809-0a0b0c0d1912
attr handle: 0x000a, end grp handle: 0x0017 uuid: 0000fee7-0000-1000-8000-00805f9b34fb
attr handle: 0x0018, end grp handle: 0x0025 uuid: 0000cc00-0000-1000-8000-00805f9b34fb

[LE]> char-desc
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: 00002800-0000-1000-8000-00805f9b34fb
handle: 0x0007, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0008, uuid: 00010203-0405-0607-0809-0a0b0c0d2b12
handle: 0x0009, uuid: 00002901-0000-1000-8000-00805f9b34fb
handle: 0x000a, uuid: 00002800-0000-1000-8000-00805f9b34fb
handle: 0x000b, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x000c, uuid: 0000fec7-0000-1000-8000-00805f9b34fb
handle: 0x000d, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x000e, uuid: 0000fec8-0000-1000-8000-00805f9b34fb
handle: 0x000f, uuid: 00002902-0000-1000-8000-00805f9b34fb
handle: 0x0010, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0011, uuid: 0000fec9-0000-1000-8000-00805f9b34fb
handle: 0x0012, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0013, uuid: 0000fea1-0000-1000-8000-00805f9b34fb
handle: 0x0014, uuid: 00002902-0000-1000-8000-00805f9b34fb
handle: 0x0015, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0016, uuid: 0000fea2-0000-1000-8000-00805f9b34fb
handle: 0x0017, uuid: 00002902-0000-1000-8000-00805f9b34fb
handle: 0x0018, uuid: 00002800-0000-1000-8000-00805f9b34fb
handle: 0x0019, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x001a, uuid: 0000cc02-0000-1000-8000-00805f9b34fb
handle: 0x001b, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x001c, uuid: 0000cc03-0000-1000-8000-00805f9b34fb
handle: 0x001d, uuid: 00002902-0000-1000-8000-00805f9b34fb
handle: 0x001e, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x001f, uuid: 0000cc04-0000-1000-8000-00805f9b34fb
handle: 0x0020, uuid: 00002902-0000-1000-8000-00805f9b34fb
handle: 0x0021, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0022, uuid: 0000cc05-0000-1000-8000-00805f9b34fb
handle: 0x0023, uuid: 00002902-0000-1000-8000-00805f9b34fb
handle: 0x0024, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0025, uuid: 0000cc06-0000-1000-8000-00805f9b34fb

但是,我很困惑,如何确定哪个句柄适用于哪个句柄 蓝牙功能。例如,位于 https://www.bluetooth.com/specifications/gatt/characteristics/ 的页面显示电池电量规格功能位于 0x2A19。但是,我无法弄清楚在上面的 char-desc 输出中从哪里或如何获取对应于 0x2A19 的 UUID。我怎么得到这个?

编辑:感谢答案中非常有用的描述(都非常好)。我在上面添加了特征信息。我还有一些可能有用也可能没用的附加信息。

通过反复试验,我能够确定血压测量值(当我按下表带上的按钮时获得)是从通知句柄的第三和第四项(十六进制格式)获得的(查看输出):

Notification handle = 0x001f value: f3 14 73 4d 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

然而,当什么都不做,即没有按下任何按钮时,我得到:

[LE]> char-read-hnd 0x001f
Characteristic value/descriptor: 07 00 04 4e 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 

相同的通知句柄在第 11 个条目中给出心率(按下该按钮时):

Notification handle = 0x001f value: 20 00 00 00 00 00 00 00 00 00 63 00 00 00 00 00 00 00 00 00 

然而,在所有情况下,第一个条目也发生了变化(而且它们似乎是操作和不操作所特有的)。

我想知道我是否可以访问这些信息,以及如何触发血压测量等。再次感谢!

如果我的问题不清楚或需要更多信息,请在评论中告诉我。我很乐意根据需要提供更多信息。感谢您花时间阅读和制作 suggesitons/pointers.

primary 的输出告诉您设备支持哪些服务,例如:uuid: 00001800-0000-1000-8000-00805f9b34fb 的第一段是 00001800,即通用访问服务(org.bluetooth.service.generic_access, 0x1800).

primary 的输出未列出电池服务(org.bluetooth.service.battery_service,0x180F):

[LE]> primary
attr handle: 0x0001, end grp handle: 0x0005 uuid: 00001800-0000-1000-8000-00805f9b34fb
attr handle: 0x0006, end grp handle: 0x0009 uuid: 00010203-0405-0607-0809-0a0b0c0d1912
attr handle: 0x000a, end grp handle: 0x0017 uuid: 0000fee7-0000-1000-8000-00805f9b34fb
attr handle: 0x0018, end grp handle: 0x0025 uuid: 0000cc00-0000-1000-8000-00805f9b34fb

由于没有电池服务,您将找不到任何与电池服务相关的特征,例如电池电量、电池电量状态、电池电源状态等。

参考文献:

电池电量特征不是 "at 0x2A19",而是 UUID 00002A19-0000-1000-8000-00805f9b34fb。您的设备上似乎没有此功能。注意 Battery Service 允许被声明为次要服务,那么它可能不会被 "primary" 命令发现。

编辑后更新:

不幸的是,此设备在非标准服务(句柄 0x18,类型 0000cc00-0000-1000-8000-00805f9b34fb)中使用了非标准特性(句柄 0x1e,类型 0000cc04-0000-1000-8000-00805f9b34fb)用于传递数据。如果有一种方法可以触发你想要的测量,那属于逆向工程。

TL;DR

Bluetooth Specification 希望允许特定于供应商的 GATT 功能与标准功能共存,而不需要全局目录。为此,GATT 中的所有内容都是通过 128 位 UUID 的 ID 指定的,任何人都可以在不关心冲突的情况下生成。

  • 一切都由 UUID 键入,参见 3.G.2.5.1:

    The Attribute Type is a UUID that describes the Attribute Value.

  • 每个服务也有一个类型,参见3.G.3.1:

    A service declaration is an Attribute with the Attribute Type set to the UUID for «Primary Service» or «Secondary Service». [...] A client may ignore any service definition with an unknown service UUID. An unknown service UUID is a UUID for an unsupported service.

  • 每个特征也有一个类型,参见 3.G.3.3.1:

    A characteristic declaration is an Attribute with the Attribute Type set to the UUID for «Characteristic» and Attribute Value set to the Characteristic Properties, Characteristic Value Attribute Handle and Characteristic UUID.

UUID 分配很简单,我们用上面的设备转储提供了完美的说明:

  • standard-defined uses,坚持标准UUID,

  • Bluetooth SIG 将其成员提供给 get a Bluetooth-based UUID 以优化使用,因此 0000fee7-0000-1000-8000-00805f9b34fb 是合规的且特定于供应商(由 SIG 在 24/分配给 Tencent Holdings Limited 04/2014).

  • 从头开始生成随机 UUID 并将其用于自定义目的。 00010203-0405-0607-0809-0a0b0c0d1912 看起来合规,但一点也不随意。其他人可能偶然得到相同的值。

OTOH,我找不到任何定义 0000cc00-0000-1000-8000-00805f9b34fb 的蓝牙规范,它看起来不合规。

蓝牙规范自行处理并通过两种方式针对其自身的规范使用优化了 UUID 过程:

  • 为了便于查找,所有蓝牙定义的 ID 都有一个共同的基础,即 nnnnnnnn-0000-1000-8000-00805f9b34fb,其中 nnnnnnnn 是 32 位值,不会发生冲突acceptable,分配时必须严格控制。参见 3.B.2.5.1:

    To reduce the burden of storing and transferring 128-bit UUID values, a range of UUID values has been pre-allocated for assignment to often-used, registered purposes.

    当您查看 GATT Service/GATT Characteristics 页面时,您实际上是在查看基于蓝牙的 UUID 列表,即当您在 gatttool 的输出中查找它们时,您应该附加 -0000-1000-8000-00805f9b34fb

  • 作为优化,GATT 协议允许将基于 Bluettoth 的 UUID 编码为更短的变体,即“16 位”和“32 位”变体,即不重复 -0000-1000-8000-00805f9b34fb .有关此类事情的示例,请参阅 3.G.3.3.1.

  • 中的 table 3.4