DrFunk
Posts: 2
Joined: Sun Mar 10, 2019 8:08 pm

Another How to turn your Pi in a Bluetooth Speaker Tutorial

Sun Mar 10, 2019 8:42 pm

1) Introduction

Why another Rasperry Pi Bluetooth Speaker tutorial ?

Because I could not find a reliable and up-to-date source to tell me how to proceed.
Because Bluez is real pain to configure.

My purpose here is hence to present a step-by-step consistent procedure to help anybody to turn its Raspberry Pi into a headless bluetooth speaker.

What follows has been tested on a Raspberry Pi 3 Model B Rev 1.2 running Raspbian Stretch Lite at the time of writing, i.e. March 9th, 2019.

Note : I choose pulseaudio and not alsa as I need to run and mix several applications in my car audio project.

2) Configuring Bluetooth as an A2DP Sink

We'll be needing pulseaudio and it's bluetooth module.

Code: Select all

pi@raspberrypi:~ $ sudo apt-get install pulseaudio pulseaudio-module-bluetooth

For some unknown reason, we need to add our user to group lp (if anybody knows why, please tell me).

Code: Select all

pi@raspberrypi:~ $ sudo usermod -a -G lp pi
pi@raspberrypi:~ $ newgrp lp

Let's make our Pi permanently discoverable as an A2DP Sink.

Code: Select all

pi@raspberrypi:~ $ sudo nano /etc/bluetooth/main.conf

And add / uncomment / change

Code: Select all

...
Class = 0x41C
...
DiscoverableTimeout = 0
...

Code: Select all

pi@raspberrypi:~ $ sudo systemctl restart bluetooth

Code: Select all

pi@raspberrypi:~ $ bluetoothctl
[NEW] Controller XX:XX:XX:XX:XX:XX raspberrypi [default]
[bluetooth]# power on
Changing power on succeeded
[bluetooth]# discoverable on
Changing discoverable on succeeded
[CHG] Controller XX:XX:XX:XX:XX:XX Discoverable: yes
[bluetooth]# pairable on
Changing pairable on succeeded
[bluetooth]# agent on
Agent registered
[bluetooth]# default-agent
Default agent request successful
[bluetooth]# quit
Agent unregistered
[DEL] Controller XX:XX:XX:XX:XX:XX raspberrypi [default]

We need to start pulseausio manually now.

Code: Select all

pi@raspberrypi:~ $ pulseaudio --start

And now let's check if everything is OK.

Code: Select all

pi@raspberrypi:~ $ sudo systemctl status bluetooth
● bluetooth.service - Bluetooth service
   Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2019-03-09 21:55:45 CET; 31s ago
     Docs: man:bluetoothd(8)
 Main PID: 2296 (bluetoothd)
   Status: "Running"
   CGroup: /system.slice/bluetooth.service
           └─2296 /usr/lib/bluetooth/bluetoothd

Mar 09 21:55:45 raspberrypi systemd[1]: Starting Bluetooth service...
Mar 09 21:55:45 raspberrypi bluetoothd[2296]: Bluetooth daemon 5.43
Mar 09 21:55:45 raspberrypi systemd[1]: Started Bluetooth service.
Mar 09 21:55:45 raspberrypi bluetoothd[2296]: Starting SDP server
Mar 09 21:55:45 raspberrypi bluetoothd[2296]: Bluetooth management interface 1.14 initialized
Mar 09 21:55:45 raspberrypi bluetoothd[2296]: Failed to obtain handles for "Service Changed" characteristic
Mar 09 21:55:45 raspberrypi bluetoothd[2296]: Sap driver initialization failed.
Mar 09 21:55:45 raspberrypi bluetoothd[2296]: sap-server: Operation not permitted (1)
Mar 09 21:56:14 raspberrypi bluetoothd[2296]: Endpoint registered: sender=:1.30 path=/MediaEndpoint/A2DPSource
Mar 09 21:56:14 raspberrypi bluetoothd[2296]: Endpoint registered: sender=:1.30 path=/MediaEndpoint/A2DPSink

