Bluez for BLE

Creative Commons
本作品采用知识共享署名

本文说明如何编译和修改和使用bluez的btgatt-client

BLE GATT Tool一文中介绍了gatttool的用法,但发现gatttool只能列出services/characteristics/descriptor,并不能显示出三者之间的所属关系。在网上寻找开源替换工具的时候发现pygatt后端使用的gatt,在其issue列表中说明了gatttool已经被新版本的bluez废弃了gatttool,参考https://github.com/peplin/pygatt/issues/112 & https://wiki.archlinux.org/index.php/bluetooth#Troubleshooting。gatttool可使用新工具btgatt-client替代,编译最新的bluez release版本可以产出btgatt-client, btgatt-client可以显示出services/characteristics/descriptor三者之间的所属关系。

bluez

下载

http://www.bluez.org/release-of-bluez-5-50/ 最新的release bluez-5.50.tar.xz

编译

1
2
3
4
tar -xvf bluez-5.50.tar.xz
cd bluez-5.50/
./configure
make

编译完成后会在bluez-5.50/tools/下找到btgatt-client

问题处理

  • 编译时提示
    1
    configure: error: D-Bus >= 1.6 is required

安装dbus可解决

1
apt install libdbus-1-dev

  • 任然需要gatttool
    重新configure然后编译
    1
    需要执行./configure --enable-deprecated

需要说明的是hcitool也是bluez废弃之一,可以用该方法编译出

btgatt-client

使用

连接

1
./btgatt-client -t random -d E9:45:EC:29:65:DB

连接后会自动发现service并列出来
bc
之后会进入client,执行help可以看到支援的命令,和gatttool相似

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[GATT client]# help
Commands:
help Display help message
services Show discovered services
read-value Read a characteristic or descriptor value
read-long-value Read a long characteristic or desctriptor value
read-multiple Read Multiple
write-value Write a characteristic or descriptor value
write-long-value Write long characteristic or descriptor value
write-prepare Write prepare characteristic or descriptor value
write-execute Execute already prepared write
register-notify Subscribe to not/ind from a characteristic
unregister-notify Unregister a not/ind session
set-security Set security level on le connection
get-security Get security level on le connection
set-sign-key Set signing key for signed write command

修改

btgatt-client虽然比gatttool多显示了所属关系,但是只有UUID,对于调试Gatt spec定义的UUID还不直观,这里对btgatt-client.c进行修改,将Gatt spec UUID对应的name显示出来,对于调试更为直观
bcm

修改方法见https://gitee.com/lgl88911/BLETool