使用bluetoothctl/gatttool读写BLE厂商自定义特征值的问题求助
树莓派4B BLE GATT工具使用问题及解决方案
问题梳理
- gatttool 存在的问题:
- 连接成功时自动弹出带OK按钮的对话框,不符合生产环境无交互需求
- 自身无解绑/移除设备的功能
- bluetoothctl 存在的问题:
- 可正常解绑设备,但无法读取厂商自定义特征值——选中自定义特征路径执行
read命令时提示「未选中属性」,但读取通用特征值正常
- 可正常解绑设备,但无法读取厂商自定义特征值——选中自定义特征路径执行
已知gatttool可正常读取自定义特征值,命令示例:
gatttool -t random -b E0:0D:10:F1:D5:FC --char-read -a 0x0014
解决方法
解决gatttool的弹窗问题
gatttool的弹窗来自系统蓝牙服务的自动交互触发,可通过修改配置禁用:
- 编辑蓝牙主配置文件:
sudo nano /etc/bluetooth/main.conf - 找到
AutoEnable项,修改为AutoEnable=false - 重启蓝牙服务生效:
sudo systemctl restart bluetooth
解决gatttool的解绑问题
gatttool本身不支持解绑,需结合bluetoothctl完成:
- 完成gatttool操作后,启动bluetoothctl:
bluetoothctl - 列出已配对设备:
devices - 执行解绑命令:
remove E0:0D:10:F1:D5:FC
解决bluetoothctl无法读取自定义特征值的问题
需确保特征路径或UUID选择正确,步骤如下:
- 先用gatttool获取自定义特征的完整信息:
gatttool --characteristics -b E0:0D:10:F1:D5:FC - 启动bluetoothctl,选中目标设备:
select-device E0:0D:10:F1:D5:FC - 列出所有属性:
list-attributes,找到对应自定义特征的完整路径(例如/org/bluez/hci0/dev_E0_0D_10_F1_D5_FC/service0012/char0014) - 选中该特征:
select-attribute /org/bluez/hci0/dev_E0_0D_10_F1_D5_FC/service0012/char0014 - 执行读取:
read
注:部分自定义特征需要先启用通知/指示权限,可先执行notify on再读取
本地参考文档
- 查看命令官方说明:执行
man bluetoothd、man bluetoothctl、man gatttool获取各工具的参数及用法细节 - 查看bluetoothctl子命令帮助:在bluetoothctl交互界面中执行
help [命令名](例如help read、help select-attribute)获取具体命令的使用说明
内容的提问来源于stack exchange,提问作者Hamilton Woods
相关产品推荐
相关产品推荐

