参考:https://wiki.archlinux.org/title/Bluetooth_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)

通过bluetoothctl软件连接,真实步骤取决于包括的设备和它们的功能。以下是配对设备的一般步骤:

准备工作:

  1. (确保)安装 bluez,该软件包提供蓝牙的协议栈。
  2. (确保)安装 bluez-utils, 该软件包提供 bluetoothctl 工具。
  3. 通用蓝牙驱动是 btusb 内核模块。检查 模块是否加载了。如果没有就先加载模块
  4. 打开系统蓝牙服务:sudo systemctl restart bluetooth.service

配置蓝牙:运行 bluetoothctl 交互命令。输入 help 来获取帮助。

  1. (可选操作) 选择一个默认的蓝牙接收器select MAC_address.
  2. 使用命令 power on 打开蓝牙,蓝牙默认是关闭的,并且重启之后默认也会关闭
  3. 使用命令 agent on (推荐)选择要连接的设备类型或者去选择一个特定的类型: 如果你在 agent 命令后按下两次tab键, 你应该看到一些可以使用的类型列表,比如 DisplayOnly KeyboardDisplay NoInputNoOutput DisplayYesNo KeyboardOnly off on.
  4. 使用命令 default-agent 去确认并完成要连接的设备类型
  5. 使用命令 devices 去获得想要去配对的设备MAC地址
  6. 如果设备没有出现在上一步的列表中,使用命令 scan on 去搜索发现所有可配对的设备
  7. 使用命令 pair MAC_address 去配对设备(tab键可以自动补全MAC地址)
  8. 如果配对设备不需要配对码(PIN), 那么你可能需要手动将给设备添加到信任列表,使用命令 trust MAC_address
  9. 使用命令 connect MAC_address 去确认最后的连接

以下为一个交互实例:

# bluetoothctl 
[NEW] Controller 00:10:20:30:40:50 pi [default]
[bluetooth]# agent KeyboardOnly 
Agent registered
[bluetooth]# default-agent 
Default agent request successful
[bluetooth]# power on
Changing power on succeeded
[CHG] Controller 00:10:20:30:40:50 Powered: yes
[bluetooth]# scan on
Discovery started
[CHG] Controller 00:10:20:30:40:50 Discovering: yes
[NEW] Device 00:12:34:56:78:90 myLino
[CHG] Device 00:12:34:56:78:90 LegacyPairing: yes
[bluetooth]# pair 00:12:34:56:78:90
Attempting to pair with 00:12:34:56:78:90
[CHG] Device 00:12:34:56:78:90 Connected: yes
[CHG] Device 00:12:34:56:78:90 Connected: no
[CHG] Device 00:12:34:56:78:90 Connected: yes
Request PIN code
[agent] Enter PIN code: 1234
[CHG] Device 00:12:34:56:78:90 Paired: yes
Pairing successful
[CHG] Device 00:12:34:56:78:90 Connected: no
[bluetooth]# connect 00:12:34:56:78:90
Attempting to connect to 00:12:34:56:78:90
[CHG] Device 00:12:34:56:78:90 Connected: yes
Connection successful
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