本文介绍从Linux终端(基于Ubuntu、Debian、Fedora、Manjaro和Arch的发行版)控制蓝牙设备,将安装和使用BlueZ(提供bluetoothctl实用程序),它为核心的蓝牙层和协议提供支持,它灵活、高效并且使用模块化实现。BlueZ具有的主要功能:完整的模块化实施、对称多处理安全、多线程数据处理、支持多个蓝牙设备、真正的硬件抽象、所有层的标准插座接口、设备和服务级别的安全支持。

在Linux操作系统上安装BlueZ

使用与你的Linux发行版匹配的命令来安装BlueZ软件包:

1、针对Ubuntu/Debian:

$ sudo apt-get -y install bluetooth bluez bluez-tools rfkill

2、针对Fedora:

$ sudo dnf -y install bluez bluez-tools

3、针对Arch Linux/Manjaro:

$ sudo pacman -S bluez bluez-utils

此安装提供了bluetoothctl实用程序,如果你想连接到bluetooth tether,则需要将你的帐户添加到lp组:

sudo usermod -aG lp $USER

newgrp lp

蓝牙设备应启动并启用:

$ systemctl is-enabled bluetooth.service

enabled

$ systemctl status bluetooth.service

如何配对并连接到蓝牙设备

我们将使用bluetoothctl CLI tp配置bluez,如果你有替代的前端工具,例如GNOME蓝牙,则可以改用它。

首先,请确保未禁用蓝牙:

$ rfkill

如果被阻止,请使用以下命令取消阻止:

$ rfkill unblock 0

确认它已被阻止,运行rfkill命令查看,如下图:

使用bluetoothctl连接到蓝牙设备

1、启动bluetoothctl交互式命令:

$ bluetoothctl

Agent registered

$ bluetoothctl show

注:可以输入help命令以获取可用命令的列表。

2、打开你的蓝牙适配器:

[bluetooth]# agent KeyboardOnly

Agent is already registered

[bluetooth]# default-agent

Default agent request successful

[bluetooth]# power on

Changing power on succeeded

3、进行扫描以检测你的蓝牙设备:

# scan on

Discovery started

[CHG] Controller 20:79:18:5E:4B:64 Discovering: yes

[NEW] Device 14:AB:C5:CC:C7:AB 14-AB-C5-CC-C7-AB

[CHG] Device 14:AB:C5:CC:C7:AB TxPower: 8

[CHG] Device 14:AB:C5:CC:C7:AB Name: DESKTOP-N9GVMQ2

[CHG] Device 14:AB:C5:CC:C7:AB Alias: DESKTOP-N9GVMQ2

[CHG] Device 14:AB:C5:CC:C7:AB UUIDs: 0000110c-0000-1000-8000-00805f9b34fb

[CHG] Device 14:AB:C5:CC:C7:AB UUIDs: 0000110a-0000-1000-8000-00805f9b34fb

[CHG] Device 14:AB:C5:CC:C7:AB UUIDs: 0000110e-0000-1000-8000-00805f9b34fb

[CHG] Device 14:AB:C5:CC:C7:AB UUIDs: 0000111f-0000-1000-8000-00805f9b34fb

[CHG] Device 14:AB:C5:CC:C7:AB UUIDs: 0000111e-0000-1000-8000-00805f9b34fb

[NEW] Device 5C:FB:7C:A4:13:C6 JBL Clip 3

4、与发现的蓝牙设备配对:

[bluetooth]# pair 5C:FB:7C:A4:13:C6

Attempting to pair with 5C:FB:7C:A4:13:C6

[CHG] Device 5C:FB:7C:A4:13:C6 Connected: yes

[CHG] Device 5C:FB:7C:A4:13:C6 UUIDs: 00001108-0000-1000-8000-00805f9b34fb

[CHG] Device 5C:FB:7C:A4:13:C6 UUIDs: 0000110b-0000-1000-8000-00805f9b34fb

[CHG] Device 5C:FB:7C:A4:13:C6 UUIDs: 0000110c-0000-1000-8000-00805f9b34fb

[CHG] Device 5C:FB:7C:A4:13:C6 UUIDs: 0000110e-0000-1000-8000-00805f9b34fb

[CHG] Device 5C:FB:7C:A4:13:C6 UUIDs: 0000111e-0000-1000-8000-00805f9b34fb

[CHG] Device 5C:FB:7C:A4:13:C6 ServicesResolved: yes

[CHG] Device 5C:FB:7C:A4:13:C6 Paired: yes

Pairing successful

# trust 5C:FB:7C:A4:13:C6

[CHG] Device 5C:FB:7C:A4:13:C6 Trusted: yes

Changing 5C:FB:7C:A4:13:C6 trust succeeded

# paired-devices

Device 5C:FB:7C:A4:13:C6 JBL Clip 3

# devices

Device 5C:FB:7C:A4:13:C6 JBL Clip 3

5、配对后连接到设备:

[bluetooth]# connect 5C:FB:7C:A4:13:C6

Attempting to connect to 5C:FB:7C:A4:13:C6

[CHG] Device 5C:FB:7C:A4:13:C6 Connected: yes

Connection successful

[JBL Clip 3]# info

打开电源时打开蓝牙适配器

要在Linux系统启动时打开蓝牙适配器的电源,请打开配置文件:

$ sudo vim /etc/bluetooth/main.conf

添加行:AutoEnable=true,如下:

[Policy]

AutoEnable=true

至此,操作完成。

相关主题

Logo

更多推荐