BLE:"handle" 是从哪里来的?
BLE: Where does the "handle" come from?
我正在尝试学习如何在 Pi4 上使用 BLE。
我已经安装了 hcitool 和 gatttool,并且知道如何使用它们。我还在 Mac.
上使用 LightBlue 设置了一个虚拟设备
pi@raspsky:~ $ sudo hcitool -i hci0 lescan
LE Scan ...
98:9E:63:39:8B:ED Blank
98:9E:63:39:8B:ED (unknown)
那个“空白”是虚拟设备。
pi@raspsky:~ $ gatttool -b 98:9E:63:39:8B:ED -I
[98:9E:63:39:8B:ED][LE]> connect
Attempting to connect to 98:9E:63:39:8B:ED
Connection successful
[98:9E:63:39:8B:ED][LE]> primary
attr handle: 0x0001, end grp handle: 0x0005 uuid: 00001800-0000-1000-8000-00805f9b34fb
<snip>
attr handle: 0x0028, end grp handle: 0x002b uuid: 00001111-0000-1000-8000-00805f9b34fb
[98:9E:63:39:8B:ED][LE]> char-desc
handle: 0x0001, uuid: 00002800-0000-1000-8000-00805f9b34fb
<snip>
handle: 0x002b, uuid: 00002901-0000-1000-8000-00805f9b34fb
现在,我希望能够从虚拟设备读取数据,但我不知道该使用哪个“句柄”。我通过反复试验发现我需要使用 002b:
[98:9E:63:39:8B:ED][LE]> char-read-hnd 002b
Characteristic value/descriptor: 53 6f 6d 65 74 68 69 6e 67
那个十六进制结果是我在虚拟设备中输入的 ascii 字符串“Something”。
但是,那个句柄是从哪里来的呢?看来我应该不用反复试验就能弄明白:
更新:
在这个例子Using bash and gatttool to get readings from Xiaomi Mijia LYWSD03MMC Temperature Humidity sensor中,作者展示了如何使用hcitool获取BLE设备的mac地址。文章接着举例:
bt=$(timeout 15 gatttool -b A4:C1:38:8C:77:CA --char-write-req --handle='0x0038' --value="0100" --listen)
但是作者从来没有解释句柄(handle='0x0038')是从哪里来的。我知道如何使用 gatttool 获取句柄列表,但我可以弄清楚每个句柄的数据的唯一方法是尝试每个句柄,直到找到我想要的那个。一定有更简单的方法。
hcitool 和 gatttool 在 2017 年是 deprecated by the BlueZ project。如果您正在学习使用它们的教程,它可能已经过时了。当前用于通用扫描和探索的 BlueZ 工具是 bluetoothctl
。
对于 BLE,UUID 是识别您感兴趣的 service/characteristic/descriptor 的关键。
16-bit UUID Numbers Document 列出了采用的 UUID。 SIG 采用的属性类型 (UUID) 共享特殊 128 位基本 UUID 中除 16 位以外的所有位:0000xxxx-0000-1000-8000-00805F9B34FB
。该文档列出了进入该基数的 16 位值。
与使用 LightBlue 的虚拟设备一样,可以创建 custom UUIDs。这些需要在为 SIG 采用的值保留的 128 位基数之外。
由于 SO 是关于软件开发的,所以我会在 BlueZ 源代码树中为您指出 BlueZ API documentation should you want to do any of this with code. There are also examples。
你要找的东西一般来说就是GATT/ATT本身。
基本上你查询一个实现 GATT 服务器的设备
与 Bluetooth® 相关的任何内容的最佳(资源)来源和参考是恕我直言,Bluetooth Core Specification 您的设备支持的版本和您正在开发的版本。
属性协议 (ATT)
蓝牙核心规范 5.2 卷。 3 F 部分描述了 属性协议 (ATT) 如下:
This Part defines the Attribute protocol; a protocol for discovering, reading, and writing attributes on a peer device
ATT 是某种“低级别”,因为它描述了实际的协议本身及其消息/协议数据单元 (PDU),但绝对值得(外围)阅读,特别章节 3.1 简介 和 3.2 基本概念.
通用属性配置文件 (GATT)
蓝牙核心规范 5.2 卷。 3 G 部分描述了 通用属性配置文件 (GATT) 如下:
This Part defines the Generic Attribute Profile that describes a service framework using the Attribute protocol for discovering services, and for reading and writing characteristic values on a peer device.
您可以将 GATT 视为高级 API,用于发出 ATT 查询和命令。
您绝对应该阅读完整的章节 2.6 GATT Profile Hierarchy,然后转到 4 GATT Feature Requirements,尤其是章节4.4 主要服务发现、4.6 特征发现 和 *4.7 特征描述符发现 - 关于您的问题
But, where does that handle come from?
由于您更新了问题,我为您提供了另一个答案。但由于您的主题没有面临任何编程问题,您最好在另一个 StackExchange 上再次提问。
the only way I can figure out what data goes with each handle is to try each one until I find the one I want. There must be an easier way.
我想我不得不在这里让你失望了。
虽然有一些 predefined GATT profiles by Bluetooth SIG,但大多数专有实现不会公开有关其特征的任何有意义的信息,但会公开强制性 UUID。
如果幸运的话,您可能能够找到特征用户描述(蓝牙核心规范第 3 卷第 G 部分 3.3.3.2)或从 Characteristic Presentation Format(Bluetooth Core Spec Vol. 3 Part G 3.3.3.5)特征描述符;但由于这些是可选的,我怀疑专有实现是否会提供这些。
因此,对于专有实施,您必须对该信息进行逆向工程或尝试在线查找(通常其他人已经对普通设备进行了逆向工程)。
我正在尝试学习如何在 Pi4 上使用 BLE。
我已经安装了 hcitool 和 gatttool,并且知道如何使用它们。我还在 Mac.
上使用 LightBlue 设置了一个虚拟设备pi@raspsky:~ $ sudo hcitool -i hci0 lescan
LE Scan ...
98:9E:63:39:8B:ED Blank
98:9E:63:39:8B:ED (unknown)
那个“空白”是虚拟设备。
pi@raspsky:~ $ gatttool -b 98:9E:63:39:8B:ED -I
[98:9E:63:39:8B:ED][LE]> connect
Attempting to connect to 98:9E:63:39:8B:ED
Connection successful
[98:9E:63:39:8B:ED][LE]> primary
attr handle: 0x0001, end grp handle: 0x0005 uuid: 00001800-0000-1000-8000-00805f9b34fb
<snip>
attr handle: 0x0028, end grp handle: 0x002b uuid: 00001111-0000-1000-8000-00805f9b34fb
[98:9E:63:39:8B:ED][LE]> char-desc
handle: 0x0001, uuid: 00002800-0000-1000-8000-00805f9b34fb
<snip>
handle: 0x002b, uuid: 00002901-0000-1000-8000-00805f9b34fb
现在,我希望能够从虚拟设备读取数据,但我不知道该使用哪个“句柄”。我通过反复试验发现我需要使用 002b:
[98:9E:63:39:8B:ED][LE]> char-read-hnd 002b
Characteristic value/descriptor: 53 6f 6d 65 74 68 69 6e 67
那个十六进制结果是我在虚拟设备中输入的 ascii 字符串“Something”。
但是,那个句柄是从哪里来的呢?看来我应该不用反复试验就能弄明白:
更新:
在这个例子Using bash and gatttool to get readings from Xiaomi Mijia LYWSD03MMC Temperature Humidity sensor中,作者展示了如何使用hcitool获取BLE设备的mac地址。文章接着举例:
bt=$(timeout 15 gatttool -b A4:C1:38:8C:77:CA --char-write-req --handle='0x0038' --value="0100" --listen)
但是作者从来没有解释句柄(handle='0x0038')是从哪里来的。我知道如何使用 gatttool 获取句柄列表,但我可以弄清楚每个句柄的数据的唯一方法是尝试每个句柄,直到找到我想要的那个。一定有更简单的方法。
hcitool 和 gatttool 在 2017 年是 deprecated by the BlueZ project。如果您正在学习使用它们的教程,它可能已经过时了。当前用于通用扫描和探索的 BlueZ 工具是 bluetoothctl
。
对于 BLE,UUID 是识别您感兴趣的 service/characteristic/descriptor 的关键。
16-bit UUID Numbers Document 列出了采用的 UUID。 SIG 采用的属性类型 (UUID) 共享特殊 128 位基本 UUID 中除 16 位以外的所有位:0000xxxx-0000-1000-8000-00805F9B34FB
。该文档列出了进入该基数的 16 位值。
与使用 LightBlue 的虚拟设备一样,可以创建 custom UUIDs。这些需要在为 SIG 采用的值保留的 128 位基数之外。
由于 SO 是关于软件开发的,所以我会在 BlueZ 源代码树中为您指出 BlueZ API documentation should you want to do any of this with code. There are also examples。
你要找的东西一般来说就是GATT/ATT本身。
基本上你查询一个实现 GATT 服务器的设备
与 Bluetooth® 相关的任何内容的最佳(资源)来源和参考是恕我直言,Bluetooth Core Specification 您的设备支持的版本和您正在开发的版本。
属性协议 (ATT)
蓝牙核心规范 5.2 卷。 3 F 部分描述了 属性协议 (ATT) 如下:
This Part defines the Attribute protocol; a protocol for discovering, reading, and writing attributes on a peer device
ATT 是某种“低级别”,因为它描述了实际的协议本身及其消息/协议数据单元 (PDU),但绝对值得(外围)阅读,特别章节 3.1 简介 和 3.2 基本概念.
通用属性配置文件 (GATT)
蓝牙核心规范 5.2 卷。 3 G 部分描述了 通用属性配置文件 (GATT) 如下:
This Part defines the Generic Attribute Profile that describes a service framework using the Attribute protocol for discovering services, and for reading and writing characteristic values on a peer device.
您可以将 GATT 视为高级 API,用于发出 ATT 查询和命令。
您绝对应该阅读完整的章节 2.6 GATT Profile Hierarchy,然后转到 4 GATT Feature Requirements,尤其是章节4.4 主要服务发现、4.6 特征发现 和 *4.7 特征描述符发现 - 关于您的问题
But, where does that handle come from?
由于您更新了问题,我为您提供了另一个答案。但由于您的主题没有面临任何编程问题,您最好在另一个 StackExchange 上再次提问。
the only way I can figure out what data goes with each handle is to try each one until I find the one I want. There must be an easier way.
我想我不得不在这里让你失望了。
虽然有一些 predefined GATT profiles by Bluetooth SIG,但大多数专有实现不会公开有关其特征的任何有意义的信息,但会公开强制性 UUID。
如果幸运的话,您可能能够找到特征用户描述(蓝牙核心规范第 3 卷第 G 部分 3.3.3.2)或从 Characteristic Presentation Format(Bluetooth Core Spec Vol. 3 Part G 3.3.3.5)特征描述符;但由于这些是可选的,我怀疑专有实现是否会提供这些。
因此,对于专有实施,您必须对该信息进行逆向工程或尝试在线查找(通常其他人已经对普通设备进行了逆向工程)。