And it is ! Bluetooth is up and running and A2DP is registered.

At this stage, you should be able to see you Pi as an A2DP Source/Sink from your mobile bluetooth menu.

Let's go for a quick test. Try to connect to your Pi. You should be able to pair but you can't connect. Your device must be trusted first. Let's do it manually for the purpose of a quick test.

Code: Select all

pi@raspberrypi:~ $ bluetoothctl
[NEW] Controller XX:XX:XX:XX:XX:XX raspberrypi [default]
[NEW] Device YY:YY:YY:YY:YY:YY <your smartphone>
[bluetooth]# trust YY:YY:YY:YY:YY:YY
Changing YY:YY:YY:YY:YY:YY trust succeeded
[bluetooth]# quit
[DEL] Controller XX:XX:XX:XX:XX:XX raspberrypi [default]

Now you should be able to connect. And play Music !!!

3) Starting pulseaudio on boot

Your system won't be seen as an A2DP capable device until pulseaudio is launched. Let's start pulseaudio on boot.

Code: Select all

pi@raspberrypi:~ $ systemctl --user enable pulseaudio
Created symlink /home/pi/.config/systemd/user/default.target.wants/pulseaudio.service → /usr/lib/systemd/user/pulseaudio.service.
Created symlink /home/pi/.config/systemd/user/sockets.target.wants/pulseaudio.socket → /usr/lib/systemd/user/pulseaudio.socket.

Code: Select all

pi@raspberrypi:~ $ sudo raspi-config

And activate autologin for user "pi"

Code: Select all

┌─────────┤ Raspberry Pi Software Configuration Tool (raspi-config) ├──────────┐
│                                                                              │
│    1 Change User Password           Change password for the current u        │
│    2 Network Options                Configure network settings               │
│    3 Boot Options                   Configure options for start-up           │
│    4 Localisation Options           Set up language and regional sett        │
│    5 Interfacing Options            Configure connections to peripher        │
│    6 Overclock                      Configure overclocking for your P        │
│    7 Advanced Options               Configure advanced settings              │
│    8 Update                         Update this tool to the latest ve        │
│    9 About raspi-config             Information about this configurat        │
│                                                                              │
│                                                                              │
│                                                                              │
│                     <Select>                     <Finish>                    │
│                                                                              │
└──────────────────────────────────────────────────────────────────────────────┘

Select 3 Boot Options

Code: Select all

┌─────────┤ Raspberry Pi Software Configuration Tool (raspi-config) ├──────────┐
│                                                                              │
│    B1 Desktop / CLI                 Choose whether to boot into a des        │
│    B2 Wait for Network at Boot      Choose whether to wait for networ        │
│    B3 Splash Screen                 Choose graphical splash screen or        │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                     <Select>                     <Back>                      │
│                                                                              │
└──────────────────────────────────────────────────────────────────────────────┘

Select B1 Desktop / CLI

Code: Select all

┌─────────┤ Raspberry Pi Software Configuration Tool (raspi-config) ├──────────┐
│                                                                              │
│    B1 Console           Text console, requiring user to login                │
│    B2 Console Autologin Text console, automatically logged in as 'pi' user   │
│    B3 Desktop           Desktop GUI, requiring user to login                 │
│    B4 Desktop Autologin Desktop GUI, automatically logged in as 'pi' user    │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                     <Ok>                         <Cancel>                    │
│                                                                              │
└──────────────────────────────────────────────────────────────────────────────┘

And choose B2 Console Autologin

Close raspi-config and reboot

At this stage, on your mobile, you can pair your Pi as a bluetooth audio speaker BUT you cannot connect if your mobile hasn't been trusted first.

4) Auto pairing / trusting / no PIN

Let's configure our Pi for bluetooth autopairing / trusting.

Code: Select all

