26 - 蓝牙驱动配置

蓝牙是现代设备互联的重要无线协议。从无线耳机到键盘鼠标,从文件传输到串口通信,蓝牙无处不在。Linux 上的蓝牙栈由 BlueZ 项目提供,Arch Linux 默认集成了完整的蓝牙支持。本章将详细介绍蓝牙驱动的安装、配置与问题排查。


26.1 Linux 蓝牙栈(BlueZ)

26.1.1 架构概述

┌─────────────────────────────────────────────────┐
│ 应用层 │
│ (bluetoothctl, blueman, 文件管理器, 音频应用) │
├─────────────────────────────────────────────────┤
│ D-Bus 接口 │
├─────────────────────────────────────────────────┤
│ BlueZ 守护进程 (bluetoothd) │
│ (设备管理、配对、协议处理) │
├─────────────────────────────────────────────────┤
│ 蓝牙内核子系统 │
│ (HCI, L2CAP, SCO, RFCOMM, BNEP) │
├─────────────────────────────────────────────────┤
│ 蓝牙适配器驱动 │
│ (btusb, btintel, btbcm, btrtl, hci_uart) │
├─────────────────────────────────────────────────┤
│ 蓝牙硬件(USB/UART/SDIO 适配器) │
└─────────────────────────────────────────────────┘

26.1.2 BlueZ 组件

组件说明
bluetoothdBlueZ 守护进程,核心服务
bluetoothctl命令行蓝牙管理工具
btmon蓝牙 HCI 流量监控
btmgmt蓝牙管理接口工具
hciconfig旧版 HCI 配置工具(已废弃)
hcitool旧版 HCI 工具(已废弃)
obexdOBEX 文件传输守护进程

26.1.3 蓝牙协议栈层次

协议层协议名用途
传输层HCIHost Controller Interface,主机与控制器通信
逻辑层L2CAP逻辑链路控制和适配协议
音频SCO/eSCO同步面向连接,电话音频
串口RFCOMM串口仿真
网络BNEP蓝牙网络封装协议
文件OBEX对象交换协议
音频流A2DP高级音频分发配置文件
输入HID人机接口设备(键鼠)
低功耗GATT/ATTBLE 通用属性协议

26.2 硬件识别

26.2.1 识别蓝牙适配器

# USB 蓝牙适配器
lsusb | grep -i bluetooth
# 输出示例:
# Bus 001 Device 003: ID 8087:0a2b Intel Corp. Bluetooth wireless interface
 
# 查看内核是否识别蓝牙设备
dmesg | grep -i bluetooth
dmesg | grep -i "Bluetooth\|btusb\|btintel\|btbcm\|btrtl"
 
# 查看 HCI 设备
hciconfig -a
# 或使用 bluetoothctl
bluetoothctl show
 
# 查看蓝牙控制器信息
bluetoothctl list

26.2.2 rfkill 检查

# 查看无线设备状态(包括蓝牙)
rfkill list
 
# 输出示例:
# 0: hci0: Bluetooth
# Soft blocked: no
# Hard blocked: no
 
# 如果蓝牙被软件阻止
rfkill unblock bluetooth
 
# 如果被硬件开关阻止(笔记本),需要按物理按键或 Fn 组合键
 
# 查看所有无线设备
rfkill list all

26.2.3 内核模块检查

# 查看蓝牙相关模块
lsmod | grep -i "bt\|bluetooth"
 
# 常见蓝牙内核模块:
# bluetooth - 蓝牙核心模块
# btusb - USB 蓝牙适配器驱动
# btintel - Intel 蓝牙固件加载
# btbcm - Broadcom 蓝牙
# btrtl - Realtek 蓝牙
# btmtk - MediaTek 蓝牙
# hci_uart - UART(串口)蓝牙适配器
# bnep - 蓝牙网络封装
# rfcomm - 蓝牙串口
 
# 手动加载模块(如果需要)
sudo modprobe btusb
sudo modprobe bluetooth

26.3 安装与启用蓝牙服务

26.3.1 安装必要软件包

# 核心蓝牙栈
sudo pacman -S bluez bluez-utils
 
# 图形化蓝牙管理(可选)
sudo pacman -S blueman # GTK 蓝牙管理器
# 或
sudo pacman -S bluedevil # KDE 蓝牙管理
 
# 蓝牙音频支持(PipeWire)
sudo pacman -S pipewire-pulse # 已包含蓝牙支持
 
