Configuring Linux for a Bluetooth stereo headset

Blue Sound


We'll show you how to set up support for a Bluetooth stereo headset.

By Marcel Hilzinger

Herbert Matern, Fotolia

Many Linux users enjoy listening to music through audio players such as XMMS, Amarok, and Kaffeine. These players typically output sound to speakers or headphones attached directly to the computer. If you prefer the headphone option, and if you are the kind of listener who needs room to dance, you might want to consider using a wireless Bluetooth headset.

Low-fidelity wireless headsets for two-way radios and telephones have been on the market for years, but Bluetooth-based wireless stereo is a more recent phenomenon. Many operating systems do not provide built-in support for Bluetooth stereo, relying instead on third-party tools. Linux, on the other hand, really does let you directly configure your system to support a Bluetooth stereo headset.

This article is based on the excellent HOWTO for Ubuntu [1], although the instructions will work with any distribution that has a recent kernel and a Bluetooth stack. For more detailed information, see the Bluetooth ALSA project page [2].

Mono or Stereo

Bluetooth supports two profiles for transmitting audio signals: Synchronous Connection Oriented (SCO) and Advanced Audio Distribution Profile (A2DP). The SCO profile defines a bidirectional connection with fast response (low latency) for voice transmissions. SCO typically uses mono sound quality. The SCO profile lets users send and receive sound at the same time. In contrast to this, the A2DP profile is designed for high-quality stereo sound. To achieve this higher quality, A2DP uses a unidirectional connection with high latency (slow response).

A Bluetooth headset might support only SCO, only A2DP, or both profiles. Single-ear headsets of the type common for mobile telephony typically support just the SCO profile, whereas stereo headsets often support both. In our lab, I tested the BT620s stereo headset by Jabra and the Wireless Headphones for PC by Logitech (Figure 1). The Jabra device supports both SCO and A2DP. The LED flash sequence tells you which mode the headset is working in. The Logitech headset only supports A2DP. In fact, the Logitech headset is not officially a Bluetooth device: Logitech sells it with a USB dongle as a normal wireless solution, although the set does have Bluetooth capabilities.

Figure 1: The Bluetooth headsets by Jabra (left) and Logitech (right) both support stereo sound. The Jabra headset also supports the mono SCO profile.

This article will be concentrating on stereo output via the a2dpd daemon. You can refer to the box called "The Old Method" for instructions on the more complicated setup for a SCO connection. This can be interesting if you would like to use a headset for phoning (via Skype, for example).

For the A2DP daemon, you need the sbc library and the Bluetooth-ALSA tool btsco. See the box titled "Downloading Code from CVS" for details.

After unpacking or downloading the source code, change to the sbc directory and enter the following commands:

./bootstrap
./configure --prefix=/usr
make
sudo make install

Depending on your distribution, you will need to enter either the root password (SUSE, Fedora), or your user password (Ubuntu) to install with a sudo command. When you are done, change directory to plugz and enter the same commands here, adding the --sysconfdir=/etc option to the ./configure command:

./bootstrap
./configure --prefix=/usr --sysconfdir=/etc
make
sudo make install

This completes the installation. The next step is to configure the A2DP daemon.

The Old Method

If the approach that uses the A2DP daemon does not work properly, you might want to try the old method based on the snd-bt-sco kernel module. This said, I didn't experience a single case in which snd-bt-sco worked but the A2DP daemon didn't.

To create the snd-bt-sco.ko kernel module, download the CVS code for btsco and sbc, build and install as described above, then change directory to btsco/kernel and run the make command. Become root, and copy the module to /lib/modules/Kernelversion/kernel/drivers/bluetooth. Typing uname -a gives you your Kernelversion.

If all of this works, you can load the module by typing modprobe snd-bt-sco. The next step is to associate with the Bluetooth headset. To do so, you need to make sure the headset is in pairing mode. Enter the command line:

btsco -r -v <I>Bt-address<I>