pi@raspberrypi:~ $ sudo apt-get install bluez-tools

The bluez-tools package comes with the bt-agent, bt-device, bt-adapter and bt-network commands, which are quite handy. I would recommend to have a look at their respective man pages.

Code: Select all

pi@raspberrypi:~ $ sudo nano /etc/systemd/system/bt-agent.service

Code: Select all

[Unit]
Description=Bluetooth Auth Agent
After=bluetooth.service
PartOf=bluetooth.service

[Service]
Type=simple
ExecStart=/usr/bin/bt-agent -c NoInputNoOutput

[Install]
WantedBy=bluetooth.target

Code: Select all

pi@raspberrypi:~ $ sudo systemctl enable bt-agent
Created symlink /etc/systemd/system/bluetooth.target.wants/bt-agent.service → /etc/systemd/system/bt-agent.service.

pi@raspberrypi:~ $ sudo systemctl start bt-agent
pi@raspberrypi:~ $ sudo systemctl status bt-agent
● bt-agent.service - Bluetooth Auth Agent
   Loaded: loaded (/etc/systemd/system/bt-agent.service; enabled; vendor preset:
   Active: active (running) since Sat 2019-02-23 11:36:23 CET; 5s ago
 Main PID: 503 (bt-agent)
   CGroup: /system.slice/bt-agent.service
           └─503 /usr/bin/bt-agent -c NoInputNoOutput

févr. 23 11:36:24 raspberrypi systemd[1]: Started Bluetooth Auth Agent.
févr. 23 11:36:24 raspberrypi bt-agent[503]: Agent registered
févr. 23 11:36:24 raspberrypi bt-agent[503]: Default agent requested

And now we're done ! Our Pi is an headless bluetooth speaker any mobile can connect, pair and play music with.

5) Not secure enough ? Let's add a (fixed) PIN

The bt-agent command allows you to add a PIN configuration file. Note that SSP must be deactivated.

Code: Select all

pi@raspberrypi:~ $ sudo nano /etc/bluetooth/pin.conf

Code: Select all

*	123456

Code: Select all

pi@raspberrypi:~ $ sudo chmod 600 /etc/bluetooth/pin.conf

Code: Select all

pi@raspberrypi:~ $ sudo nano /etc/systemd/system/bt-agent.service

Code: Select all

[Unit]
Description=Bluetooth Auth Agent
After=bluetooth.service
PartOf=bluetooth.service

[Service]
Type=simple
ExecStart=/usr/bin/bt-agent -c NoInputNoOutput -p /etc/bluetooth/pin.conf
ExecStartPost=/bin/sleep 1
ExecStartPost=/bin/hciconfig hci0 sspmode 0

[Install]
WantedBy=bluetooth.target

Code: Select all

pi@raspberrypi:~ $ sudo systemctl daemon-reload
pi@raspberrypi:~ $ sudo systemctl restart bt-agent
pi@raspberrypi:~ $ sudo systemctl status bt-agent
● bt-agent.service - Bluetooth Auth Agent
   Loaded: loaded (/etc/systemd/system/bt-agent.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2019-02-24 09:34:42 UTC; 1min 26s ago
  Process: 604 ExecStartPost=/bin/hciconfig hci0 sspmode 0 (code=exited, status=0/SUCCESS)
  Process: 590 ExecStartPost=/bin/sleep 1 (code=exited, status=0/SUCCESS)
 Main PID: 589 (bt-agent)
   CGroup: /system.slice/bt-agent.service
           └─589 /usr/bin/bt-agent -c NoInputNoOutput -p /etc/bluetooth/pin.conf

févr. 24 09:34:41 raspberrypi systemd[1]: Starting Bluetooth Auth Agent...
févr. 24 09:34:41 raspberrypi bt-agent[589]: Agent registered
févr. 24 09:34:41 raspberrypi bt-agent[589]: Default agent requested
févr. 24 09:34:42 raspberrypi systemd[1]: Started Bluetooth Auth Agent.

Done.

6) Using an external USB dongle