# 蓝牙文件传输
sudo pacman -S obexftp
# 或
sudo pacman -S bluez-obex # BlueZ OBEX 支持
 
# 低功耗蓝牙开发工具(可选)
sudo pacman -S bluez-hid2hci # HID 转 HCI 工具

26.3.2 启用蓝牙服务

# 启用并启动蓝牙服务
sudo systemctl enable bluetooth.service
sudo systemctl start bluetooth.service
 
# 检查服务状态
systemctl status bluetooth.service
 
# 查看蓝牙服务日志
journalctl -u bluetooth -f
 
# 如果需要开机自动启用蓝牙适配器电源
# 编辑 /etc/bluetooth/main.conf
# [Policy] 部分设置
# AutoEnable=true

26.3.3 基本配置文件

# 主配置文件
# /etc/bluetooth/main.conf
[General]
# 设备名称
Name = ArchLinux
# 可被发现的超时时间(0=永久可见)
DiscoverableTimeout = 0
# 可配对的超时时间(0=永远可配对)
PairableTimeout = 0
# 隐私模式
Privacy = device
# 允许的 JustWorks 配对
JustWorksRepairing = always
# 启用实验性功能
# Experimental = true
 
[Policy]
# 开机自动打开蓝牙
AutoEnable=true
# 自动连接已配对设备
ReconnectAttempts=7
ReconnectIntervals=1,2,4,8,16,32,64
 
[GATT]
# BLE GATT 缓存
Cache = always

26.4 bluetoothctl 详细使用

bluetoothctl 是 BlueZ 提供的交互式蓝牙管理工具。

26.4.1 基本操作

# 进入交互模式
bluetoothctl
 
# 也可以使用非交互模式(单条命令)
bluetoothctl power on
bluetoothctl scan on
bluetoothctl devices

26.4.2 常用命令大全

# === 控制器管理 ===
# 列出所有控制器
bluetoothctl list
 
# 查看控制器信息
bluetoothctl show
bluetoothctl show <controller_mac>
 
# 开/关蓝牙
bluetoothctl power on
bluetoothctl power off
 
# 设置可发现
bluetoothctl discoverable on
bluetoothctl discoverable off
 
# 设置可配对
bluetoothctl pairable on
bluetoothctl pairable off
 
# 设置设备名称
bluetoothctl system-alias "MyArchPC"
 
# === 扫描设备 ===
# 开始扫描
bluetoothctl scan on
 
# 停止扫描
bluetoothctl scan off
 
# 列出已发现的设备
bluetoothctl devices
 
# 列出已配对的设备
bluetoothctl paired-devices
 
# 查看设备详细信息
bluetoothctl info <device_mac>
 
# === 配对与连接 ===
# 配对设备
bluetoothctl pair <device_mac>
 
# 连接设备
bluetoothctl connect <device_mac>
 
# 断开连接
bluetoothctl disconnect <device_mac>
 
# 信任设备(允许自动连接)
bluetoothctl trust <device_mac>
bluetoothctl untrust <device_mac>
 
# 删除(取消配对)设备
bluetoothctl remove <device_mac>
 
# 阻止/取消阻止设备
bluetoothctl block <device_mac>
bluetoothctl unblock <device_mac>

26.4.3 不同设备类型的配对流程

蓝牙耳机/音箱:

# 1. 开启扫描
bluetoothctl scan on
 
# 2. 将耳机置于配对模式(通常长按电源键)
# 3. 等待设备出现在扫描列表中
# [NEW] Device AA:BB:CC:DD:EE:FF Sony WH-1000XM4
 
# 4. 配对
bluetoothctl pair AA:BB:CC:DD:EE:FF
# 通常耳机不需要输入 PIN,直接配对成功
 
# 5. 信任(允许自动重连)
bluetoothctl trust AA:BB:CC:DD:EE:FF
 
# 6. 连接
bluetoothctl connect AA:BB:CC:DD:EE:FF
 
# 7. 确认音频输出已切换
wpctl status
# 应该看到新的蓝牙音频 Sink

蓝牙键盘:

# 1. 开启扫描
bluetoothctl scan on
 
# 2. 将键盘置于配对模式
 
# 3. 配对(可能需要在键盘上输入 PIN)
bluetoothctl pair AA:BB:CC:DD:EE:FF
# 提示:输入键盘上显示的 PIN 或在键盘上输入屏幕显示的 PIN
# [agent] Confirm passkey 123456 (yes/no): yes
# 或
# [agent] Enter PIN code: (在蓝牙键盘上输入显示的数字并回车)
 
