Linux 系统蓝牙命令

转载自 https://ywnz.com/linux/6185.html

在Linux操作系统上安装BlueZ

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

1、针对Ubuntu/Debian:

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

参考:在Ubuntu 16.04下编译安装BlueZ 5.49。

2、针对Fedora:

1
$ sudo dnf -y install bluez bluez-tools

3、针对Arch Linux/Manjaro:

1
$ sudo pacman -S bluez bluez-utils

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

1
2
3
sudo usermod -aG lp $USER

newgrp lp

蓝牙设备应启动并启用:

1
2
3
4
5
$ systemctl is-enabled bluetooth.service 

enabled

$ systemctl status bluetooth.service

安装和使用BlueZ(提供bluetoothctl程序)从Linux终端连接到蓝牙设备

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

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

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

1
$ rfkill

安装和使用BlueZ(提供bluetoothctl程序)从Linux终端连接到蓝牙设备

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

1
$ rfkill unblock 0

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

安装和使用BlueZ(提供bluetoothctl程序)从Linux终端连接到蓝牙设备

使用bluetoothctl连接到蓝牙设备

1、启动bluetoothctl交互式命令:

1
2
3
4
5
$ bluetoothctl

Agent registered

$ bluetoothctl show

安装和使用BlueZ(提供bluetoothctl程序)从Linux终端连接到蓝牙设备

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

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

1
2
3
4
5
6
7
8
9
10
11
[bluetooth]# agent KeyboardOnly

Agent is already registered

[bluetooth]# default-agent

Default agent request successful

[bluetooth]# power on

Changing power on succeeded

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 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、与发现的蓝牙设备配对:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
[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、配对后连接到设备:

1
2
3
4
5
6
7
8
9
[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

安装和使用BlueZ(提供bluetoothctl程序)从Linux终端连接到蓝牙设备

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

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

1
2
3
4
5
6
7
$ sudo vim /etc/bluetooth/main.conf

添加行:AutoEnable=true,如下:

[Policy]

AutoEnable=true

至此,操作完成。

Comments

:D 一言句子获取中...

Loading...Wait a Minute!