本文说明如何编译和修改和使用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

编译 链接到标题

tar -xvf bluez-5.50.tar.xz
cd bluez-5.50/
./configure
make

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

问题处理 链接到标题

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

安装dbus可解决

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

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

btgatt-client 链接到标题

使用 链接到标题

连接

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

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

[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