# 4. 信任并连接
bluetoothctl trust AA:BB:CC:DD:EE:FF
bluetoothctl connect AA:BB:CC:DD:EE:FF

蓝牙鼠标:

# 蓝牙鼠标通常使用 Just Works 配对
bluetoothctl scan on
# 按鼠标配对按钮
bluetoothctl pair AA:BB:CC:DD:EE:FF
bluetoothctl trust AA:BB:CC:DD:EE:FF
bluetoothctl connect AA:BB:CC:DD:EE:FF

蓝牙手柄(游戏控制器):

# DualShock 4 / DualSense
# 同时按住 PS 键和 Share 键进入配对模式
bluetoothctl scan on
bluetoothctl pair AA:BB:CC:DD:EE:FF
bluetoothctl trust AA:BB:CC:DD:EE:FF
bluetoothctl connect AA:BB:CC:DD:EE:FF
 
# Xbox 无线控制器
# 按住控制器顶部配对按钮
# 注意:Xbox 控制器可能需要 xpadneo 驱动(AUR)
yay -S xpadneo-dkms

26.4.4 交互式使用完整示例

$ bluetoothctl
Agent registered
[CHG] Controller 00:11:22:33:44:55 Pairable: yes
 
[bluetooth]# power on
Changing power on succeeded
 
[bluetooth]# agent on
Agent is already registered
 
[bluetooth]# default-agent
Default agent request successful
 
[bluetooth]# scan on
Discovery started
[NEW] Device AA:BB:CC:DD:EE:FF WH-1000XM4
[NEW] Device 11:22:33:44:55:66 MX Master 3
 
[bluetooth]# pair AA:BB:CC:DD:EE:FF
Attempting to pair with AA:BB:CC:DD:EE:FF
[CHG] Device AA:BB:CC:DD:EE:FF Connected: yes
[CHG] Device AA:BB:CC:DD:EE:FF Paired: yes
Pairing successful
 
[bluetooth]# trust AA:BB:CC:DD:EE:FF
[CHG] Device AA:BB:CC:DD:EE:FF Trusted: yes
Changing AA:BB:CC:DD:EE:FF trust succeeded
 
[bluetooth]# connect AA:BB:CC:DD:EE:FF
Attempting to connect to AA:BB:CC:DD:EE:FF
[CHG] Device AA:BB:CC:DD:EE:FF Connected: yes
Connection successful
 
[WH-1000XM4]# scan off
Discovery stopped
 
[WH-1000XM4]# quit

26.5 蓝牙音频

26.5.1 音频编解码器

编解码器码率延迟音质支持情况
SBC328 kbps一般所有设备必须支持
AAC256 kbps良好iOS 设备常用
aptX352 kbps中低良好高通芯片设备
aptX HD576 kbps优秀高通高端设备
aptX Adaptive可变优秀新一代高通设备
LDAC990 kbps极佳索尼设备
LC3可变极低优秀BT 5.2+ LE Audio

26.5.2 PipeWire 蓝牙音频配置

# PipeWire 默认支持蓝牙音频,确保安装了必要组件
sudo pacman -S pipewire pipewire-pulse wireplumber
 
# 蓝牙编解码器支持由 PipeWire 内置
# 默认支持 SBC、SBC-XQ、AAC
# LDAC 和 aptX 需要额外库
 
# 安装 LDAC 编解码器支持
sudo pacman -S libldac
 
# 安装 aptX 支持(如果需要)
sudo pacman -S libfreeaptx # 开源 aptX 实现
 
# 查看当前蓝牙音频连接使用的编解码器
pw-dump | grep -A 5 "bluez"
 
# 使用 wpctl 查看蓝牙设备
wpctl status | grep -A 10 "bluetooth"

26.5.3 编解码器选择与配置

# WirePlumber 蓝牙配置
# ~/.config/wireplumber/wireplumber.conf.d/50-bluetooth.conf
monitor.bluez.properties = {
 # 启用 LDAC、aptX、AAC 编解码器
 bluez5.enable-sbc-xq = true
 bluez5.enable-msbc = true
 bluez5.enable-hw-volume = true

 # 编解码器支持列表
 bluez5.codecs = [ sbc sbc_xq aac ldac aptx aptx_hd aptx_ll aptx_ll_duplex ]
}

