Skip to content

Instantly share code, notes, and snippets.

@actuino
Last active October 26, 2023 23:54
Show Gist options
  • Star 82 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save actuino/9548329d1bba6663a63886067af5e4cb to your computer and use it in GitHub Desktop.
Save actuino/9548329d1bba6663a63886067af5e4cb to your computer and use it in GitHub Desktop.
Setting up a Bluetooth Speaker from the command line on a raspberry Pi Zero W

The setup of a bluetooth speaker on a Pi Zero W is pretty touchy.

Please get in touch via Twitter @actuino or http://www.actuino.fr/ if you've got comments or improvements to this quick draft.

First checks

  • Use a solid power source
  • check the speaker works on another hardware (android phone f.i.)
  • make sure you've updated your Raspbian, install and run rpi-update just in case.

In case of compatibility doubt

  • Use a raspbian Pixel, run sudo apt-get update then sudo apt-get dist-upgrade and reboot.
  • use the graphical widget to pair and connect your speaker. You may have to try twice or more.

What to install?

pi-bluetooth and bluez are already installed on recent Raspbian.

Install:

sudo apt-get install pulseaudio pulseaudio-module-bluetooth

(without this module, you get messages like "bluetoothd[5556]: a2dp-sink profile connect failed for 00:1D:43:6D:03:26: Protocol not available")

Extra steps

Add your pi user to the bluetooth group

sudo usermod -G bluetooth -a pi

sudo reboot

Start a pulseaudio server

pulseaudio --start

If it complains about not being able to spawn a local server, run

pax11publish -r; /usr/bin/pulseaudio --start

Pair and connect

run bluetoothctl

use "help" if you want more details about the available commands. The first time, you'll have to run the following:

  • power on
  • agent on
  • scan on
  • wait for the device to be discovered, note it's address (you can then use tab for auto-completion)
  • pair <dev>
  • trust <dev>
  • connect <dev> wait for the confirmation, then
  • quit

test : aplay something.wav

edit /etc/pulse/default.pa , add

# automatically switch to newly-connected devices
load-module module-switch-on-connect

edit /etc/bluetooth/main.conf, at the end of the file, add AutoEnable in the existing Policy section :

[Policy]
AutoEnable=true

After a reboot

You may need to:

  • pulseaudio --start

(This last step was not useful for me):

  • bluetoothctl -a then wait and quit when (auto) connected

Still have to sort out

  • Sometimes, the BT speaker disconnects itself, but it's still viewed as "connected" from the Pi. From this point, nothing will fix it exept a Pi reboot.
@james4388
Copy link

Thanks for the guide

@ritiek
Copy link

ritiek commented May 29, 2019

I wasn't able to control the volume with my phone. Following this guide fixed it for me hadess/CHIP-bluetooth-speaker#8 (comment):

I've just fixed the problem by upgrading to latest bluez version (5.43-1 at this moment) and modifying bluetooth.service.

Step 1. Upgrading BlueZ

Run:

apt-get update
apt-get install bluez-tools pulseaudio-module-bluetooth

and continue to Step 2.

Several users have reported that this is not necessary, but if you want to upgrade BlueZ to testing (unstable) version, use this instructions:
Upgrading BlueZ to testing (unstable) version

Step 2. Edit bluetooth.service

Run

nano /lib/systemd/system/bluetooth.service

and add --plugin=a2dp as an argument to bluetoothd, like

ExecStart=/usr/lib/bluetooth/bluetoothd --plugin=a2dp

Run

systemctl daemon-reload
systemctl restart bluetooth

to apply changes. Now volume control from bluetooth devices should work (tested on iPhone iOS 10.0).

Troubleshooting

If you couldn't connect to CHIP after upgrading, verify that bluetooth adapter is powered on:
Run bt-adapter --info.
If you see Powered: 0, you can re-enable it using bt-adapter --set Powered 1.

@joemaffei
Copy link

Thank you for the guide! It worked like a charm.

