HowTo: Use a Bluetooth headset with Ubuntu Jaunty

Discussion in 'Other Operating Systems' started by HyRax1, May 13, 2009.

  1. HyRax1

    HyRax1 ¡Viva la Resolutión!

    Joined:
    Jun 28, 2001
    Messages:
    7,764
    Location:
    At a desk
    At the risk of self-appointing myself tech support for this, I present an updated version of my Bluetooth headset with Ubuntu Hardy guide, this time for Ubuntu Jaunty 9.04.

    This HowTo illustrates how to pair and configure a Bluetooth headset with Ubuntu Jaunty 9.04 and redirect your music and media players to it. This guide is based on the guides at Ubuntu Forums with some modifications to be more step-explicit and thus newbie-friendly and reflects the changes to the Bluetooth stack since my original Hardy instruction.

    Purpose: To play your audio such as MP3's and movie output through a Bluetooth-connected audio headset using Ubuntu Jaunty 9.04.

    Scenario: You are too lazy to plug in a cable to your PC. You want to walk around your house without a long cable after you. You want to look cool to your mates. You want to psyche out your non-tech parents by listening to music without any audio device or cables attached to you. Problem is, very few Linux apps have direct support for directing sound to any other device.

    Solution: Redirect all audio using the PulseAudio Server on Ubuntu Jaunty.

    Prerequisites:
    • Bluetooth 1.2 compliant (or better) adapter to your PC. This can be in-built, such as on a modern notebook PC, or a USB dongle such as the one I use. You will NOT have success with Bluetooth adapters that are only compliant with the 1.0 or 1.1 specification.
    • Ubuntu Jaunty 9.04 (I'm using the 64-bit version in this example).
    • Internet access or other such access to the Ubuntu repositories to install extra software from.
    • Suitable Bluetooth headset. The unit I bought is a TDK BT100 pair of buds. Supports Bluetooth 1.2 and 2.0, does headset, handsfree A2DP and AVRCP functions (typical function set of any headset/handsfree unit these days). Though the sound quality of these things isn't that great, it was only $49 at my local PC shop.
    Pros:
    • You are cable free! You can walk around the house (as far as the range of your Bluetooth adapter goes) and listen to music/movies.
    • If your headset also has a mic, you can use that too for VOIP apps and the like, though this is not a working feature (at least for me) in Jaunty.

    Cons:
    • A Bluetooth headset is generally more expensive than a cabled one.
    • Most headset batteries only last four hours in continuous use before needing a recharge (though the TDK unit I have plugs into mini-USB to recharge and can still be used whilst recharging, which is convenient).
    • Audio quality is generally not as good as a dedicated cabled headset.
    • For those headsets that have battery-saving functions when idle, some are known to clip the start of the audio playback when turning back on (my set causes Ubuntu to wait until it's up and running before streaming the audio, thus no clipping).
    • Due to large changes in how Bluetooth works in Ubuntu between Hardy and Jaunty, you may find your Bluetooth headset might not be visible at all by the system or is seen, but refuses to pair.

    These instructions should be adaptable to other distributions.

    1. Fire up/install Ubuntu as normal.
    2. Plug in or enable your Bluetooth adapter. Your Bluetooth adapter will be automatically detected and drivers loaded - there is nothing for you to do manually here.
    3. Turn on your Bluetooth headset.
    4. Switch your headset into pairing mode (refer to your headset's manual).
    5. While the headset is in pairing mode, left click the Bluetooth icon in your system tray and choose "Setup new device" from the menu. Follow the wizard prompts to seek out and pair your headset.
    6. Once paried, open a terminal, and type in the following:
      Code:
      $ hcitool scan
      
      Your PC will now scan for local Bluetooth devices and your headset should appear in the resulting list after a few seconds (along with anyone's Bluetooth-enabled mobile phones that are in range). The output will look something like:
      Code:
      $ hcitool scan
      Scanning ...
      	00:11:22:AA:BB:CC	Nokia N95
      	00:33:44:DD:EE:FF	BT81
      $ 
      
      In this example, my PC has found my Nokia mobile phone and my Bluetooth headset and shown me the MAC addresses for both of them.
      .
    7. Highlight and copy the MAC address of the headset to the clipboard using your mouse and CTRL + SHIFT + C. In this example my headset's MAC address is "00 : 33 : 44 : DD : EE : FF". Yours will be different - copy YOUR address, not this example's address.
    8. Now type in:
      Code:
      sudo gedit ~/.asoundrc
      
      Note the period before "asoundrc". This will create a new hidden text file called .asoundrc in the root of your Home directory and open GEdit so you can add to it. The file is hidden because of the leading period.
      .
    9. In the text editor, type in the following, replacing the MAC address in the example with the one you copied earlier from YOUR output (paste with CTRL + V):
      Code:
      pcm.btheadset {
        type bluetooth
        device 00:33:44:DD:EE:FF
        profile "auto"
      }
      
      .
    10. Save and exit.
    11. Now type in:
      Code:
      $ sudo hciconfig hci0 voice 0x0060
      
      This will enable your Bluetooth adapter to carry Bluetooth audio.
      .
    12. Now we need to tell PulseAudio that your Bluetooth headset exists:
      Code:
      $ pactl load-module module-alsa-sink device=btheadset
      $ pactl load-module module-alsa-source device=btheadset
      
      Note that this enables your Bluetooth headset for PulseAudio only temporarily. When you reboot, the PulseAudio configuration for Bluetooth will be lost. For future convenience, create a bash script with the above commands in it and create a launcher on your desktop to run the commands when you double-click on the launcher icon. Due to needing to have the headset paired BEFORE you run these commands, you cannot have these commands run automatically during system startup. It will cause PulseAudio to fail.
      .
    13. Once pairing has completed, we can now test to see if we can send audio to the headset. In your terminal, type in the following:
      Code:
      $ aplay -D btheadset -f s16_le /usr/share/sounds/ubuntu/stereo/dialog-question.wav
      
      This will attempt direct communication with your headset, and within a second or so, you should suddenly hear the familiar Ubuntu "login ready" drum sound play through your headset! If you didn't head it first time, try the command a second time as there may be a delay between "activating" your headset and playing sound.

      Unfortunately only aplay will play anything through your headset. All other sounds are still coming through your speakers. Unless the application in question can redirect audio to another detected device, it will always play through the standard-out, so applications such as Totem and Rhythmbox will still output via your speakers and not give a hoot about your Bluetooth headset. To fix this, we need to make use of the PulseAudio Server which can redirect output to another device.
      .
    14. The PulseAudio Server is already installed by default in Ubuntu Jaunty, so we just need to install some tools to manipulate it. Go back to your terminal and type in the following:
      Code:
      $ sudo apt-get install paprefs paman padevchooser
      
      This will install the PulseAudio Preferences app, the PulseAudio Manager app and the PulseAudio Device Chooser app.
      .
    15. Once installed, go to Applications->Sound & Video->PulseAudio Device Chooser. This will add a black microphone jack icon to your system tray.
    16. Do a left-click on the jack icon and a menu appears. In this menu, choose "Manager". A new window appears.
    17. If it's not already connected, click on the "Connect" button to connect to your local PulseAudio server. When connected, you will see details about it listed.
    18. Click on the Devices tab. Under "Sinks" you should see an entry for "alsa_output.btheadset". This is picked up directly from your .asoundrc file.
    19. Now go to the Sample Cache tab. You are shown a list of sounds. Choose a WAV file from this list (it won't play any other format). At the bottom is a "Playback on" drop-down. Choose "alsa_output.btheadset" from this list and click on the Play button. You should hear the Ubuntu "login ready" sound through your speakers. This proves to us that PulseAudio can play through your Bluetooth headset (but this is NOT the redirection - this is just a test).
    20. Close the PulseAudio Manager.
    21. Do another left-click on the mic jack icon in your system tray.
    22. Go to "Default Sink" and then choose "Other" from the sub-menu. A window appears.
    23. In this window, type in "alsa_output.btheadset" and click OK.
    24. Play a sound from somewhere, eg: MP3 or movie in Totem. You should now hear your audio coming through your Bluetooth headset! NOTE: Existing audio streams at the time of changing the sink will continue to play through whatever they were playing through until stopped and started again.
    25. To switch back to your speakers, simply click on the mic jack icon again, choose "Default Sink" and choose "Default" from the sub-menu. The next audio stream played will go back through your speakers.
    26. To make the PulseAudio Device Chooser start automatically on startup, click on the mic jack icon again, choose Preferences from the menu and then click on "Start applet on Session Login" in the window.
    27. Enjoy! :p

    KNOWN ISSUES:
    • This does not work with Skype. Despite the "btheadset" device being listed as a Sound Device option within Skype, you will get errors when it tries to playback or record audio via the headset and it will in fact kill the PulseAudio server forcing you to restart PulseAudio or your PC to get it running again. I have not figured this one out yet.
    • You cannot have your headset auto-pair and be auto-configured with PulseAudio upon startup (yet?). You will need to pair first, then run the two "pactl" commands in step 10 manually or via a script launcher. Before you say "can't I put those commands in startup?", you cannot have these commands auto-run on startup or PulseAudio will hang or crash (because the pairing with your headset has not been established yet).
    • The Sound Recorder is unable to lock onto the headset for recording audio (in fact, it goes nuts when trying to record).
    • The second "pactl" command in Step 12 may cause unusual undesired system behaviour. Since the second command only exists to setup the microphone on your headset, if you do not have one or don't intend to use the microphone, you may omit this line.

    CONTRIBUTIONS:
    • mbarclay has contributed a nifty script that can be used to prepare PulseAudio to talk to your headset (note that you still need to pair beforehand, and then redirect all audio with PulseAudio Dev Chooser after the script is run). You can use this script with a simple launcher icon from the desktop or a panel.
    • arkonr has discovered that if you are getting "Connection refused" or "Module initialization failed" errors from the pactl commands, you can resolve this by stopping and starting (as opposed to restarting) the Bluetooth stack using the commands:
      Code:
      $ sudo /etc/init.d/bluetooth stop
      $ sudo /etc/init.d/bluetooth start
      
    • inc595 has noted that you can define a sink name in Step 12, so that at step 23 you can use a simplified name rather than the convoluted "alsa_output.btheadset" name, eg:
      Code:
      pactl load-module module-alsa-sink device=btheadset [B]sink_name=btheadset[/B]
      
      ...means you can then use the name "btheadset" as a sink name instead of "alsa_output.btheadset".
     
    Last edited: Aug 24, 2009
  2. Oblong Cheese

    Oblong Cheese Member

    Joined:
    Aug 31, 2001
    Messages:
    10,574
    Location:
    Brisbane
    Nice guide.

    Hoping you'll update the "Syncing your Nokia handset with Evolution" thread for 9.04 too. :D
     
  3. OP
    OP
    HyRax1

    HyRax1 ¡Viva la Resolutión!

    Joined:
    Jun 28, 2001
    Messages:
    7,764
    Location:
    At a desk
    Yeah, yeah, I'll get to that. :p
     
  4. Xsoldier2000

    Xsoldier2000 Member

    Joined:
    May 14, 2009
    Messages:
    5
    Mate, you are absolutely the BEST!

    I've been struggling with this for a few months and had actually been using Windows 7 for a while as I couldn't get to use my S9's.

    You are a hero in my eyes.


    Quick question for you tho....of course there's got to be one.

    If I log out/in or restart, I have to re do like the last 4 steps and then I'm good again. Do you think it's possible for me to write a script that will automate those steps?

    I've got your post saved as a document so I can just copy paste my way to bluetooth headphone bliss...but I was just wondering....

    Thanks again!
     
  5. GumbyNoTalent

    GumbyNoTalent Member

    Joined:
    Jan 8, 2003
    Messages:
    4,788
    Location:
    Briz Vegas
    :thumbup: Keep up the good work, I don't use Bluetooth, but if I did I would be very grateful that the Linux Community continues to help like always. Should link in the Why Linux suxs. as proof that the Linux Community does help those that ask intelligent questions. ;)
     
  6. OP
    OP
    HyRax1

    HyRax1 ¡Viva la Resolutión!

    Joined:
    Jun 28, 2001
    Messages:
    7,764
    Location:
    At a desk
    Er, well... ummm... haha... shucks <blush> ;)

    The only three things you have to do when you reboot is:

    1. Make sure the headset is paired again.
    2. Run the two "pactl" commands.
    3. Re-direct your audio to the headset via PulseAudio.
    You can certainly simplify the second bit by putting the two commands into a script file as follows:
    Code:
    #!/bin/bash
    echo Setting up PulseAudio for your Bluetooth headset...
    pactl load-module module-alsa-sink device=btheadset
    pactl load-module module-alsa-source device=btheadset
    echo Done!
    
    Save that somewhere appropriate, make it executable and then create a launcher icon on your desktop to run that script when you double-click on the icon, then go and redirect the audio in the PulseAudio Dev Chooser tool. I haven't yet looked into any fancy ways to control PulseAudio via the command line yet, so we may be able to script the redirection too.
     
  7. Xsoldier2000

    Xsoldier2000 Member

    Joined:
    May 14, 2009
    Messages:
    5
    Thanks again HyRax1! I will continue to try and come up with a script to reconnect fully.

    :thumbup::thumbup::thumbup:
     
  8. rugbeeprop

    rugbeeprop New Member

    Joined:
    May 17, 2009
    Messages:
    2
    Partially work

    Thanks for the tips.

    I am able to run this code after pairing.
    Code:
    pactl load-module module-alsa-sink device=btheadset
    

    However, failed running this part.
    Code:
    pactl load-module module-alsa-source device=btheadset
    
    When I ran the second part, the whole sound system just went balistic. Rhythmbox pretty much skip every single song.
     
  9. OP
    OP
    HyRax1

    HyRax1 ¡Viva la Resolutión!

    Joined:
    Jun 28, 2001
    Messages:
    7,764
    Location:
    At a desk
    The second command is not technically required - it's for setting up the Bluetooth headset's microphone as in input source. If you do not have a mic on your headset, you can safely ignore this command (it really shouldn't matter, but in your case I suspect that if you don't have a mic, this bit is probably confusing Ubuntu a bit).
     
  10. rugbeeprop

    rugbeeprop New Member

    Joined:
    May 17, 2009
    Messages:
    2

    Thanks HyRax for a quick response.

    The headset does have a mic, however, I don't really use it so I guess I can skip the second command (which is what I have been doing).
     
  11. fruityren7

    fruityren7 New Member

    Joined:
    May 17, 2009
    Messages:
    2
    Stuck on step 12

    I cant get past step 12. When I try either command, I get a short pause followed by:

    Code:
    Failure: Module initalization failed
    ([sic.] on the spelling)

    I'm on Jaunty 64 bit.

    What am I doing wrong?
     
    Last edited: May 18, 2009
  12. OP
    OP
    HyRax1

    HyRax1 ¡Viva la Resolutión!

    Joined:
    Jun 28, 2001
    Messages:
    7,764
    Location:
    At a desk
    Is your headset paired successfully? Those commands will not work if you haven't already paired your headset or if you haven't defined the ~/.asoundrc file properly.

    Also ensure that your Bluetooth adapter is protocol v1.2 compliant or higher. Older ones will NOT work.
     
  13. mbarclay

    mbarclay New Member

    Joined:
    May 19, 2009
    Messages:
    1
    Scripting pactl commands

    This totally rocks! Using A2DP from my laptop is great. Wanted to share the script I'm using to load the pulseaudio module. Since the bluetooth support is somewhat delicate, I throw in some extra checks to see if the module is loaded already, then I scan the network for the btheadphone MAC as defined in ~/.asoundrc, then finally I load the module. I use this script from the gnome panel. The sleep statements are just so the terminal window doesn't close right away and I can see what happened.

    This script assumes a single device declaration in ~/.asoundrc

    The alsa-source module is commented OUT. I don't need it with my NS-BTHDP head phones, and it usually fails to load anyway. Uncomment those lines if you have a microphone. I think you might need a sleep between the alsa-sound and alsa-source module loading, but maybe not...

    Code:
    #!/bin/bash
    
    if pactl list | grep -q btheadset ; then
        echo Module already loaded
        sleep 5
        exit 0
    fi
    
    MAC=`grep device ~/.asoundrc | awk '{print $2}'`
    if [ -z "$MAC" ]; then
        echo couldnt parse MAC from ~/.asoundrc
        sleep 5
        exit 1
    fi
    
    BTHDP=`hcitool name $MAC`
    if [ -z "$BTHDP" ]; then
        echo Cant find the device $MAC
        sleep 10
        exit 1
    else
        echo Found $BTHDP $MAC
    fi
    
    echo Loading Bluetooth ALSA sink
    pactl load-module module-alsa-sink device=btheadset
    echo
    echo Done
    sleep 5
    
    #sleep 2
    #echo Loading Bluetooth ALSA source
    #pactl load-module module-alsa-source device=btheadset
    
    Regards,
    Matt
     
  14. OP
    OP
    HyRax1

    HyRax1 ¡Viva la Resolutión!

    Joined:
    Jun 28, 2001
    Messages:
    7,764
    Location:
    At a desk
    That's an awesome contribution - thanks! :thumbup:
     
  15. fruityren7

    fruityren7 New Member

    Joined:
    May 17, 2009
    Messages:
    2
    All of the above except the last. How do I check to see if my bluetooth adapter is protocol v1.2 compliant or higher?
     
  16. OP
    OP
    HyRax1

    HyRax1 ¡Viva la Resolutión!

    Joined:
    Jun 28, 2001
    Messages:
    7,764
    Location:
    At a desk
    Not sure of a way to do it within Ubuntu, but the packaging of your adapter or looking up the model for the specs should tell you.
     
  17. Jadlak

    Jadlak Member

    Joined:
    May 25, 2002
    Messages:
    88
    Location:
    Brisbane, 4116
    Nothing more than adding my thanks to this thread. Followed these instructions for a set of Motorola HT820 stereo bluetooth headphones plugged into an eeepc901 running netbook remix 9.04 and it worked.

    My only comment was that during the 'testing' steps I had to play the sound a few times before it came out loud and clear.

    :thumbup:
     
  18. impactor

    impactor New Member

    Joined:
    Jun 5, 2009
    Messages:
    1
    Seems like a great guide but I only got as far as step 12 where I received this error:
    Code:
    ~$ pactl load-module module-alsa-sink device=btheadset
    Failure: Timeout
    Any clues?
    BTW, since this method does not work for Skype, does anyone know how can I use my headset with Skype?
     
  19. emerix

    emerix New Member

    Joined:
    Jun 9, 2009
    Messages:
    1
    Location:
    Paris, France
    Hi!
    I also have this error :
    Code:
    pactl load-module module-alsa-sink device=btheadset
    Failure: Timeout
    
    During this command my Nokia BH-102 beeps ... which might indicate that the headset is correctly accessed.

    Do anybody know how to troubleshot this ? (pactl does not seem to have a --verbose option)

    Thanks :)
     
  20. OP
    OP
    HyRax1

    HyRax1 ¡Viva la Resolutión!

    Joined:
    Jun 28, 2001
    Messages:
    7,764
    Location:
    At a desk
    Have both of you been able to successfully pair your devices before this step? Does Ubuntu acknowledge that? Does your headset acknowledge that (ie: does the pairing light(s) go off?)

    I've observed Ubuntu Jaunty's Bluetooth stack currently has this issue whereby it doesn't properly pair everything. My headset is fine, but my mouse is not. It can see it, it can try and pair with it, but it will never finish that task, or Ubuntu may acknowledge the pairing but the mouse still has its pairing light flashing away, which means something is amiss.

    If your headset is doing that, then it's not paired properly hence the timeout from the pactl command.

    We may need to get a few people to lodge more bug reports to Launchpad to get this addressed as it appears Bluetooth is beginning to take a back seat to other functions within Ubuntu atm.
     

Share This Page