我有一个带有一些自定义 GATT 服务的设备,我想编写一个 Linux 程序来与之交互。经过一些搜索,我发现 Linux 正在使用 BlueZ 来处理蓝牙 LE 协议(protocol)。我正在使用带有 BlueZ 5.35 的 Ubuntu 15.10,但我不知道如何从用户空间程序中使用这个 BlueZ。我在任何地方都找不到 API 文档,没有教程、示例,什么都没有。除了使用默认服务连接到蓝牙设备之外,是否有可能使用这个 BlueZ 堆栈来做任何事情?如果是这样,文档在哪里? (最好是 C/C++ API,但此时一切正常)
最佳答案
查看 bluez 源代码 [1] 中的
attrib/gatttool.c
。 Gatttool 是一个命令行实用程序,用于使用 C“API”连接到 BTLE 设备。不过,GATT 接口(interface)并未在 libbluetooth 中公开。gatttool 的更新替代品,因此是另一个学习示例 from 是 btgatt-client,你可以在
tools/btgatt-client.c
(启用编译配置 bluez--enable-experimental
).除了 C 接口(interface)之外,bluez 还集成了一个 DBUS 接口(interface)。 bluetoothctl 是使用 DBUS 接口(interface)的示例工具。的代码 bluetoothctl 可以在
client/
[2] 中找到。另一个使用 bluez C 接口(interface)的示例程序是 Anki 驱动 SDK [3]。它将 bluez GATT C 接口(interface)打包在自己的 库 libbzle [4]。使用 C 接口(interface)时,您必须连接一个 建立 BTLE 连接时的套接字。 gatttool 是这样做的 通过 GATT 接口(interface),后者又使用 glib iirc。但是你可以 也可以使用系统调用(套接字、连接等)来执行此操作,例如 这里 [5]。该文档还解释了:
Unfortunately, as of now there is no official API reference to refer to, so more curious readers are advised to download and examine the BlueZ source code.
Gilbert Brault 还从 bluez [6] 中提取了 GATT 接口(interface),并链接到 GATT 接口(interface) [7] 的基本 doxygen 文档,并附有以下免责声明:
This is a work in progress with the intent of documenting all important functions and data structures
Szymon Janc 在 2016 年嵌入式 Linux 大会上的演讲“现代 Linux 上的蓝牙”中也给出了很好的概述 [8]。从 42:00 开始,他谈到了未公开的 C 接口(interface)。但总的来说,他似乎推荐 DBUS API(请参阅 45:30 处的“提示”幻灯片)。一些 DBUS 文档可以在 doc/gatt-api.txt
[9] 中找到,使用 DBUS 接口(interface)的 Python 示例可以在 test/
中找到。
希望这对您有所帮助。
[1] http://git.kernel.org/cgit/bluetooth/bluez.git/tree/attrib/gatttool.c
[2] http://git.kernel.org/cgit/bluetooth/bluez.git/tree/client/
[3] https://github.com/anki/drive-sdk/
[4] https://github.com/anki/drive-sdk/tree/master/deps/bzle/
[5] https://people.csail.mit.edu/albert/bluez-intro/c404.html
[6] https://github.com/gbrault/gattclient
[7] http://gbrault.github.io/gattclient/index.html
[8] https://www.youtube.com/watch?v=tclS9arLFzk
[9] http://git.kernel.org/cgit/bluetooth/bluez.git/tree/doc/gatt-api.txt
关于linux - Linux API 上的低功耗蓝牙,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35389894/