【问题标题】:How to read characteristic from Bluetooth?如何从蓝牙读取特征?
【发布时间】:2018-05-29 05:42:41
【问题描述】:

我正在尝试从蓝牙读取特征:

service uuid: 00001800-0000-1000-8000-00805f9b34fb
characteristic uuid: 00002a00-0000-1000-8000-00805f9b34fb

我不知道如何调用chr_get_value 和chr_read 的这些方法。我知道密钥是gdbus。

【问题讨论】:

  • 您能否发布一些您尝试过的代码?我们需要更好地了解您想要做什么。
  • 我编译了bluez5.47,你可以在/bluez5.47/tools/gatt-service.c中看到源代码。我试图调用 chr_read(struct characteristic *chr, DBusMessageIter *iter),但我不知道如何获取 iter。我的英语很差,希望你能理解。
  • 你能用这些细节更新你的帖子吗?

标签: bluetooth-lowenergy bluez bluetooth-gatt


【解决方案1】:

您提到的服务 UUID 是 GAP(通用访问配置文件)服务 UUID,您尝试读取的特征 UUID (00002a00-0000-1000-8000-00805f9b34fb) 是设备名称特征并返回远程 LE 设备的设备名称.

bluez 通过其 DBus API 公开其所有功能。如果您想在 DBus 和 C 中执行此操作,您首先需要了解 DBus 协议和 libdbus 或任何其他 DBus 绑定 API。

有关文档和 python 示例,请参阅 bluez 源目录中的 doc/gatt-api.txt 和 test/example-gatt-client。 test/example-gatt-client 有很好的 python 示例,可以帮助您了解如何扫描、查找和读取设备的特征。

否则,如果您知道要连接的设备的 BD_ADDR 地址(蓝牙设备地址),您可以使用 bluez 的 gatttool 工具简单地执行此任务。

例如,如果地址是 03:0F:45:65:43:FF 并且您的设备 hci 接口地址是 hci0 下面的命令序列读取 2a00 特征

[03:0F:45:65:43:FF][LE]> connect
Attempting to connect to 03:0F:45:65:43:FF
Connection successful

[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 other primary services

[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
# lists all other characteristics as well

[03:0F:45:65:43:FF][LE]> char-read-uuid 2a00
handle: 0x0003   value: 4d 79 20 6e 61 6d 65

【讨论】:

  • 谢谢。我没有尝试连接远程设备。我似乎很难用英语表达我的意思。我想在 Linux 中阅读我的外围设备。我运行命令“gatt-service”注册“00001802-0000-1000-8000-00805f9b34fb”服务并返回OK,但我无法在android应用程序中扫描它。如果您不能理解我的意思,则无需回复。 ^_^
  • 好的。 gatt-service 只是注册服务。但是要从 android 应用程序中找到您的 Linux 外围设备,您需要宣传您的外围设备。请参阅工具目录中的 advtes​​t 工具。您还可以使用 DBus API 注册您的广告,请参阅 test/example-advertisement python 脚本。
  • 我已经运行命令“sudo hciconfig hci0 Leadv”来宣传我的外围设备。当我运行“bluetoothctl”和“show”时,我可以找到“00001802-0000-1000-8000-00805f9b34fb”uuid,但我只能扫描“00001800-0000-1000-8000-00805f9b34fb”和“00001801-0000-” 1000-8000-00805f9b34fb”在安卓应用中。我将看到 DBus API 和广告示例。谢谢。
猜你喜欢
  • 2023-03-21
  • 2014-05-05
  • 1970-01-01
  • 1970-01-01
  • 2015-05-22
  • 1970-01-01
  • 1970-01-01
  • 2021-08-07
  • 1970-01-01
相关资源
最近更新 更多