monitor.bluez.rules = [
 {
 matches = [
 { device.name = "~bluez_card.*" }
 ]
 actions = {
 update-props = {
 # 优先使用高品质编解码器
 bluez5.auto-connect = [ hfp_hf hsp_hs a2dp_sink ]
 bluez5.hw-volume = [ hfp_ag hsp_ag a2dp_source ]
 }
 }
 }
]

26.5.4 A2DP 与 HFP/HSP 切换

蓝牙音频有两种主要模式:

模式说明音质麦克风
A2DP高品质音乐播放高(立体声)不可用
HFP/HSP通话模式低(单声道 8/16kHz)可用
# 查看蓝牙设备当前配置文件
pactl list cards | grep -A 30 "bluez"
 
# 切换到 A2DP 模式(高品质音乐)
pactl set-card-profile bluez_card.AA_BB_CC_DD_EE_FF a2dp-sink
 
# 切换到 HFP 模式(通话,有麦克风)
pactl set-card-profile bluez_card.AA_BB_CC_DD_EE_FF headset-head-unit
 
# 使用 wpctl 切换
# 先查看可用配置
wpctl inspect <bluetooth_card_id>

26.5.5 高品质蓝牙语音(mSBC 宽带语音)

# mSBC 提供 16kHz 宽带语音(默认 CVSD 是 8kHz 窄带)
# PipeWire 默认启用 mSBC
 
# 确认配置
# ~/.config/wireplumber/wireplumber.conf.d/50-bluetooth.conf
monitor.bluez.properties = {
 bluez5.enable-msbc = true
 bluez5.hfphsp-backend = "native"
}
# 验证是否使用了 mSBC
pw-dump | grep -i msbc
 
# 如果通话质量差,可以尝试切换 HFP 后端
# native 后端(推荐)
# ofono 后端(需要 ofono 服务)

26.5.6 蓝牙音频延迟优化

# 减少蓝牙音频延迟(适合看视频或游戏)
# ~/.config/wireplumber/wireplumber.conf.d/50-bluetooth-latency.conf
monitor.bluez.rules = [
 {
 matches = [
 { node.name = "~bluez_output.*" }
 ]
 actions = {
 update-props = {
 # 减少缓冲区大小(可能导致音频断续)
 api.bluez5.a2dp.internal-delay = 0
 session.suspend-timeout-seconds = 0
 }
 }
 }
]

26.6 蓝牙键盘/鼠标

26.6.1 配对与连接

# 基本配对流程(参见 26.4.3)
bluetoothctl scan on
bluetoothctl pair <mac>
bluetoothctl trust <mac>
bluetoothctl connect <mac>
 
# 查看 HID 设备
cat /proc/bus/input/devices | grep -A 5 "Bluetooth"

26.6.2 登录界面(GDM/SDDM)蓝牙支持

# 要在登录界面使用蓝牙键盘,需要确保:
# 1. 蓝牙服务在登录界面可用
sudo systemctl enable bluetooth.service
 
# 2. 设备已被信任(trust)
# 已信任的设备会在蓝牙服务启动时自动连接
 
# 3. 如果使用 GDM,确保 GDM 用户有蓝牙权限
# GDM 通常已自动处理

26.6.3 鼠标性能优化

# 降低蓝牙鼠标轮询间隔(减少延迟)
# 查看当前连接间隔
cat /sys/kernel/debug/bluetooth/hci0/conn_info_min_age
 
# 对于 BLE 鼠标,连接参数由设备协商
# 某些鼠标支持高刷新率模式(如 Logitech MX Master 3)
 
# 如果鼠标有延迟,尝试:
# 1. 确保适配器支持 BT 5.0+
# 2. 减少同一频段的 WiFi 干扰
# 3. 重新配对设备

26.7 蓝牙文件传输(OBEX)

26.7.1 发送文件

# 使用 bluetoothctl 的 OBEX 传输(需要 obex 服务)
# 安装 obexftp
sudo pacman -S obexftp
 
# 发送文件到手机
obexftp --bluetooth <phone_mac> --channel 10 --put file.jpg
 
# 或使用 bluetooth-sendto(GNOME)
bluetooth-sendto --device=AA:BB:CC:DD:EE:FF file.pdf
 
# 使用 blueman 图形界面发送
# 右键设备 -> 发送文件

26.7.2 接收文件

# 启动 OBEX 守护进程(接收文件)
# BlueZ 自带的 obexd 通常由桌面环境自动启动
 
