You'll need to discover the Bluetooth ID of your speaker. In my case, it was 0A:EC:12:01:02:A9. Adjust the scripts below accordingly.
Initial setup:
apt-get update
apt-get install -y pulseaudio pulseaudio-module-bluetooth
gpasswd -a root pulse-access
Startup 'bluetoothctl' while the speaker is in discovery mode and do:
scan on
agent on
default-agent
pair 0A:EC:12:01:02:A9
connect 0A:EC:12:01:02:A9
trust 0A:EC:12:01:02:A9
Replace the last line in /etc/dbus-1/system.conf with:
<policy user="root">
<allow own="org.pulseaudio.Server"/>
<allow send_destination="org.bluez"/>
<allow send_interface="org.bluez.Manager"/>
</policy>
<policy user="pulse">
<allow own="org.pulseaudio.Server"/>
<allow send_destination="org.bluez"/>
<allow send_interface="org.bluez.Manager"/>
</policy>
<policy context="default">
<deny own="org.pulseaudio.Server"/>
<deny send_destination="org.bluez"/>
<deny send_interface="org.bluez.Manager"/>
</policy>
</busconfig>
Then reboot.
After each boot:
# Load the PulseAudio audio manager and load Bluetooth modules
LANG=C pulseaudio -vvvv --log-time=1 -D --system --disallow-exit --disable-shm
pactl load-module module-bluetooth-policy
pactl load-module module-bluetooth-discover
# Trigger the Bluetooth connection
echo connect 0A:EC:12:01:02:A9 | bluetoothctl
# Set the Bluetooth speaker to be the default output
pactl set-default-sink bluez_sink.0A_EC_12_01_02_A9
# Play a sound
aplay /usr/share/sounds/alsa/Front_Center.wav
Useful https://github.com/ev3dev/ev3dev.github.io/pull/24/files/50787e9fae767f4a8e5e1748c5bb70b40eb9f259#diff-254568253028136165da48a17fccd79f