As the onboard bluetooth module is not so good, especially when used in conjunction with the Wi-Fi onboard module, one may want to use an external dongle instead. I would definitely recommend it.

In that case, we need to disable the onboard bluetooth module

Code: Select all

pi@raspberrypi:~ $ sudo nano /etc/modprobe.d/blacklist-bluetooth.conf

Code: Select all

blacklist btbcm
blacklist hci_uart

Code: Select all

pi@raspberrypi:~ $ sudo reboot

As simple as that.

7) Choppy sound, slitches, skips or crackling ?

This may be due to some troubles with pulseaudio resampling. Please have a look to

https://wiki.archlinux.org/index.php/Pu ... io_quality

That's all for today

LiJu09
Posts: 3
Joined: Thu Oct 11, 2018 12:16 pm

Re: Another How to turn your Pi in a Bluetooth Speaker Tutorial

Fri Jul 26, 2019 4:39 pm

Thanks for help.
I use it in my internet radio + Bluetooth speaker project.
I can send code if you are interested.

DrFunk
Posts: 2
Joined: Sun Mar 10, 2019 8:08 pm

Re: Another How to turn your Pi in a Bluetooth Speaker Tutorial

Sun Jul 28, 2019 9:03 pm

LiJu09 wrote:
Fri Jul 26, 2019 4:39 pm
Thanks for help.
I use it in my internet radio + Bluetooth speaker project.
I can send code if you are interested.
I'm glad it helped. Sure I'm interested. You can either post your code below or PM me.

granel
Posts: 1
Joined: Sun Sep 08, 2019 8:36 pm

Re: Another How to turn your Pi in a Bluetooth Speaker Tutorial

Sun Sep 08, 2019 9:05 pm

hi, I am Pi unior.
need connect bluetooth SANWU amplifier with Pi 3B/B+
All day work -- no result.
Can you help (((((

Tobias31512
Posts: 1
Joined: Mon Oct 14, 2019 5:03 pm

Re: Another How to turn your Pi in a Bluetooth Speaker Tutorial

Mon Oct 14, 2019 5:08 pm

You really helped me. I've tried it several times with different tutorials, but it didn't work. At the moment I only can use the pi with headphones, because I don't have free speakers at home. But I'm planning to install a Sound- System in my room.

Muchieman7
Posts: 1
Joined: Mon Oct 28, 2019 5:44 am

Re: Another How to turn your Pi in a Bluetooth Speaker Tutorial

Mon Oct 28, 2019 5:45 am

I got my phone to connect, but the audio plays through HDMI. I tried

Code: Select all

amixer cset numid=3 1
but it doesn't work...

Albertoo
Posts: 4
Joined: Thu Sep 19, 2019 7:56 am

Re: Another How to turn your Pi in a Bluetooth Speaker Tutorial

Sun Nov 03, 2019 8:12 pm

Hello,

very clear tutorial. i am wondering on how to make it possible to vary the volume. when i change the volume of my phone i would like to change the volume of the aux output as well. is this possible?

daskalov_st
Posts: 4
Joined: Sat Jan 23, 2016 11:11 am

Re: Another How to turn your Pi in a Bluetooth Speaker Tutorial

Mon Nov 04, 2019 10:09 am

Related to this project, i have couple of questions:


1. How i can reed Track ID, track duration, loaded playlist?
2. How i can send commands from PI to the phone, commands like play/pause, next track, volume control
3. How i can set different levels for music playing and headset. in headset mode the volume level is to high and have sound distortion.

ToothpickPi
Posts: 1
Joined: Wed Nov 13, 2019 3:50 am

Re: Another How to turn your Pi in a Bluetooth Speaker Tutorial

Wed Nov 13, 2019 3:53 am

I dont know if it's a volume thing but I'm connect and It says the music is playing on my phone but no sound through the speakers plz help if you know what the problem could be

Return to “Media centres”