Introduction: Raspberry Pi Bluetooth Speaker

Hi all.

Here is an Instructable about how to make a Bluetooth Speaker using Raspberry Pi. This was made after a brand new install of the latest Raspbian (as of 31/10/2020.) In this Instructable we will be converting the Raspberry Pi to receive audio over Bluetooth and output it through the built in 3.5 audio jack. If you are using Raspberry Pi 1 or 2 then you will have to use a separate Bluetooth Adapter. For this Instructable we will be using Raspberry Pi 3B.

Supplies

Raspberry Pi 1, 2, 3 or 4.

Speaker with 3.5 audio jack input.

Power Supply for Raspberry Pi.

Mouse and Keyboard for Raspberry Pi (or you can SSH in with another computer).

Bluetooth dongle if using Raspberry Pi 1 or 2.

Step 1: Log in to Raspberry Pi.

This step is fairly self-intuitive!

You will need to be logged in to the Raspberry Pi either through SSH or with a monitor, mouse and keyboard. If you are in the Raspian GUI then open a Terminal. If you are SSH'ing then you will already be on there.

Before you proceed with the next steps it is advisable to make sure that you have updated your system.

sudo apt-get update
sudo apt-get upgrade

This may take a few minutes.

Step 2: Sort Out Bluetooth.

If you enter

sudo systemctl status blue*

then it will probably return something like below.

pi@raspberrypi:~ $ sudo systemctl status blue*
● bluetooth.service - Bluetooth service
   Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled; vendor preset
   Active: active (running) since Sat 2020-10-31 12:36:04 GMT; 40min ago
     Docs: man:bluetoothd(8)
 Main PID: 523 (bluetoothd)
   Status: "Running"
    Tasks: 1 (limit: 2065)
   CGroup: /system.slice/bluetooth.service
           └─523 /usr/lib/bluetooth/bluetoothd