Copy link

ghost commented Jun 4, 2020

worked for me too. nice guide, thanks a lot!

@OttoNL
Copy link

OttoNL commented Aug 2, 2020

Couldn't get it to work sadly. Followed all the steps, Configuration all seemed to work just fine, no trouble finding or pairing to my Ikea Eneby 20. When I tried to use the aplay command to play one of my .wav files it started playing on my HiFi (via HDMI).

I made sure I didn't force audio to play through HDMI in raspi-config, but it's set to Auto. I then right-clicked the volume control in the bottom right and noticed audio was being sent to HDMI. I clicked 'Eneby 20' and I could see that it switched to that, tried again and no audio from the speaker (or my HiFi).

Rebooted the Pi a couple times, it automatically connected to the speaker, audio settings remaimed, but still no Audio. Not sure where to start troubleshooting.

By the way, when it's working should it work with any software? Could I use Chromium or a Volumio to send audio to the speaker? Because if I can only play audio from the commandline using aplay it wouldn't really fit my needs.

@okgagen
Copy link

okgagen commented Oct 28, 2020

@OttoNL
I use omxplayer to play sound files on bluetooth speaker.

=====================================

To response to this comment above, "Sometimes, the BT speaker disconnects itself, but it's still viewed as "connected" from the Pi. From this point, nothing will fix it exept a Pi reboot."

I reset my bluebooth connection with these commands when I am not able to play sound files to bluebooth speaker (even it shows connected):
$ sudo rfkill block bluetooth
$ sudo rfkill unblock bluetooth
$ sudo systemctl restart bluetooth

:-)

@nebhead
Copy link

nebhead commented Oct 31, 2020

@OttoNL I also had the same problem and wasn't able to play audio, but did have some luck by doing the following:

First I had to install the package bluealsa by doing this:
sudo apt install bluealsa

You'll also need to create and edit the following file with your device id:

nano ~/.asoundrc

Add these lines (replace the device id with your connected device):

defaults.bluealsa.interface "hci0"
defaults.bluealsa.device "00:00:00:00:00:00"
defaults.bluealsa.profile "a2dp"
defaults.bluealsa.delay 10000

Then, do a reboot, and ensure that you are connected to your device.

Test by running:
aplay -D bluealsa yourfile.wav

@TimoBoiii
Copy link

The whole setting up was without a problem except the aplay something.wav it says command not found i tried even typing Play something.wav but it did the same thing. So now i have the whole thing set up but have no clue how to actually play music on it. I would appreciate if anyone could help me with this issue. Im kinda new to raspberry pi linux and all and i don't know what to do. If somebody explained to me i would really appreciate it Thanks.

@striderssoftware
Copy link

To send USB Mic Input to the bluetooth device:
aplay < arecord --device hw:1,0 -c1 -d 0 -r 48100 -f S16_LE -V mono

@striderssoftware
Copy link

Sorry my mistake, here is the command to send USB MIC input to the connected bluetooth device:
arecord --device hw:1,0 -c1 -d 0 -r 48100 -f S16_LE -V mono | aplay

@sirhopcount
Copy link

By default the audio played over my HDMI, I "fixed" this by disabling it in /boot/config.txt

# Enable audio (loads snd_bcm2835)
#dtparam=audio=on

If anybody knows a better way of doing this please let me know.

@donundeen
Copy link

donundeen commented Nov 3, 2022

this is fantastic, and is working great for me, connecting and playing audio, if I'm logged in to the Pi. Thanks!

I've set up my pi to auto-login, so I can connect the bluetooth headphones to the pi just by turning the headphones on.

However, when I try to use a crontab (pi user) to play an audio file it still plays through the audio jack, not the bluetooth

Using mpg123 on the command-line to play an audio file WILL however play the audio file over bluetooth.

Amy thoughts? Ultimately I want a headless pi running a node app that will use mpg123 to play audio, and this app will start on boot, probably from cron.

Thanks again for these great instructions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment