Setting up a Bluetooth Speaker from the command line on a Raspberry Pi

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

Install:

sudo apt update
sudo apt install pulseaudio pulseaudio-module-bluetooth

Add your pi user to the bluetooth group:

sudo usermod -G bluetooth -a pi
sudo reboot

Start a pulseaudio server:

pulseaudio --start  

Run:

bluetoothctl

At # prompt 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 audiofile.wav

Links