bluetoothctl 使用指南
一、bluetoothctl 无法 disconnect
在 bluetoothctl 中,当我们想要断开设备连接或者取消配对时,很多时候我们会发现无法成功执行这些操作。这是因为 bluez5 的升级后,配对信息在更改时需要保存到磁盘上,而在某些情况下没有正确保存,从而导致无法断开连接或取消配对。此时可以尝试删除主机上所有蓝牙设备的配对信息,然后再重新配对连接即可。
$ bluetoothctl
[bluetooth]# remove <设备MAC地址>
[bluetooth]# remove <设备MAC地址>
[bluetooth]# exit
二、bluetoothctl 命令
以下是 bluetoothctl 中常用的一些命令:
bluetoothctl # 打开蓝牙交互式命令行界面
power on # 打开蓝牙
power off # 关闭蓝牙
agent on # 启用代理
agent off # 禁用代理
scan on # 搜索设备
scan off # 停止搜索设备
devices # 列出搜索到的设备
pair <设备MAC地址> # 配对设备
trust <设备MAC地址> # 信任设备
untrust <设备MAC地址> # 不信任设备
connect <设备MAC地址> # 连接设备
disconnect <设备MAC地址> # 断开与设备的连接
exit # 退出蓝牙交互式命令行界面
三、bluetoothctl 配对
在 bluetoothctl 中,我们可以进行手动配对操作。配对时,我们需要提供设备的 MAC 地址,并将设备设置为可被发现模式。
$ bluetoothctl
[bluetooth]# power on
[bluetooth]# discoverable on
[bluetooth]# scan on
[bluetooth]# devices
Device 20:13:03:22:66:19 Bose SoundSport Wireless
[bluetooth]# pair 20:13:03:22:66:19
[bluetooth]# trust 20:13:03:22:66:19
[bluetooth]# connect 20:13:03:22:66:19
[bluetooth]# exit
四、bluetoothctl 源码
bluez 是基于 C 语言开发的开源项目,源代码在其官方网站 https://github.com/bluez/bluez 上提供。
五、bluetoothctl 下载
bluez 项目提供了 bluetoothctl 命令行工具,该工具可以通过 apt-get 工具在 Ubuntu 上直接下载安装。
$ sudo apt-get install bluez
六、bluetoothctl 安装
bluetoothctl 已经包含在 bluez 蓝牙套件中,只需要通过 apt-get 安装即可。
$ sudo apt-get install bluez
七、bluetoothctl 最早版本号
bluetoothctl 是 bluez5 版本中新增的交互式命令行工具,因此,bluetoothctl 命令的最早版本号应该是 bluez5 版本。
八、bluetoothctl 启动参数选取
bluetoothctl 可以通过指定启动参数来改变其行为。以下是 bluetoothctl 支持的启动参数及其作用:
-h, --help # 显示帮助信息并退出
-v, --version # 显示版本信息并退出
-t, --timeout=<timeout> # 指定超时时间,默认为60秒