# 手动启动 obexd
/usr/lib/bluetooth/obexd
 
# 接收的文件默认保存在 ~/Downloads
 
# 使用 dbus 接受传入文件
# 桌面环境通常会弹出确认对话框

26.7.3 浏览远程设备文件

# 使用 obexftp 浏览手机文件
obexftp --bluetooth <mac> --list /
 
# 下载文件
obexftp --bluetooth <mac> --get /DCIM/photo.jpg
 
# 使用 GVFS(GNOME 文件管理器集成)
# 文件管理器中浏览:obex://[AA:BB:CC:DD:EE:FF]/

26.8 蓝牙串口(RFCOMM)

# 蓝牙串口可用于连接蓝牙 GPS、蓝牙打印机等设备
 
# 加载 rfcomm 模块
sudo modprobe rfcomm
 
# 绑定蓝牙设备到串口
sudo rfcomm bind /dev/rfcomm0 <device_mac> 1
 
# 查看绑定状态
rfcomm show
 
# 使用串口工具连接
screen /dev/rfcomm0 9600
# 或
minicom -D /dev/rfcomm0
 
# 释放绑定
sudo rfcomm release /dev/rfcomm0
 
# 使用 sdptool 查看设备支持的服务
sdptool browse <device_mac>

26.9 自动连接与开机配对

26.9.1 配置自动连接

# 1. 确保设备已被信任
bluetoothctl trust <device_mac>
 
# 2. 确保 AutoEnable 开启
# /etc/bluetooth/main.conf
[Policy]
AutoEnable=true
ReconnectAttempts=7
ReconnectIntervals=1,2,4,8,16,32,64
# 3. 确保蓝牙服务自启动
sudo systemctl enable bluetooth.service

26.9.2 创建自动连接脚本

# /etc/systemd/system/bluetooth-autoconnect@.service
[Unit]
Description=Bluetooth auto-connect for %i
After=bluetooth.service
Requires=bluetooth.service
 
[Service]
Type=oneshot
ExecStartPre=/usr/bin/sleep 5
ExecStart=/usr/bin/bluetoothctl connect %i
 
[Install]
WantedBy=multi-user.target
# 启用自动连接特定设备
sudo systemctl enable bluetooth-autoconnect@AA:BB:CC:DD:EE:FF.service

26.9.3 使用 udev 规则自动执行

# /etc/udev/rules.d/90-bluetooth-autoconnect.rules
ACTION=="add", SUBSYSTEM=="bluetooth", RUN+="/usr/bin/bluetoothctl connect AA:BB:CC:DD:EE:FF"

26.10 双系统蓝牙配对共享

在 Windows/Linux 双系统中,蓝牙设备需要在两个系统中分别配对。但由于配对密钥不同,切换系统后需要重新配对。可以通过共享配对密钥解决。

26.10.1 原理

蓝牙配对后,设备和适配器之间共享一个 Link Key(经典蓝牙)或 LTK/IRK(BLE)。如果两个系统使用相同的密钥,设备可以在不重新配对的情况下连接。

26.10.2 操作步骤

# 1. 先在 Linux 下配对设备
bluetoothctl pair <device_mac>
bluetoothctl trust <device_mac>
 
# 2. 重启进入 Windows,配对同一设备
 
# 3. 从 Windows 提取配对密钥
# 方法一:使用 PsExec 导出注册表
# HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\BTHPORT\Parameters\Keys
 
# 方法二:使用 chntpw 从 Linux 读取 Windows 注册表
sudo pacman -S chntpw
sudo mount /dev/sdXY /mnt/windows
sudo chntpw -e /mnt/windows/Windows/System32/config/SYSTEM
 
# 在 chntpw 中导航到:
# \ControlSet001\Services\BTHPORT\Parameters\Keys\<adapter_mac>\<device_mac>
 
# 4. 将 Windows 的密钥写入 Linux
# Linux 蓝牙配对信息存储位置:
# /var/lib/bluetooth/<adapter_mac>/<device_mac>/info
 
# 5. 编辑 info 文件,替换 Key 值
sudo vim /var/lib/bluetooth/00:11:22:33:44:55/AA:BB:CC:DD:EE:FF/info

配对信息文件格式:

[General]
Name=设备名称
Class=0x000000
SupportedTechnologies=BR/EDR;LE;
Trusted=true
Blocked=false
 
