Connect a Raspberry Pi 3 to a Bluetooth speaker (BlueALSA and A2DP)

30/04/2018 - Raspberry Pi 3, Raspbian, Stretch, BlueALSA, Bluetooth, speaker, A2DP, mpd

How to connect a Raspberry Pi 3 to a Bluetooth speaker using the A2DP profile. This uses BlueALSA rather than PulseAudio.

Setup

Make sure BlueALSA is installed to handle sending audio to Bluetooth devices.

apt install bluealsa

Add the user that will play the audio (user1234 in the following) to the lp and audio groups (you may need to log out and in again or even reboot to make this take effect):

usermod -a -G lp,audio user1234

Connect

Use the bluetoothctl command to connect to the speaker (aGLOW in the following):

$ bluetoothctl
[NEW] Controller 00:1A:7D:DA:71:13 hostname [default]
[bluetooth]# agent on
Agent registered
[bluetooth]# default-agent
Default agent request successful
[bluetooth]# power on
Changing power on succeeded
[bluetooth]# scan on
Discovery started
[CHG] Controller 00:1A:7D:DA:71:13 Discovering: yes
[NEW] Device 00:1A:7D:DA:71:13 aGLOW
[bluetooth]# scan off
Discovery stopped
[bluetooth]# pair 00:1A:7D:DA:71:13
Attempting to pair with 00:1A:7D:DA:71:13
[CHG] Device 00:1A:7D:DA:71:13 Connected: yes
[CHG] Device 00:1A:7D:DA:71:13 UUIDs: 0000320b-0000-1000-8000-00402f2c34cb
[CHG] Device 00:1A:7D:DA:71:13 UUIDs: 0000320e-0000-1000-8000-00402f2c34cb
[CHG] Device 00:1A:7D:DA:71:13 UUIDs: 0000321e-0000-1000-8000-00402f2c34cb
[CHG] Device 00:1A:7D:DA:71:13 ServicesResolved: yes
[CHG] Device 00:1A:7D:DA:71:13 Paired: yes
Pairing successful
[CHG] Device 00:1A:7D:DA:71:13 ServicesResolved: no
[CHG] Device 00:1A:7D:DA:71:13 Connected: no
[bluetooth]# trust 00:1A:7D:DA:71:13
[CHG] Device 00:1A:7D:DA:71:13 Trusted: yes
Changing 00:1A:7D:DA:71:13 trust succeeded
[bluetooth]# connect 00:1A:7D:DA:71:13
Attempting to connect to 00:1A:7D:DA:71:13
[CHG] Device 00:1A:7D:DA:71:13 Connected: yes
Connection successful
[CHG] Device 00:1A:7D:DA:71:13 ServicesResolved: yes
[aGLOW]#

Play a sound

To play a wav file:

aplay -D bluealsa:HCI=hci0,DEV=00:1A:7D:DA:71:13,PROFILE=a2dp file.wav

You can save the settings in ~/.asoundrc:

defaults.bluealsa.interface "hci0"
defaults.bluealsa.device "DEV=00:1A:7D:DA:71:13"
defaults.bluealsa.profile "a2dp"
defaults.bluealsa.delay 10000

And instead play the file:

aplay -D bluealsa file.wav

Autoconnect

I use a simple script in my crontab to automatically connect to my speaker if disconnected:

1
2
3
4
5
#!/bin/bash
address="DEV=00:1A:7D:DA:71:13"
if ! hcitool con | grep -q ${address}; then
  echo -e "power on\nconnect ${address}\nquit" | bluetoothctl > /dev/null 2>&1
fi

Music Player Daemon

I use Music Player Daemon (MPD) to output audio to the speaker. I have setup the ~/.asoundrc file in the home directory of the user that runs MPD. The audio_output section of my /etc/mpd.conf file looks like:

audio_output {
  type          "alsa"
  name          "aGLOW"
  device        "bluealsa"
  mixer_device  "bluealsa"
  mixer_control "aGLOW - A2DP"
}

To find the correct mixer_control setting:

$ amixer -D bluealsa
Simple mixer control 'aGLOW - A2DP',0
  Capabilities: pvolume pswitch
  Playback channels: Front Left - Front Right
  Limits: Playback 0 - 127
  Mono:
  Front Left: Playback 127 [100%] [on]
  Front Right: Playback 127 [100%] [on]
Simple mixer control 'aGLOW - SCO',0
  Capabilities: pvolume pvolume-joined cvolume cvolume-joined pswitch pswitch-joined cswitch cswitch-joined
  Playback channels: Mono
  Capture channels: Mono
  Limits: Playback 0 - 15 Capture 0 - 15
  Mono: Playback 15 [100%] [on] Capture 15 [100%] [on]