Sign In

    Enjoy FOSSwire's content? Have it delivered! Subscribe

    PulseAudio Bluez

    Some time ago, I’ve shown different methods of using scripts and configuration files to get Bluetooth A2DP audio working properly. But that’s a problem: it requires setup. Ideally this should all happen automatically right when the headphones are paired.

    In the latest releases of PulseAudio and Bluez, I found this to be the case. It works so well that I figured I’d make a screencast out of it just to show you. The application used in the video is pavucontrol, the volume controls for PulseAudio.

    (And a sidenote: this video was edited using PiTiVi, which just made a new release. I highly recommend you check it out.)


    Better Bluetooth Audio

    Earlier this year, I showed you how to get high-quality A2DP audio streaming to your Bluetooth headphones. And while that method of connecting your headphones to a music player does work fine, it really only works with media players and anything else set up to specifically play to your headset. What if you want to watch a Flash video with A2DP audio? How about play a game of Nexuiz through the headset? Well, with that implementation, you can't.

    And next week, with the release of Ubuntu 8.10, that method will not even work anymore due to a new incompatible version of Bluez. But on the flipside, we really don't even need to do that anymore.

    Most distributions include a somewhat recent version of PulseAudio. Love it or hate it, it becomes an excellent tool for the job. Using PulseAudio sinks (outputs), we are able to create a new device visible for Pulse to play to. This means not only your music player, but also your games, websites, and everything else, can play through your wireless headset.

    Note: If you used any of the instructions from the previous article, undo them entirely. First make sure your GStreamer output is set back to speaker mode if you used it. Then, delete the .a2dp folder in your home directory. Finally, remove the A2DP Connection script from System > Preferences > Sessions. Leaving anything laying around could interfere with the new process.

    Also note that these are merely some things to try out. They are by no means complete; real, full Bluetooth support will come in a later PulseAudio release. PulseAudio 0.9.13 includes the start of this work, but it isn't too use-friendly at the moment.

    Let's get started. Open up the file .asoundrc in your home directory (create it if you can't find it), and change its contents to something like this:
    pcm.bluetooth {
    type bluetooth
    device "00:00:00:00:00:00"
    }

    Replace the device line with the MAC address of your headphones. To find the MAC address, put the headset in pairing mode, and run hcitool scan. Also, if you headphones aren't already paired with your computer, do that now using the Bluetooth applet in the notification area.

    Save that file and log out and back in again to make ALSA re-read the configuration. Now we can set up PulseAudio with a simple one-liner:

    pactl load-module module-alsa-sink device="bluetooth"

    Be sure your headphones are on. When you see a number appear on the command line, they are ready. Pulse will automatically turn on and off the headphones as needed when it wants to play sound. Caveat: You cannot set this to run at startup, as it will sometimes fail. You can, however, add it as a panel launcher and enable your headphones with a single click.

    Now go ahead and open up Rhythmbox or another music player (or something else you know uses PulseAudio) and play a track. If you don't get any audio, check to make sure your headphones are on and paired properly, and try the above command again. If the audio still comes out of your speakers, then you will want to re-route the audio.

    To do this, install the pavucontrol (PulseAudio Volume Control) application, which may be present on some distributions already. Open it up, and go to the Output Devices tab. Find your headset in the list (labeled "bluetooth") and click the down arrow on the right. Check Default, and try playing audio again. If it still comes out of the speakers, then Pulse is remembering previous settings. Leave the music playing, and go to the Playback tab. Click the down arrow next to the active stream (probably labeled as your music player and current song) and select Move To and then "bluetooth". Wait a moment, and the audio should then route to the headphones. From that point on, Pulse should remember that it should always play to your headphones for that application if they are available.

    Feel free to experiment with different setups to find out what works best for you. If things don't work out the first time, simply keep trying. Share your findings in the comments below.

    Bonus tip: AVRCP


    Just like in the last article, you can enable AVRCP (play/next button) support by adding uinput to the end of /etc/modules. Reboot and your headphones should work!


    A2DP (Stereo Bluetooth Headset) on Linux

    Note: These instructions will eventually be obsolete. This will not work with Bluez 4 or above, included with Ubuntu 8.10+. PulseAudio is also set to get automatic A2DP configuration through a Google SoC project very soon.

    Wires are for suckers. In this day and age, wireless headphones are where it's at. While we may not be able to drop the power cord for the time being, we can at least get rid of annoying, east-to-tangle headphone wires by using a Bluetooth headset.

    But no, you say, Bluetooth audio sounds horrible. Well my friend, you were probably using a low-quality profile such as HSP. If you want full-quality stereo profile, you want A2DP.

    The first requirement is a Bluetooth adapter on your PC or laptop. The second is a Bluetooth headset that supports the A2DP profile, such as the Jabra BT60s headphones. Most earbud headsets don't support A2DP, so you're out of luck if you own one. Finally, you'll need the BlueZ Utils package installed.

    Now we're on to the actual setup. A lot of A2DP tutorials out there require you to install some custom system service or do some weird voodoo dance to get them to work. The reference I have found so far has worked every time for me. Most of this is simplified instructions from the BlueZ wiki, so if you are looking for additional setups I suggest you look there after trying this.

    Feeling lazy and want me to do this all for you? Well, today is your lucky day. I've made a custom script that will automatically configure it all for you.

    a2dp.sh (1.3 KB, Shell Script)
    (Paranoid? View all of the scripts that will be installed.)

    Simply run it as a normal user and follow all of the steps:
    bash a2dp.sh

    It will automatically download the connection script and ALSA configuration file to get the job done. All of the scripts will be installed into ~/.a2dp, with an autostart file in ~/.config/autostart.

    After you run it, you probably want to use it with a media player. All you need to do is tell your music/video player to use the "bluetooth" output device. Some players, like Audacious and Amarok, let you specify this in their preferences. GStreamer-based applications, like Banshee and Rhythmbox, rely on GStreamer to output to the right device. To do this, a "toggle" script was included with the script above: ~/.a2dp/toggle.sh. To get the most use out of it, add it to your panel.

    In GNOME, right-click a blank space and select Add to Panel, then Custom Application Launcher. Fill it in with the settings below. You must use a full path to the script. To see any dialogs, you must have Zenity. It will work without them, but there will be no notification.

    To switch to and back from your Bluetooth headphones and speakers, just give it a click:

    (Again, for non-GStreamer apps, this step is not required. Just set the ALSA device to "bluetooth" in the player preferences.)

    Fire up your favorite music player and enjoy.

    AVRCP (Remote Control)


    Some headsets may have control buttons, such as pause, next, and rewind. These may work by default on some desktops, but if it doesn't, there is a good chance you don't have uinput enabled. Open the file /etc/modules with root permissions and add the line with uinput at the end. Save the file and reboot, and AVRCP should work.


    1. 1
    2. 2