封面图

树莓派连接蓝牙耳机

日期: 2025-4-12

Raspberry Pi Bluetooth 树莓派4B

1. 首次配置(仅需执行一次)

# 安装必要的包
sudo apt install -y bluetooth bluez bluez-tools rfkill pulseaudio pulseaudio-module-bluetooth pavucontrol

# 启用蓝牙服务开机自启
sudo systemctl enable bluetooth

# 确保配置文件包含正确设置
sudo nano /etc/bluetooth/main.conf

在main.conf中确保有以下配置:

[General]
Enable=Source,Sink,Media,Socket

2. 快速连接方法(重启后)

# 进入蓝牙控制台
bluetoothctl

# 在bluetoothctl中执行:
power on
connect 08:F0:B6:C5:02:A6    # 替换为你的耳机MAC地址

3. 完整连接流程(如果快速连接失败)

# 在bluetoothctl中执行:
power on
scan on
pair 08:F0:B6:C5:02:A6
trust 08:F0:B6:C5:02:A6
connect 08:F0:B6:C5:02:A6

4. 测试音频

# 播放测试音频
aplay /usr/share/sounds/alsa/Front_Center.wav

故障排除

# 如果听不到声音,重启音频服务
pulseaudio -k
pulseaudio --start

# 检查蓝牙服务状态
sudo systemctl status bluetooth

# 如果蓝牙被阻止
rfkill list
sudo rfkill unblock bluetooth

创建快速连接脚本(推荐)

# 创建脚本
nano ~/connect_headphone.sh

在脚本中写入:

#!/bin/bash
echo "power on" | bluetoothctl
echo "connect 08:F0:B6:C5:02:A6" | bluetoothctl

设置脚本权限:

chmod +x ~/connect_headphone.sh

之后只需运行:

~/connect_headphone.sh

将MAC地址保存到配置文件(可选)

# 创建配置文件
nano ~/.config/bluetooth/devices.conf

添加内容:

[Devices]
headphone=08:F0:B6:C5:02:A6

重要提示:

重启后连接步骤:

  1. 确保耳机开启
  2. 运行connect_headphone.sh脚本或使用bluetoothctl连接
  3. 测试音频输出