[LinkKey]
Key=AABBCCDDEEFF00112233445566778899
Type=4
PINLength=0
 
[LongTermKey]
Key=AABBCCDDEEFF00112233445566778899
Authenticated=0
EncSize=16
EDiv=12345
Rand=1234567890123456
 
[IdentityResolvingKey]
Key=AABBCCDDEEFF00112233445566778899
# 6. 重启蓝牙服务
sudo systemctl restart bluetooth.service

26.10.3 BLE 设备的双系统共享

BLE 设备需要同步更多密钥:

# 需要从 Windows 提取以下密钥:
# - LTK (Long Term Key)
# - IRK (Identity Resolving Key)
# - CSRK (Connection Signature Resolving Key)
# - EDiv 和 Rand 值
 
# 使用 bt-dualboot(AUR 工具,自动化此过程)
yay -S bt-dualboot
sudo bt-dualboot

26.11 常见问题排查

26.11.1 蓝牙适配器未识别

# 1. 检查 USB 设备是否被系统检测
lsusb | grep -i bluetooth
 
# 2. 检查是否被 rfkill 阻止
rfkill list bluetooth
rfkill unblock bluetooth
 
# 3. 检查内核模块
lsmod | grep btusb
# 如果没有加载
sudo modprobe btusb
 
# 4. 检查内核日志
dmesg | grep -i "bluetooth\|btusb\|firmware"
 
# 5. 检查固件是否存在
ls /lib/firmware/intel/ # Intel 蓝牙固件
ls /lib/firmware/rtl_bt/ # Realtek 蓝牙固件
ls /lib/firmware/brcm/ # Broadcom 蓝牙固件
 
# 6. 安装缺失固件
sudo pacman -S linux-firmware
 
# 7. 如果是 USB 蓝牙 dongle,尝试重新插拔
# 或重置 USB 设备
echo "1-1" | sudo tee /sys/bus/usb/drivers/usb/unbind
echo "1-1" | sudo tee /sys/bus/usb/drivers/usb/bind

26.11.2 配对失败

# 1. 确保设备处于配对模式
# 大多数设备需要长按按钮进入配对模式
 
# 2. 删除旧配对信息并重试
bluetoothctl remove <device_mac>
bluetoothctl scan on
bluetoothctl pair <device_mac>
 
# 3. 确认 agent 已注册
bluetoothctl agent on
bluetoothctl default-agent
 
# 4. 某些设备需要特定的配对方法
# 尝试以下 agent 类型:
bluetoothctl agent NoInputNoOutput
bluetoothctl agent KeyboardDisplay
bluetoothctl agent KeyboardOnly
 
# 5. 查看详细日志
journalctl -u bluetooth -f
# 同时尝试配对
 
# 6. 重启蓝牙栈
sudo systemctl restart bluetooth
bluetoothctl power off
bluetoothctl power on

26.11.3 连接不稳定

# 1. 检查信号强度
bluetoothctl info <device_mac> | grep RSSI
 
# 2. 减少 2.4GHz 干扰
# 蓝牙与 WiFi 2.4G 共用频段
# 尝试将 WiFi 切换到 5GHz
 
# 3. 调整蓝牙电源级别
# 查看当前发射功率
hciconfig hci0 inqtpl
 
# 4. 更新固件
sudo pacman -S linux-firmware
# 重启后生效
 
# 5. 禁用蓝牙省电模式
# /etc/modprobe.d/bluetooth.conf
options btusb enable_autosuspend=0
# 6. USB 蓝牙适配器:禁用 USB 自动挂起
# /etc/udev/rules.d/91-bluetooth-usb.rules
ACTION=="add", SUBSYSTEM=="usb", ATTRS{idVendor}=="8087", ATTR{power/autosuspend}="-1"

26.11.4 音频断续

# 1. 切换到更稳定的编解码器
# 将 LDAC 切换到 SBC-XQ 或 aptX
pactl list cards | grep -A 5 "bluez"
 
# 2. 增加音频缓冲区
# ~/.config/wireplumber/wireplumber.conf.d/50-bluetooth-buffer.conf
monitor.bluez.rules = [
 {
 matches = [
 { node.name = "~bluez_output.*" }
 ]
 actions = {
 update-props = {
 # 增加缓冲区减少断续
 node.latency = "512/48000"
 }
 }
 }
]
# 3. 检查 CPU 负载(编解码需要 CPU)
top -H | grep pipewire
 
