Although there are few options for wired audio out from Raspberry Pi, a wireless Bluetooth speaker may be useful for some projects.
There are 3 high-level steps in this tutorial:
- Install PulseAudio +Bluetooth module
- Connect Bluetooth speaker and configure
- Edit scripts to connect Bluetooth speaker on startup
Install PulseAudio +Bluetooth module
sudo apt-get install pulseaudio
sudo apt-get install pavucontrol paprefs
sudo apt-get install pulseaudio-module-bluetooth
Connect Bluetooth speaker and configure
bluetoothctl
power on
agent on
default-agent
quit
sudo killall bluealsa
pulseaudio --start
pactl load-module module-bluetooth-discover
Turn your Bluetooth speaker on and disconnect from other devices.
bluetoothctl
scan on
Wait until Raspberry finds the target Bluetooth speaker, and note the MAC address such as “41:42:87:63:FD:3E”.
scan off
Replace “41:42:87:63:FD:3E” with your address.
pair 41:42:87:63:FD:3E
trust 41:42:87:63:FD:3E
connect 41:42:87:63:FD:3E
Confirm it is recognized as a sound device.
pacmd list-cards
You should see your device address like “41:42:87:63:FD:3E” somewhere.
pacmd set-card-profile bluez_card.41_42_87_63_FD_3E a2dp_sink
pacmd set-default-sink bluez_sink.41_42_87_63_FD_3E.a2dp_sink
Test the Bluetooth speaker by copy an audio file to Raspberry Pi and execute below.
paplay your_sound_file.wav
The Bluetooth speaker should play the sound.
Edit scripts to connect Bluetooth speaker on startup
Modify “default.pa”.
sudo nano /etc/pulse/default.pa
Find and comment out the line below by entering “#” in front.
#load-module module-bluetooth-discover
Save and Exit.
Modify “start-pulseaudio-x11”.
sudo nano /usr/bin/start-pulseaudio-x11
Find the block of commands below.
if [ x”$SESSION_MANAGER” != x ] ; then
/usr/bin/pactl load-module module-x11-xsmp “display=$DISPLAY session_manager=$SESSION_MANAGER” > /dev/null
fi
Add 6 lines of commands right after it.
/usr/bin/pactl load-module module-bluetooth-discover
sudo killall bluealsa
pulseaudio --start
bluetoothctl << EOF
connect 41:42:87:63:FD:3E
EOF
Save and Exit.
Modify “.bashrc”.
sudo nano ~/.bashrc
Add 3 lines of commands below at the bottom.
bluetoothctl << EOF
connect 41:42:87:63:FD:3E
EOF
Save and Exit, then restart the Raspberry Pi.
sudo reboot
Test the Bluetooth speaker with an audio file again.
paplay your_sound_file.wav
The Bluetooth speaker should play the sound again..
Bluetooth Speaker Stopped Working?
If your Raspberry Pi just stopped connecting to your Bluetooth speaker without changing any configurations with an error message like below.
Failed to connect: org.bluez.Error.Failed
Try making your Raspberry Pi forget the device and then pair it again.
bluetoothctl
remove 41:42:87:63:FD:3E
scan on
Wait for your speaker to be discovered again.
scan off
pair 41:42:87:63:FD:3E
trust 41:42:87:63:FD:3E
connect 41:42:87:63:FD:3E
quit