Making sure to replace Bt-address with the Bluetooth address for your headset. If you see the error message

btsco v0.42
Error: control open (hw:1): No such device
Error: Can't find device. Bail

either your headset is not supported by the module, or the kernel module might not be loaded. If everything works out, you should see the following message:

btsco v0.42
Device is 1:0
Voice setting: 0x0060
RFCOMM channel 1 connected
Using interface hci0

Now, pop up a second console and enter the following command to start the output:

mpg123 --au - <I>file.mp3<I> | a2play <I>Bt-address<I>

For playback, you need to replace file.mp3 with the name of any MP3 file and Bt-address with the Bluetooth address for your headset. If you then see the messages

Listen thread running
read 2C00C0 2873 times / sec.
read 30ABC0 1197 times / sec.
read 3061C0 1198 times / sec.
read 2FF0C0 1120 times / sec.
read 3052C0 1154 times / sec.

you can be sure that playback is running. The kernel module approach has a couple of major disadvantages compared with the A2DP daemon. For example, to play a track, you need to enter commands at the command line - playlists are not supported - and whenever you update the kernel, you will need to install a new module. In addition, playback did not work at all on the Logitech headset because it does not support SCO.

Downloading Code from CVS

After logging in, you're prompted to enter your password. Just press Enter.

cvs -d:pserver:anonymous@sbc.cvs.sourceforge.net:/cvsroot/sbc login
cvs -d:pserver:anonymous@sbc.cvs.sourceforge.net:/cvsroot/sbc co sbc
cvs -d:pserver:anonymous@bluetooth-alsa.cvs.sourceforge.net:/cvsroot/bluetooth-alsa login
cvs -d:pserver:anonymous@bluetooth-alsa.cvs.sourceforge.net:/cvsroot/bluetooth-alsa co plugz

Configuration Files

For playback, you need to tell the daemon which headset to use, and you need a .asoundrc file to create a matching ALSA device. If you do not have a .asoundrc file in your home directory, create a new file like so:

pcm.a2dpd {
       type a2dpd
}

