[Raspberry Pi] Bluetooth Audio Headphone

Bluetooth Audio Device with Raspberry PI


Raspberry pi with Bluetooth again.
Last time I connected my DualShock 4 controller to raspberry pi.
This time, my motivation was to use Bluetooth audio headset using Raspberry pi.
I am not sure if this will give me any new project ideas.
Many people have already managed streaming Bluetooth audio from the raspberry pi.
There are a lot of creative projects available.
I hope I can make something unique by learning the Bluetooth audio with raspberry pi.

Setup
I was thinking this would be super easy and just need to work on the pulseaudio. 
This was a bad thought...
Because of this, I created unnecessary problems and took me about 3 hours to make it work.
I added "Note" at the very end of this post if you are interested in what happened.
Anyway, what I learned was that Bluetooth is a little bit tricky depending on the hardware and OS version.
I would highly recommend to double check the hardware and software versions before you start.
    This is the one of the great site to read through.

1. Check hardware version
Hardware version can be referred from cpuinfo file.
$ cat /proc/cpuinfo
When the information is displayed, you can scroll down and find the "Hardware" and "Revision" info.
In my case:
Hardware    : BCM2835
Revision      : a02082

For the relationship between the raspberry pi model and revision number, you can refer to this
I am referring to new style revision code and my hardware is model 3B.
a02082 3B 1.2 1GB Sony UK

If your model is 3B+ with latest raspbian image, I think you can simply follow this.

2. Check OS version
For OS version, you can check os-release file. 
$ cat /etc/os-release

My version is "Stretch". 


3. Testing
You can use desktop Bluetooth icons to pair and connect the device, but here I tried with bluetoothctl.
$ bluetoothctl
After running the bluetoothctl, you can turn on the agent and set it to the default.
agent on
default-agent
Now you are ready to scan the BT devices, and set the headset to pairing mode.
Then type the following command in the bluetoothctl terminal.
scan on

Once your headset is discovered, you will see something like this in the terminal.
[NEW] Device XX:XX:XX:XX:XX:XX  

"XX:XX:XX:XX:XX:XX" is your controller MAC address and type the following command with the MAC address.
Connect XX:XX:XX:XX:XX:XX

Once you see the message "Connection successful", your headset is connected, and last thing you may want to do is remember the device so that it is automatically connected next time.
Trust XX:XX:XX:XX:XX:XX

Now you are ready, and you can close the bluetoothctl by typing "quit".
Before play any audio file, make sure the Bluetooth audio device is selected as playback device.
To do so, right click the volume icon and select the device you want to send the audio.
Try play the audio file, youtube, or any others, and you will hear the audio from your headset! 

You can disconnect the controller from upper right BT icon by selecting "Headset name" > "Disconnect".

Note:
As I mentioned previously, I made big mistake by installing pulseaudio.
Somehow, I was thinking pulseaudio is the must and installed it without checking anything.
Then, I paired my Bluetooth headset without any problem.
I can see my desktop BT icon as connected to the device I want.
Audio device is selected through volume control icon.
However, I could not hear anything...

I run the command "pacmd" to  "list-cards" and what I noticed was there is no BT device indexed...
Just alsa device... hmm...

Then I killed pulseaudio process and restarted, run the "systemctl" command to check the status.
$ sudo systemctrl status bluetooth*

The error message I got was something like below. 
            bluetoothd "unable to get connect data for headset"
From this point, I spend lots of time to solve issue by looking at system, config, and other files.
Until, until I actually found that just simply remove the pulseaudio.
It is always better to read RaspberryPi.org documentation...
It says:
In Jessie, we used PulseAudio to provide support for audio over Bluetooth, but integrating this with the ALSA architecture used for other audio sources was clumsy. For Stretch, we are using the bluez-alsa package to make Bluetooth audio work with ALSA itself. PulseAudio is therefore no longer installed by default, and the volume plugin on the taskbar will no longer start and stop PulseAudio. From a user point of view, everything should still work exactly as before – the only change is that if you still wish to use PulseAudio for some other reason, you will need to install it yourself.

So for the people who see the similar errors, please try removing the pulseaudio ;)
$ sudo apt-get remove --purge pulseaudio

After removing pulseaudio, you can check the bluetooth status with "systemctl" and it should be working now.

Some people might still want to use pulseaudio.
If that is the case, this blog by Youness might help.

Good luck and thank you for reading!




[AKM Chip Booster] Audio ADC AK5704 PCB Design

Designing a PCB prototype with AK5704 is not so difficult and I show an example with my design. People who are not familiar with AK5704,...