# 4. 减少蓝牙干扰
# 关闭不使用的蓝牙设备
# 远离微波炉、USB 3.0 设备等干扰源
 
# 5. 重新配对并选择不同编解码器
bluetoothctl disconnect <mac>
bluetoothctl connect <mac>
 
# 6. 检查 PipeWire 蓝牙日志
journalctl --user -u pipewire -f | grep -i blue

26.11.5 排查流程总结

#!/bin/bash
echo "=== 蓝牙适配器状态 ==="
bluetoothctl show
 
echo -e "\n=== rfkill 状态 ==="
rfkill list bluetooth
 
echo -e "\n=== 蓝牙服务状态 ==="
systemctl status bluetooth --no-pager
 
echo -e "\n=== 内核模块 ==="
lsmod | grep -i "bt\|bluetooth"
 
echo -e "\n=== USB 蓝牙设备 ==="
lsusb | grep -i bluetooth
 
echo -e "\n=== 已配对设备 ==="
bluetoothctl paired-devices
 
echo -e "\n=== 内核日志(蓝牙) ==="
dmesg | grep -i bluetooth | tail -20
 
echo -e "\n=== 蓝牙服务日志 ==="
journalctl -u bluetooth --no-pager -n 20

26.12 蓝牙 5.0/5.1/5.2/5.3 新特性

26.12.1 版本演进对比

版本主要新特性实际影响
BT 5.02x 速度、4x 范围、8x 广播容量更快的文件传输、更远距离
BT 5.1方向定位(AoA/AoD)室内定位精度提升
BT 5.2LE Audio、LC3 编解码器、多流音质飞跃、助听器支持
BT 5.3增强周期性广播、子评级IoT 设备功耗降低

26.12.2 LE Audio(蓝牙 5.2+)

# LE Audio 是蓝牙音频的未来方向
# 核心特性:
# - LC3 编解码器(比 SBC 更高音质、更低功耗)
# - 多流音频(左右耳独立通道)
# - 广播音频(Auracast,公共场所音频广播)
# - 助听器支持
 
# 检查适配器是否支持 LE Audio
bluetoothctl show | grep -i "le\|iso"
 
# BlueZ 5.65+ 开始支持 LE Audio
# 需要内核 6.0+ 和支持 ISO 通道的蓝牙适配器
 
# 启用实验性 LE Audio 支持
# /etc/bluetooth/main.conf
[General]
Experimental = true
KernelExperimental = true
 
[LE]
EnableAdvMonInterleaveScan = true

26.12.3 确认适配器版本

# 查看蓝牙适配器支持的版本
bluetoothctl show | grep -i "version\|manufacturer"
 
# 使用 btmgmt 查看详情
btmgmt info
 
# HCI 版本对应蓝牙版本:
# HCI 9.0 -> Bluetooth 5.0
# HCI 10.0 -> Bluetooth 5.1
# HCI 11.0 -> Bluetooth 5.2
# HCI 12.0 -> Bluetooth 5.3
 
# 查看支持的功能
btmgmt features

26.13 低功耗蓝牙(BLE)

26.13.1 BLE 基础

BLE(Bluetooth Low Energy)是蓝牙 4.0 引入的低功耗规范,广泛用于 IoT 设备、传感器、智能家居等。

# BLE 设备扫描
bluetoothctl scan le
 
# 使用 hcitool 扫描 BLE 设备(旧方式)
sudo hcitool lescan
 
# 查看 BLE 设备的 GATT 服务
bluetoothctl
[bluetooth]# menu gatt
[bluetooth]# list-attributes <device_mac>

26.13.2 GATT 交互

# 连接 BLE 设备
bluetoothctl connect <device_mac>
 
# 查看 GATT 服务
bluetoothctl
[device]# menu gatt
[device]# list-attributes
 
# 读取特征值
[device]# select-attribute <uuid>
[device]# read
 
# 写入特征值
[device]# select-attribute <uuid>
[device]# write 0x01
 
# 启用通知
[device]# select-attribute <uuid>
[device]# notify on

26.13.3 BLE 开发工具

# 安装 BLE 开发/调试工具
sudo pacman -S bluez-utils
 
# gatttool(旧工具,仍可用)
gatttool -b <mac> --interactive
[LE]> connect
[LE]> primary
[LE]> characteristics
[LE]> char-read-hnd 0x0003
 
# 使用 Python 进行 BLE 开发
pip install bleak # 跨平台 BLE 库
 
# 示例:扫描 BLE 设备
python3 -c "
import asyncio
from bleak import BleakScanner
 
async def main():
 devices = await BleakScanner.discover()
 for d in devices:
 print(f'{d.address}: {d.name}')
 
asyncio.run(main())
"

26.14 内核固件问题与解决

26.14.1 常见固件缺失问题

# 开机时常见的固件警告
dmesg | grep -i "firmware\|btusb\|btintel\|btrtl\|btbcm"
 
# 典型错误信息:
# bluetooth hci0: firmware file intel/ibt-20-1-3.sfi not found
# Bluetooth: hci0: Failed to load Intel firmware file intel/ibt-xxx.sfi
 
# 解决方案:安装 linux-firmware
sudo pacman -S linux-firmware
 
# 如果 linux-firmware 不包含所需固件
# 检查 AUR 是否有专门的固件包
yay -Ss bluetooth firmware

26.14.2 各厂商固件位置

厂商固件路径驱动模块
Intel/lib/firmware/intel/btintel
Realtek/lib/firmware/rtl_bt/btrtl
Broadcom/lib/firmware/brcm/btbcm
MediaTek/lib/firmware/mediatek/btmtk
QCA (Qualcomm)/lib/firmware/qca/btqca

26.14.3 手动安装固件

# 如果官方仓库固件过旧,从 linux-firmware git 获取最新版
 
# 克隆固件仓库(很大,建议浅克隆)
git clone --depth 1 https://git.kernel.org/pub/scm/linux/kernel/git/firmware/linux-firmware.git /tmp/linux-firmware
 
# 复制需要的固件
sudo cp /tmp/linux-firmware/intel/ibt-* /lib/firmware/intel/
# 或
sudo cp /tmp/linux-firmware/rtl_bt/* /lib/firmware/rtl_bt/
 
# 更新 initramfs(如果固件在早期启动时需要)
sudo mkinitcpio -P
 
# 重新加载蓝牙模块
sudo modprobe -r btusb
sudo modprobe btusb

26.14.4 Realtek 蓝牙适配器常见问题

# Realtek 蓝牙适配器经常需要特定固件
# 常见型号:RTL8761B、RTL8821C、RTL8852A
 
# 检查设备 ID
lsusb | grep -i realtek
# 例如:ID 0bda:8771 Realtek Semiconductor Corp. Bluetooth Radio
 
# 确认固件文件存在
ls /lib/firmware/rtl_bt/
 
# 如果固件缺失,从 AUR 安装
yay -S rtl8761bu-fw # 示例
 
# 或手动下载固件
# https://github.com/nicman23/bluetooth-firmwares (社区维护)

26.14.5 Intel AX200/AX210 蓝牙问题

# Intel AX200/AX201/AX210 是常见的无线网卡+蓝牙组合
# 蓝牙部分通常工作良好,但偶有固件问题
 
# 检查固件版本
dmesg | grep -i "intel.*firmware\|ibt"
 
# 如果蓝牙不稳定,尝试:
# 1. 更新 linux-firmware
sudo pacman -S linux-firmware
 
# 2. 禁用蓝牙省电
# /etc/modprobe.d/iwlwifi.conf
options iwlwifi bt_coex_active=0
# 3. 如果 WiFi 和蓝牙互相干扰
# 这是 2.4GHz 频段的固有问题
# 尽量使用 5GHz WiFi
 
# 4. 更新到最新内核
sudo pacman -S linux linux-headers

26.15 图形化蓝牙管理工具

26.15.1 Blueman(GTK)

# 安装
sudo pacman -S blueman
 
# 启动
blueman-manager
 
# Blueman 功能:
# - 设备发现与配对
# - 文件传输
# - 音频设备管理
# - 网络共享(NAP/DUN)
# - 系统托盘图标(blueman-applet)
 
# 启用 blueman-applet 自启动
# 通常桌面环境自动处理

26.15.2 KDE Bluedevil

# 安装(KDE Plasma 通常预装)
sudo pacman -S bluedevil
 
# 通过系统设置管理蓝牙
# 系统设置 -> 蓝牙
 
# 或使用系统托盘蓝牙图标

26.15.3 GNOME 蓝牙

# 安装(GNOME 通常预装)
sudo pacman -S gnome-bluetooth-3.0
 
# 通过 GNOME 设置管理蓝牙
# 设置 -> 蓝牙
 
# 快速设置中也有蓝牙开关