首页 文章

如何在Linux中使用Bluez实现蓝牙LE

提问于
浏览
13

我正在为BLE演示设置两个Linux系统 . 显然,一个系统将是外围设备,一个系统将是中央设备 . 围绕这两种配置我有几个问题 .

Environment

Peripheral Device Setup

第一项业务是通过配置的GATT服务器获得外围系统设置和广告 . 此时,似乎无法从命令行配置GATT服务器 . 因此,虽然将USB加密狗带入并通告它是一项简单的任务,但这不允许创建自定义服务和特性 . 我能找到的GATT服务器的唯一例子是Bluez包中的gatt-example.c文件 . 所以我下载并构建了最新的bluez-5.23源代码 . (http://www.linuxfromscratch.org/blfs/view/svn/general/bluez.html) . 另外使用--enable-maintainer-mode标志配置强制将gatt-example.c插件构建到bluetoothd中 . 我在 ~/bluez-5.23/plugins 目录中验证了post-build有一个 bluetoothd-gat-example.o 文件 . 这告诉我gatt-example至少是成功构建的 .

然后我修改了配置文件以启用LE和属性服务器 .

$ sudo vi /etc/bluetooth/main.conf
EnableLE = true           // Enable Low Energy support. Default is false.
AttributeServer = true    // Enable the GATT attribute server. Default is false.

然后只需重启或重启蓝牙守护进程......

Central Device Setup

由于中央设备不需要像外围设备那样构建任何特殊插件,我只是使用 apt-get 安装了bluez . 这似乎已根据 bluetoothd -v 安装了v4.101 .

Session Setup

然后连接过程应该相当简单 . 我设置外围设备进行广告宣传,然后与中央设备连接:

外围:

$ sudo hciconfig hci0 up        // Make sure the interface is up
$ sudo hciconfig hci0 leadv     // Set the interface to advertise

中央:

$ sudo hcitool -i hci0 lescan   // Scan for nearby devices advertising
LE Scan ...
00:02:72:C9:5E:0F (unknown)     // Not sure why two of the same MAC are found?
00:02:72:C9:5E:0F (unknown)     // but I know this is my device...

$ sudo gatttool -i hci0 -b 00:02:72:C9:5E:0F -m 48 --interactive     // Connect interactively
[   ][00:02:72:C9:5E:0F][LE]> connect
[CON][00:02:72:C9:5E:0F][LE]> primary
attr handle: 0x0001, end grp handle: 0x0008 uuid: 00001800-0000-1000-8000-00805f9b34fb
attr handle: 0x0010, end grp handle: 0x0010 uuid: 00001801-0000-1000-8000-00805f9b34fb
[CON][00:02:72:C9:5E:0F][LE]> characteristics 
handle: 0x0004, char properties: 0x02, char value handle: 0x0006, uuid: 00002a00-0000-1000-8000-00805f9b34fb
handle: 0x0007, char properties: 0x02, char value handle: 0x0008, uuid: 00002a01-0000-1000-8000-00805f9b34fb

我们看不到gatt-example的服务或特性之一可用 .

Questions

  • 外围设备

  • 如何创建自己的自定义GATT服务器?它可以是一个独立的C应用程序,还是需要像gatt一样的插件内置到bluetoothd?这个问题的答案(Creating a Gatt Server?)意味着您执行以下操作:"start by initializing the GATT library and additional modules"然后"register your GATT database" . 但是没有一个如何实现这些通用语句的例子,所提供的链接只是蓝牙网站的URL .

  • GATT规范(https://developer.bluetooth.org/gatt/Pages/default.aspx)提供了许多可以XML格式下载的"adopted"服务和特征 . 但是没有关于如何使用它们的说明?!

  • 如何验证我的GATT服务器是否正在运行?

  • 中央设备

  • 为什么我的中央设备没有看到外围设备上运行的GATT服务器的服务和特性?

我可以提供任何必要的额外信息 . 谢谢 .

1 回答

  • 2

    要在单独的流程中创建GATT服务器,您至少有两种情况:

    • Bluez v4.x:您的GATT服务必须是Bluez插件

    • Bluez v5.x:您的GATT服务应使用新的GATT DBus API(但建议至少使用Bluez v5.39(自2016年4月起) . 否则使用它(使用Bluez GATT Server API)更安全Bluez v4.x插件方法 .

    如果您的中央设备没有看到新导出的GATT服务可能是外围问题,而不是中央设备上的问题 . 当您需要在中央设备上实施GATT客户端时,您仍然有两种情况:

    • Bluez v4.x:Bluez不公开GATT API . 您可以使用shell脚本启动 gatttool 命令,也可以使用GATT库(如gattlib)与BLE设备进行交互

    • Bluez v5.x:同样的,如果你不能迁移到Bluez v5.39,那么最好使用Bluez v4.x方法 .

相关问题