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= # 指定超时时间,默认为60秒