How to Use Bluetooth Speaker with Raspberry Pi

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:

  1. Install PulseAudio +Bluetooth module
  2. Connect Bluetooth speaker and configure
  3. 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
Other Tutorials
High-level Steps: Prepare a Micro SD Card Download Raspbian ImageFlash the SD CardStart Raspberry Pi and Configure Find Raspberry Pi ...
There are a few different types of accessing Raspberry Pi remotely. In this post I'll introduce the following types: SSH ...
When you experimenting with Raspberry Pi, you often wish to go back to a "clean" working state, especially after messing ...
Raspberry Pi itself already has a lot of potential but with Camera, it makes even more interesting. You can easily ...
OpenCV is an Open Source Computer Vision and Machine Learning software library.With a bit of Python programming, you can turn ...
Having a database comes in handy in some Raspberry Pi projects. SQLite gives you a lightweight database with Structured Query ...
LCD Screen is a much more effective way to communicate the current status with a user, compare to beeping sound ...
Although there are few options for wired audio out from Raspberry Pi, a wireless Bluetooth speaker may be useful for ...
Don't you love robotic voice coming out from Raspberry Pi?Pico Text-to-Speech engine will give Raspberry Pi ability to speak without ...
Raspberry Pi Zero has HDMI, Bluetooth and USB for potential Audio Outputs but it is missing PWM audio output compared ...