Alright, for those who come from my previous post of bluetooth audio streaming using Raspberry Pi. I finally found a proper procedure to setup bluetooth device in Raspbian Jessie using solely command line only. This method will work on Raspbian Jessie lite version as well. Let's start.
My hardware setup is the same as shown in my previous post. The differences start on the software setup side. Assuming you already flash fresh Raspbian on your Pi (Mine is Raspbian Jessie dated on 2nd March 2017). Firstly we need to install additional packages using command below.
- sudo apt-get update && sudo apt-get upgrade
- sudo apt-get install alsa-utils bluez bluez-tools pulseaudio pulseaudio-module-bluetooth
- pactl list sources short (List available audio sources with simplified format)
- pactl list sources (List available audio sources with more details)
- 0 alsa_output.platform-soc_audio.analog-stereo.monitor module-alsa-card.c s16le 2ch 44100Hz SUSPENDED
Next, it is time for us to setup the bluetooth speaker. Follow instructions below:
- Type in bluetoothctl in terminal to enter bluetooth command line terminal. You should see the next line in terminal start with [bluetooth]# once you press enter.
- Type in power on and press enter. You should see output "Changing power on succeeded" from terminal.
- Type in agent on and press enter. You should see output "Agent registered" from terminal.
- Type in default-agent and press enter. You should see output "Default agent request successful" from terminal.
- Type in discoverable on and press enter. You need to key in this command to make sure your bluetooth dongle is visible to your bluetooth speaker. You should see output "Changing discoverable on succeeded [CHG] Controller XX:XX:XX:XX:XX:XX Discoverable: yes" from terminal. The string XX:XX:XX...XX is your bluetooth dongle's mac address.
- [Optional] If you see message from terminal state "[CHG] Controller XX:XX:XX:XX:XX:XX Discoverable: no", just repeat step 5 to make your device discoverable again.
- Turn on your bluetooth speaker and set it to pairing mode. (Follow your bluetooth speaker manual to set it to pairing mode)
- Type in scan on to start scanning for bluetooth devices. You should see a similar message "[NEW] Device YY:YY:YY:YY:YY:YY BLUE SYNC OR3". For my case my bluetooth speaker name is Blue Sync OR3. YY:YY:YY...YY is the bluetooth speaker mac address.
- Type in pair YY:YY:YY:YY:YY:YY to pair with the bluetooth speaker. You will see a bunch of message and ends with message "[CHG] Device YY:YY:YY:YY:YY:YY Paired: yes Pairing successful" if pairing successful.
- Type in trust YY:YY:YY:YY:YY:YY to enable authentication-less connection with your bluetooth speaker. You should see a message "[CHG] Device YY:YY:YY:YY:YY:YY Trusted: yes".
- Type in connect YY:YY:YY:YY:YY:YY to establish connection to bluetooth speaker. You should see output "Attempting to connect to YY:YY:YY:YY:YY:YY [CHG] Device YY:YY:YY:YY:YY:YY Connected: yes Connection successful" from terminal if connection is successful.
- Type in exit to exit from bluetooth command line terminal.
- 0 alsa_output.platform-soc_audio.analog-stereo module-alsa-card.c s16le 2ch 44100Hz SUSPENDED
- 1 bluez_sink.YY_YY_YY_YY_YY_YY module-bluez5-device.c s16le 2ch 44100Hz SUSPENDED
Now that we have almost everything ready, the trick is to redirect whatever source that is playing in Raspberry Pi and send the audio signal to your bluetooth speaker. To do this, we need to send another command to PulseAudio so that it will establish an audio link between the source and sink. Now remember in earlier section where we use one command to verify PulseAudio after its installation? That is the method we identify the audio source. We've already identify the name of the source audio to be alsa_output.platform-soc_audio.analog-stereo.monitor and its unique id is 0. We also know that our audio sink name is bluez_sink.YY_YY_YY_YY_YY_YY and its unique id is 1. Let's key in command below to establish the link between the require source and sink.
- pactl load-module module-loopback source=0 sink=1 rate=44100 adjust_time=0
Lastly we need to test whether the bluetooth speaker is playing the audio properly. Similar to my previous post, I'll use mplayer to test it, so go ahead and install mplayer by type in command sudo apt-get install mplayer2. Wait for the installation to complete.
I will recommend to set the source volume to 50% first because by default it is set to 100%. If you play a song using 100% i think it is not going to be pleasant to your ear. So go ahead type in pactl set-source-volume 0 32768 in your command line and press enter. 0 is the unique id of your source audio and 32768 represent 50% of volume. If volume = 100% the value is 65536. So if you want to set your own level of volume by percentage X, the formula is (X * 65536 / 100). You can verify the current volume level by type in command pactl list sources, Look for details under "Source#0 -> Volume:".
Once mplayer2 installation complete, simply upload one MP3 file to your Pi for testing. In command line, type in mplayer -ao help to list available audio output driver visible to mplayer. You should see one PulseAudio driver in the output. The output look like this "pulse PulseAudio audio output".
Type in command mplayer -ao pulse path_to_your_song.mp3 to start playing a song. You should be able to hear the song playing from your bluetooth speaker. It will play exactly like video shown in my previous post.
Thanks for reading! Enjoy playing with your bluetooth speaker using latest Raspbian Jessie!
Additional note:
- To remove the link between audio source and sink when you are not using it. Simply type in command pactl unload-module module-loopback.
- pacmd set-source-volume 0 32768 command works as well to set source volume.
- You can also set sink volume using command pacmd set-sink-volume 1 32768. Remember use pactl list sinks short command to find out the unique id of your desired bluetooth speaker.
Reference:
https://gist.github.com/oleq/24e09112b07464acbda1