If the file already exists, add the three preceding lines at the end of the file. To configure the A2DP daemon, copy the sample.a2dprc file called .a2dprc from the plugz/also-plugins directory to your home directory. Note that both files start with a dot. Then open the .a2dprc file, remove the pound sign (#) in line 48 to enable the entry #address=00:08:F4:30:07:64, and replace the Bluetooth address in this entry with the address for your dongle. To find the address, refer to the next section.

Bluetooth Basics

To play music on your headset, you need to connect it to your PC. To allow this to happen, you need to make sure your headset is visible. If you have a Jabra headset, press the pads on both earphones for about five seconds. You can tell that the headset is in pairing mode because the LEDs will alternately flash red, blue, and green.

If you have the Logitech headset, hold down the button at the center of the left earphone until the LED starts to flash quickly. Now enter hcitool scan at the command line, and you should see the Bluetooth address and name of the headsets - for example:

marcel@kim:~> hcitool scan
Scanning ...
00:13:17:70:44:5A Jabra BT620s

Now enter this address in the .a2dprc file in your home directory. If the command fails to find a Bluetooth device, either your computer's Bluetooth dongle is not working or the headset is not in pairing mode. To find out whether Linux has detected the dongle, give the hcitool dev command. It should display the Bluetooth address for the dongle.

Let's Rock

If you have created the two configuration files for the ALSA configuration and the A2DP daemon, you can launch the daemon at the command line by typing a2dpd. You don't need to be root to do this. The following two lines should appear as output:

avrcp: Accepting incoming connection
avdtp: Accepting incoming connection

If you see an error message stating Cannot open /dev/input/uinput: No such file or directory, you can just ignore it.

Leaving the terminal window open, launch XMMS and press Ctrl+P to open the configuration dialog. As your Output Plugin, select ALSA [libALSA.so] and then click on Configure. In the Audio device box, manually enter a2dpd (you can't select the device from the list), and then click OK twice to apply the settings.

When you start to play back in XMMS, a dialog box appears, prompting you to pair the device. The headset must be in pairing mode for this.

On SUSE Linux, the pairing dialog appears in the KDE Bluetooth framework; on Ubuntu, you will see a Gnome Bluetooth dialog box, assuming you have installed the bluez-passkey-gnome package on Edgy.

If pairing works as intended, the player should start to play back. You will then see the following message in the terminal window with the a2dpd command:

DEBUG: a2dp_transfer_raw: Bandwith: 27250 (212 kbps) 32

Instead of XMMS, you could just as easily use Amarok or Kaffeine for sound output. To do so in Amarok, select Settings | Configure Amarok | Audio output, and set the Output module to Alsa. After clicking on Apply, enter the a2dpd device in both the Mono and Stereo boxes (Figure 2).

Figure 2: To be able to play music via the A2DP daemon, you need to set up a2dpd as an ALSA device in Amarok.

In Kaffeine, select Settings | Xine Engine Parameters | Audio, click the Expert Options tab, and scroll down until you see device.alsa_front_device device used for stereo output. Instead of default, enter a2dpd here.

Pros and Cons

One of the biggest advantages of the Bluetooth approach compared with legacy wireless headsets is that you can use a Bluetooth headset to send commands to your Linux computer. For example, you can stop the player, move to the next track, or backtrack.

The .a2dprc file specifies which commands the A2DP daemon receives when you press a button. In the # AVRCP Commands to run section, you need to enter commands for XMMS. If you prefer to use Amarok, replace, for example, cmdplay=xmms --play with cmdplay=amarok -p, enter amarok --previous for cmdprev=, and so on. In the a2dpd window, you will see which commands the daemon sends.

The same file is used to set the transmission bandwidth. By default, a2dpd uses a maximum of 256Kbps. If you want to increase the bandwidth, change the value for sbcbitpool from 32 to 64. In our lab, I experienced some dropouts doing so. Assessing the value of 16 reduces the maximum bandwidth to 128Kbps; however, the sound quality is inferior at this bit rate.

Because the Bluetooth headset automatically contacts the A2DP server, you can develop a configuration to transmit the stream from one computer to another with some tinkering; this might be useful if you want to cover the whole of a large building, for example.

I wasn't entirely happy with the sound quality of the Bluetooth solution. Although the sound provided by the A2DP profile was far superior to that of the SCO connection, which is designed for voice only, some background noise clouded what was otherwise a very positive impression.

Some latency issues occurred with some versions of the daemon. For example, the daemon stopped playing back in an early version from November 7, 2006, if the headset was out of range, and then it played the notes it had missed at double or triple speed once the headset was back in range.

After applying a patch by Fréderic Dalleau, who is currently continuing the development of Bluetooth-Alsa in cooperation with Brad Migley, this problem disappeared. However, the latency problems occurred in a CVS version from November 15.

This time, the speed wowed and fluttered after short periods of dropout during playback. If you remember the days of vinyl, you can probably imagine what that sounds like. Because of the slow response of the A2DP connection, the player takes about a second to react; however, this is not a genuine issue. The solution is not currently recommended for video playback: With some video, I only heard the voices, and the soundtrack was distorted or nonexistent.

Conclusions

Now you can enjoy wireless fun with stereo sound via Bluetooth. It is worth mentioning that Linux is a pioneer in this field. Mac OS-X Bluetooth versions only support the SCO profile; Apple plans to introduce A2DP sound with its future "Leopard" OS-X release.

Windows XP is also behind on tools for playing stereo sound with Bluetooth. Vista needs to resort to a third-party vendor for an A2DP profile.

INFO
[1] Ubuntu Bluetooth Audio HOWTO: https://help.ubuntu.com/community/BluetoothAudio
[2] Bluetooth ALSA Project homepage: http://bluetooth-alsa.sourceforge.net