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

时间:2020-02-23 14:32:37  来源:igfitidea点击:

对于在终端中最多的人来说,它可以从终端中连接到蓝牙设备(如讲台)等蓝牙设备,从终端中连接。
在本文中,我们将讨论如何从Linux终端 - Ubuntu,Debian,Fedora和Arch基于Arch的发行版控制蓝牙设备。

蓝牙无线技术是全球规范,用于小型系列,低成本的无线电解决方案,提供移动计算机,移动电话,其他便携式手持设备和与互联网连接之间的链接。
该规范由蓝牙特殊兴趣组(SIG)开发,发布和促进。

在本教程中,我们将使用Bluez,为核心蓝牙图层和协议提供支持。
它灵活,高效,并使用模块化实现。
它具有以下关键函数。

完整的模块化实现对称多处理SafeMultithed数据处理函数,用于多个蓝牙设备重建硬件Abstraction标准套接字接口到所有图层设计和服务级安全支持

在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

Bluetooth设备应启动并启用。

$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.start 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