树莓派蓝牙音箱

树莓派蓝牙音箱

使用pulseauido及bluez相关的工具实现树莓派蓝牙音箱

pulseaudio

启动pulseaudio并加载蓝牙模块提供服务

1
2
pulseaudio -D
pactl load-module module-bluetooth-discover

蓝牙

1 进入可以管理蓝牙的控制台

1
bluetoothctl

2 显示可选的蓝牙设备

1
2
3
list

Controller B8:27:EB:F1:6D:C1 raspberrypi #2 [default]

如果有usb设备通过select选择需要操作的蓝牙设备

3 显示蓝牙设备的状态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
show

Controller B8:27:EB:F1:6D:C1 (public)
Name: raspberrypi #2
Alias: raspberrypi #2
Class: 0x00000000
Powered: yes
Discoverable: no
Pairable: yes
UUID: Generic Attribute Profile (00001801-0000-1000-8000-00805f9b34fb)
UUID: A/V Remote Control (0000110e-0000-1000-8000-00805f9b34fb)
UUID: PnP Information (00001200-0000-1000-8000-00805f9b34fb)
UUID: A/V Remote Control Target (0000110c-0000-1000-8000-00805f9b34fb)
UUID: Generic Access Profile (00001800-0000-1000-8000-00805f9b34fb)
Modalias: usb:v1D6Bp0246d0532
Discovering: no

4 更改蓝牙显示名称并设置相关的状态

1
2
3
4
5
6
7
8
system-alias Pi
//供电
power on
//可配对
pairable on
//可被发现
discoverable on
trust phone's mac

此时从手机可以与蓝牙配对,但是无法从手机主动去连接,需要信任该手机才可以从手机连接树莓派的蓝牙播放音频

5 设置配置文件

1
2
3
4
5
6
7
8
9
10
sudo vim /etc/bluetooth/main.conf
# 自动开启蓝牙
[Policy]
AutoEnable=true

[General]
# 可以被发现的时间,0为一直可被扫描到
DiscoverableTimeout = 0
# 默认为可以被发现
Discoverable=true