如何从Linux终端连接到蓝牙设备

对于那些将大部分时间都花在终端上的人来说,从终端连接到蓝牙设备(如扬声器,发射器和其他电子设备)非常有意义。本文介绍如何从Linux终端(UbuntuDebian,Fedora,基于Arch的发行版)控制蓝牙设备。

蓝牙无线技术是一种小型,低成本无线解决方案的全球规范,可在移动计算机,手机,其他便携式手持设备之间建立链接,并提供与Internet的连接。规格是 蓝牙特别兴趣小组(SIG)

在本指南中, 蓝色Z 提供对核心蓝牙层和协议的支持。使用灵活,高效和模块化的实现。它具有以下主要功能:

  • 完整的模块实施
  • 对称多处理安全
  • 多线程数据处理
  • 支持多个蓝牙设备
  • 真正的硬件抽象
  • 所有层的标准插座接口
  • 设备和服务级别的安全支持

在Linux上安装BlueZ

使用与您的Linux发行版匹配的以下命令之一安装BlueZ软件包。

----- Ubuntu / Debian -----
$ sudo apt-get -y install bluetooth bluez bluez-tools rfkill

----- Fedora -----
$ sudo dnf -y install bluez bluez-tools

----- Arch Linux / Manjaro -----
$ sudo pacman -S bluez bluez-utils 

在此安装中, bluetoothctl 实用程序。如果要连接到蓝牙系绳,则需要将帐户添加到lp组。

sudo usermod -aG lp $USER
newgrp lp

您需要激活并启用蓝牙设备。

$ systemctl is-enabled bluetooth.service 
enabled

$ systemctl status bluetooth.service
● bluetooth.service - Bluetooth service
   Loaded: loaded (/usr/lib/systemd/system/bluetooth.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2019-11-30 17:14:12 EAT; 3h 55min ago
     Docs: man:bluetoothd(8)
 Main PID: 1638 (bluetoothd)
    Tasks: 1 (limit: 18590)
   Memory: 2.3M
      CPU: 47ms
   CGroup: /system.slice/bluetooth.service
           └─1638 /usr/libexec/bluetooth/bluetoothd

Nov 30 17:14:12 myfed.example.net systemd[1]: Starting Bluetooth service...
Nov 30 17:14:12 myfed.example.net bluetoothd[1638]: Bluetooth daemon 5.52
Nov 30 17:14:12 myfed.example.net systemd[1]: Started Bluetooth service.
Nov 30 17:14:12 myfed.example.net bluetoothd[1638]: Starting SDP server
Nov 30 17:14:12 myfed.example.net bluetoothd[1638]: Bluetooth management interface 1.14 initialized
Nov 30 21:07:52 myfed.example.net bluetoothd[1638]: Endpoint registered: sender=:1.77 path=/MediaEndpoint/A2DPSink/sbc
Nov 30 21:07:52 myfed.example.net bluetoothd[1638]: Endpoint registered: sender=:1.77 path=/MediaEndpoint/A2DPSource/sbc

如何配对和连接蓝牙设备

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

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

$ rfkill 
ID TYPE      DEVICE               SOFT          HARD
0 bluetooth tpacpi_bluetooth_sw   blocked       unblocked
1 wlan      phy0                  unblocked     unblocked

如果是这样,请使用以下命令将其解除阻止。

$ rfkill unblock bluetooth

确保您没有被阻止。

$ rfkill          
ID TYPE      DEVICE                   SOFT      HARD
 0 bluetooth tpacpi_bluetooth_sw unblocked unblocked
 1 wlan      phy0                unblocked unblocked
 2 bluetooth hci0                unblocked unblocked

使用bluetoothctl连接到蓝牙设备

这是使用蓝牙配对设备的一般概述。

  • 启动bluetoothctl交互式命令。
$ bluetoothctl
Agent registered

$ bluetoothctl show 
Controller 20:79:18:5E:4B:64 (public)
 Name: myfed.example.net
 Alias: myfed.example.net
 Class: 0x000c010c
 Powered: yes
 Discoverable: no
 DiscoverableTimeout: 0x00000000
 Pairable: no
 UUID: Headset AG                (00001112-0000-1000-8000-00805f9b34fb)
 UUID: A/V Remote Control        (0000110e-0000-1000-8000-00805f9b34fb)
 UUID: PnP Information           (00001200-0000-1000-8000-00805f9b34fb)
 UUID: Audio Sink                (0000110b-0000-1000-8000-00805f9b34fb)
 UUID: Headset                   (00001108-0000-1000-8000-00805f9b34fb)
 UUID: A/V Remote Control Target (0000110c-0000-1000-8000-00805f9b34fb)
 UUID: Generic Access Profile    (00001800-0000-1000-8000-00805f9b34fb)
 UUID: Audio Source              (0000110a-0000-1000-8000-00805f9b34fb)
 UUID: Generic Attribute Profile (00001801-0000-1000-8000-00805f9b34fb)
 Modalias: usb:v1D6Bp0246d0534
 Discovering: no
Advertising Features:
 ActiveInstances: 0x00
 SupportedInstances: 0x05
 SupportedIncludes: tx-power
 SupportedIncludes: appearance
 SupportedIncludes: local-name

输入值 help 获取可用命令列表。

  • 打开蓝牙适配器
[bluetooth]# agent KeyboardOnly
Agent is already registered

[bluetooth]# default-agent
Default agent request successful

[bluetooth]# power on
Changing power on succeeded
  • 执行扫描以检测蓝牙设备
# 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
  • 与发现的蓝牙设备配对
[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
  • 配对后连接到设备
[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
Device 5C:FB:7C:A4:13:C6 (public)
 Name: JBL Clip 3
 Alias: JBL Clip 3
 Class: 0x00200414
 Icon: audio-card
 Paired: yes
 Trusted: yes
 Blocked: no
 Connected: yes
 LegacyPairing: no
 UUID: Headset                   (00001108-0000-1000-8000-00805f9b34fb)
 UUID: Audio Sink                (0000110b-0000-1000-8000-00805f9b34fb)
 UUID: A/V Remote Control Target (0000110c-0000-1000-8000-00805f9b34fb)
 UUID: A/V Remote Control        (0000110e-0000-1000-8000-00805f9b34fb)
 UUID: Handsfree                 (0000111e-0000-1000-8000-00805f9b34fb)
 ManufacturerData Key: 0x0057
 ManufacturerData Value:
  01 00 cb 0e d0 1e 03 01                          ........        
 RSSI: -21
 TxPower: 0

开机时打开蓝牙适配器

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

$ sudo vim /etc/bluetooth/main.conf

添加行 AutoEnable=true

[Policy]
AutoEnable=true

另请参阅:

其他提示:

在Windows 10 / Windows Server上安装Windows Terminal

如何从Linux终端搜索Google

适用于Linux的最佳控制台/终端文件管理器

适用于Linux的10个最佳终端模拟器

如何在Web浏览器中共享Linux终端会话

Sidebar