Oct 31 12:36:04 raspberrypi systemd[1]: Starting Bluetooth service...
Oct 31 12:36:04 raspberrypi bluetoothd[523]: Bluetooth daemon 5.50
Oct 31 12:36:04 raspberrypi systemd[1]: Started Bluetooth service.
Oct 31 12:36:04 raspberrypi bluetoothd[523]: Starting SDP server
Oct 31 12:36:04 raspberrypi bluetoothd[523]: Bluetooth management interface 1.14
Oct 31 12:36:04 raspberrypi bluetoothd[523]: Sap driver initialization failed.
Oct 31 12:36:04 raspberrypi bluetoothd[523]: sap-server: Operation not permitted
Oct 31 12:36:04 raspberrypi bluetoothd[523]: Endpoint registered: sender=:1.10 p
Oct 31 12:36:04 raspberrypi bluetoothd[523]: Endpoint registered: sender=:1.10 p
Oct 31 12:36:04 raspberrypi bluetoothd[523]: Failed to set privacy: Rejected (0x

● bluealsa.service - BluezALSA proxy
   Loaded: loaded (/lib/systemd/system/bluealsa.service; static; vendor preset:

You will notice that there are a number of errors. We will first sort these out.

The first ones we will sort are "sap driver initialization failed." and "sap-server: Operation not permitted". Enter

sudo nano /lib/systemd/system/bluetooth.service 

into the terminal. It will come up with this.

[Unit]
Description=Bluetooth service
Documentation=man:bluetoothd(8)
ConditionPathIsDirectory=/sys/class/bluetooth

[Service]
Type=dbus
BusName=org.bluez
ExecStart=/usr/lib/bluetooth/bluetoothd
NotifyAccess=main
#WatchdogSec=10
#Restart=on-failure
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
LimitNPROC=1
ProtectHome=true
ProtectSystem=full

[Install]
WantedBy=bluetooth.target
Alias=dbus-org.bluez.service

Add

--noplugin=sap

after

ExecStart=/usr/lib/bluetooth/bluetoothd

to make it like below.

[Unit]
Description=Bluetooth service
Documentation=man:bluetoothd(8)
ConditionPathIsDirectory=/sys/class/bluetooth

[Service]
Type=dbus
BusName=org.bluez
ExecStart=/usr/lib/bluetooth/bluetoothd --noplugin=sap
NotifyAccess=main
#WatchdogSec=10
#Restart=on-failure
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
LimitNPROC=1
ProtectHome=true
ProtectSystem=full

[Install]
WantedBy=bluetooth.target
Alias=dbus-org.bluez.service

Save and exit. (ctrl-x, y, enter). Then

sudo reboot

When you are back on terminal enter

sudo systemctl status blue*

The errors should be solved, except for one.

Failed to set privacy: Rejected (0x0b)

Enter

sudo systemctl restart blue*

to fix.

We are not quite done yet. We will also need to add the user "pi" to bluetooth using

sudo adduser pi bluetooth

pi@raspberrypi:~ $ sudo adduser pi bluetooth
Adding user `pi' to group `bluetooth' ...
Adding user pi to group bluetooth
Done.

We next need to give bluealsa the ability to play audio streams through the Raspberry Pi audio interfaces (namely the 3.5 audio jack).

sudo nano /lib/systemd/system/bluealsa.service

It should come up with

[Unit]
Description=BluezALSA proxy
Requires=bluetooth.service
After=bluetooth.service

[Service]
Type=simple
User=root
ExecStart=/usr/bin/bluealsa

Add

-p a2dp-source -p a2dp-sink

after

ExecStart=/usr/bin/bluealsa

to make

[Unit]
Description=BluezALSA proxy
Requires=bluetooth.service
After=bluetooth.service

[Service]
Type=simple
User=root
ExecStart=/usr/bin/bluealsa -p a2dp-source -p a2dp-sink

Save and exit (ctrl-x, y, enter).

Then

sudo reboot

This make our Bluetooth more or less ready. Now we will pair and connect to our Raspberry Pi.

Step 3: Pairing, Connecting and Trusting.

We have now laid out the basics to make a Bluetooth Speaker. But we still need to be able to connect to play music through it.

First, enter

sudo bluetoothctl

then

power on

then

scan on

something like this should come up

pi@raspberrypi:~ $ sudo bluetoothctl
Agent registered
[bluetooth]# power on
Changing power on succeeded
[bluetooth]# scan on
Discovery started
[CHG] Controller B8:27:EB:A2:FD:3C Discovering: yes
[NEW] Device 60:D8:19:C0:2E:41 FARTHINGSLAPTOP
[NEW] Device A4:E4:B8:59:BE:8D A4-E4-B8-59-BE-8D
[CHG] Device A4:E4:B8:59:BE:8D Name: APR-BLACKBERRY
[CHG] Device A4:E4:B8:59:BE:8D Alias: APR-BLACKBERRY
[CHG] Device A4:E4:B8:59:BE:8D TxPower: 0
[CHG] Device A4:E4:B8:59:BE:8D UUIDs: 00001800-0000-1000-8000-00805f9b34fb
[CHG] Device A4:E4:B8:59:BE:8D UUIDs: 00001801-0000-1000-8000-00805f9b34fb
[CHG] Device A4:E4:B8:59:BE:8D UUIDs: 00001200-0000-1000-8000-00805f9b34fb
[CHG] Device A4:E4:B8:59:BE:8D UUIDs: 0000113b-0000-1000-8000-00805f9b34fb
[CHG] Device A4:E4:B8:59:BE:8D UUIDs: 00001124-0000-1000-8000-00805f9b34fb
[CHG] Device A4:E4:B8:59:BE:8D UUIDs: 0000110e-0000-1000-8000-00805f9b34fb
[CHG] Device A4:E4:B8:59:BE:8D UUIDs: 0000110f-0000-1000-8000-00805f9b34fb
[CHG] Device A4:E4:B8:59:BE:8D UUIDs: 0000110c-0000-1000-8000-00805f9b34fb
[CHG] Device A4:E4:B8:59:BE:8D UUIDs: 0000110a-0000-1000-8000-00805f9b34fb
[CHG] Device A4:E4:B8:59:BE:8D UUIDs: 0000111f-0000-1000-8000-00805f9b34fb
[CHG] Device A4:E4:B8:59:BE:8D UUIDs: 00001203-0000-1000-8000-00805f9b34fb
[CHG] Device A4:E4:B8:59:BE:8D UUIDs: 00001116-0000-1000-8000-00805f9b34fb
[CHG] Device A4:E4:B8:59:BE:8D UUIDs: 0000112f-0000-1000-8000-00805f9b34fb
[CHG] Device A4:E4:B8:59:BE:8D UUIDs: 00001105-0000-1000-8000-00805f9b34fb
[CHG] Device A4:E4:B8:59:BE:8D UUIDs: 00001132-0000-1000-8000-00805f9b34fb
[CHG] Device 60:D8:19:C0:2E:41 RSSI: -66
[bluetooth]#

in this example we will be pairing and connecting to the device called FarthingsLaptop.

So (replace the mac address with the device you will be connecting to).

pair 60:D8:19:C0:2E:41
[bluetooth]# pair 60:D8:19:C0:2E:41
Attempting to pair with 60:D8:19:C0:2E:41
[CHG] Device 60:D8:19:C0:2E:41 Connected: yes
Request confirmation
[agent] Confirm passkey 478737 (yes/no): yes
[CHG] Device 60:D8:19:C0:2E:41 UUIDs: 00000002-0000-1000-8000-0002ee000002
[CHG] Device 60:D8:19:C0:2E:41 UUIDs: 00001000-0000-1000-8000-00805f9b34fb
[CHG] Device 60:D8:19:C0:2E:41 UUIDs: 00001104-0000-1000-8000-00805f9b34fb
[CHG] Device 60:D8:19:C0:2E:41 UUIDs: 00001105-0000-1000-8000-00805f9b34fb
[CHG] Device 60:D8:19:C0:2E:41 UUIDs: 00001106-0000-1000-8000-00805f9b34fb
[CHG] Device 60:D8:19:C0:2E:41 UUIDs: 00001107-0000-1000-8000-00805f9b34fb
[CHG] Device 60:D8:19:C0:2E:41 UUIDs: 0000110a-0000-1000-8000-00805f9b34fb
[CHG] Device 60:D8:19:C0:2E:41 UUIDs: 0000110b-0000-1000-8000-00805f9b34fb
[CHG] Device 60:D8:19:C0:2E:41 UUIDs: 0000110c-0000-1000-8000-00805f9b34fb
[CHG] Device 60:D8:19:C0:2E:41 UUIDs: 0000110e-0000-1000-8000-00805f9b34fb
[CHG] Device 60:D8:19:C0:2E:41 UUIDs: 00001112-0000-1000-8000-00805f9b34fb
[CHG] Device 60:D8:19:C0:2E:41 UUIDs: 00001115-0000-1000-8000-00805f9b34fb
[CHG] Device 60:D8:19:C0:2E:41 UUIDs: 0000111b-0000-1000-8000-00805f9b34fb
[CHG] Device 60:D8:19:C0:2E:41 UUIDs: 0000111e-0000-1000-8000-00805f9b34fb
[CHG] Device 60:D8:19:C0:2E:41 UUIDs: 0000111f-0000-1000-8000-00805f9b34fb
[CHG] Device 60:D8:19:C0:2E:41 UUIDs: 0000112f-0000-1000-8000-00805f9b34fb
[CHG] Device 60:D8:19:C0:2E:41 UUIDs: 00001304-0000-1000-8000-00805f9b34fb
[CHG] Device 60:D8:19:C0:2E:41 ServicesResolved: yes
[CHG] Device 60:D8:19:C0:2E:41 Paired: yes
Pairing successful

Then we can connect to the device.

connect 60:D8:19:C0:2E:41

Successful connection

[bluetooth]# connect 60:D8:19:C0:2E:41
Attempting to connect to 60:D8:19:C0:2E:41
[CHG] Device 60:D8:19:C0:2E:41 Connected: yes
Connection successful
[CHG] Device 60:D8:19:C0:2E:41 ServicesResolved: yes
[CHG] Device 60:D8:19:C0:2E:41 RSSI: -59

Next we will trust the device so we can connect to it without confirmation.

trust 60:D8:19:C0:2E:41
[bluetooth]# trust 60:D8:19:C0:2E:41
[CHG] Device 60:D8:19:C0:2E:41 Trusted: yes
Changing 60:D8:19:C0:2E:41 trust succeeded

At this point you should be connected but you will notice that if you play music, it does not play through the speaker connected to the Raspberry Pi. That is what we will do on the next step.

Step 4: Playing Music.

By now you should be connected to your Raspberry Pi, but no music is playing through the speaker.

You can fix this by the command

bluealsa-aplay 00:00:00:00:00:00

Now you should hear sound coming through you're speaker when you play media, but you do not want to have to execute this command everytime you want to play a song, so we will make it run on boot.

Enter

sudo nano /etc/rc.local

It should come up with this.

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

exit 0
Enter
bluealsa-aplay 00:00:00:00:00:00

just before

exit 0

so it looks like this.

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

bluealsa-aplay 00:00:00:00:00:00

exit 0
Save and exit (ctrl-x, y, enter).

Then

sudo reboot

When it boots up, connect and play a song!

Now you should have a fully functional Bluetooth speaker!

If you have any questions leave a comment below and I will get round to them as soon as I can.

Thanks for looking.