Blog Archive

Saturday, June 12, 2010

Stream audio in Bluetooth Headset using Dongle

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

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

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.

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).
  • There are some known issues with using this method with 32-bit Skype under 64-bit Ubuntu, which I won't go into detail here (since I don't use Skype). Refer to the Ubuntu Forums for more details about it.
  • At least on my headset, there is an ever-so-slight delay in audio sync when watching video.

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. If you have NEVER used Bluetooth on your Ubuntu setup before, then go to the next step, otherwise skip to Step 11 because you're probably already setup properly.
  4. Get into a terminal.
  5. Verify that your Bluetooth adapter is running with:
    Code:
     $ hciconfig -a 
    If you get details about hci0 listed including manufacturer's name, then your adapter is working.
    .
  1. Type in the following to edit your Bluetooth configuration file:
    Code:
     $ sudo gedit /etc/bluetooth/hcid.conf 
    This will bring up the Bluetooth configuration into the GEdit text editor.
    .
  1. Near the top of the file you will see the following:
    Code:
     # HCId options options { 	# Automatically initialize new devices 	autoinit yes;  	# Security Manager mode 	#   none - Security manager disabled 	#   auto - Use local PIN for incoming connections 	#   user - Always ask user for a PIN 	# 	security user; 
    Change the security user line to security auto
    .
  1. A few lines beneath this is a section that reads as follows:
    Code:
     	# Default PIN code for incoming connections 	passkey "1234"; 
    Change the 1234 to something else, eg: 4493. This is the pin number required for other Bluetooth devices to connect to you and it would be insecure to leave it at the default.
    .
  1. Save and exit.
  2. Now restart Bluetooth by typing in:
    Code:
     $ sudo /etc/init.d/bluetooth restart 
    When you do this, an informational bubble will appear in your task bar saying <hostname>-0 Device has been made connectable, eg: if your PC's name is "gordon", the message would say "gordon-0 Device has been made connectable".
    .
  1. Turn on your Bluetooth headset, but don't go into pairing mode just yet.
  2. In your terminal, 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.

    NOTE: If your headset does NOT appear, you probably already have it paired with something else, like your mobile phone. In this case, switch the headset to pairing mode and then run the scan again.
    .
  1. Highlight and copy the MAC address of the headset to the clipboard using your mouse and CTRL + SHIFT + C.
  2. 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.
    .
  1. In the text editor, type in the following, replacing the MAC address with the one you copied earlier (paste with CTRL + V):
    Code:
     pcm.bluetooth {   type bluetooth   device 00:33:44:DD:EE:FF   profile "auto" } 
    NOTE: Depending on your distro, you may find that the word "Bluetooth" is reserved. If, when you get to Step 23, you hear anything BUT the audio you are playing, change the "pcm.bluetooth" bit above into something else, eg: "pcm.btheadset" and try your audio again. Remember then to change every typed reference in this guide from "bluetooth" to "btheadset".
    .
  1. Save and exit.
  2. Now type in:
    Code:
     $ sudo hciconfig hci0 voice 0x0060 $ sudo modprobe snd_bt_sco $ sudo modprobe sco 
    This will enable sound on your adapter and load the modules necessary to carry bluetooth audio. Note that the two modprobe lines will only enable Bluetooth audio temporarily until you reboot. If you would like to load the drivers automatically on each boot, only add the two modprobe lines above to the end of the /etc/modules file (sudo gedit /etc/modules).
    .
  1. Now we need to tell PulseAudio that your Bluetooth headset exists:
    Code:
     $ pactl load-module module-alsa-sink device=bluetooth $ pactl load-module module-alsa-source device=bluetooth 
    Note that this enables your Bluetooth headset for PulseAudio only temporarily. To enable it permanently, create a new file using gedit ~/.pulse/default.pa and paste the two lines above into it and then save. NOTE: I've just noticed this tends to break PulseAudio because the bonding of your headset needs to have occured before issuing the above commands, or PulseAudio refuses to start upon reboot. Don't create this file (or delete/rename if you've already created it) and PulseAudio works fine again. To keep using your Bluetooth headset, move the file you created to your Desktop and make it executable. Once your Desktop has loaded and you've paired your headset, then execute the file by double-clicking on it to notify PulseAudio about the headset.
  1. We're almost ready to pair with the headset and hear some basic audio, but before we do that, do a right-mouse click on the Bluetooth icon in your system tray and choose "Preferences".
  2. Now click on the Services tab and ensure Audio Service is enabled. If not, check the box and then close the window.
  3. Now type in:
    Code:
     $ sudo cat /proc/asound/cards 
    ...and you should see output similar to the following:
    Code:
     0 [Intel          ]: HDA-Intel - HDA Intel                       HDA Intel at 0xea400000 irq 22  1 [Headset        ]: Bluetooth SCO - BT Headset                       BT Headset 1 
    This shows us that the system is ready to use the Bluetooth headset as device 1 (but we haven't paired it yet, so technically it won't be able to use it just yet).
    .
  1. The best way to trigger a pairing is to provoke the unit into a response as the normal methods don't always work. Switch your headset into pairing mode (refer to your headset's manual).
  2. While the headset it in pairing mode, type in the following:
    Code:
     $ aplay -D bluetooth -f s16_le /usr/share/sounds/login.wav 
    This will attempt direct communication with your headset, and within a second or so, an information bubble will appear under the Bluetooth icon in the system tray asking you for the PIN number to access the headset. Click on the button to enter the PIN and then type it in. For most headsets, the PIN is 0000, but refer to your headset's manual.

    Ubuntu should soon after confirm that it has "bonded" with the headset and you should suddenly hear the familiar Ubuntu login sound play through your headset! Hooray! We have 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.
    .
  1. The PulseAudio Server is already installed by default in Ubuntu Hardy, 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.

  1. Once installed, go to the System->Preferences->Sound. The Sound prefs window will appear.
  2. Under the Devices tab, change the "Sound playback" option for Sound Events, Music and Movies, and Audio Conferencing from "Autodetect" to "PulseAudio Sound Server".
  3. Click Close.
  4. Play something, be it an MP3 or video to ensure that your audio still works in general through your speakers. This confirms PulseAudio is working in general.
  5. Now go to Applications->Sound & Video->PulseAudio Device Chooser. This will add a black microphone jack icon to your system tray.
  6. Do a left-click on the jack icon and a menu appears. In this menu, choose "Manager". A new window appears.
  7. 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.
  8. Click on the Devices tab. Under "Sinks" you should see an entry for "alsa_output.bluetooth". This is picked up directly from your .asoundrc file.
  9. Now go to the Sample Cache tab. At the bottom is a "Playback on" drop-down. Choose Bluetooth from this list and click on the Play button. You should hear the Ubuntu login 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).
  10. Close the PulseAudio Manager.
  11. Do another left-click on the mic jack icon in your system tray.
  12. Go to "Default Sink" and then choose "Other" from the sub-menu. A window appears.
  13. In this window, type in "alsa_output.bluetooth" and click OK.
  14. Play a sound from somewhere, eg: MP3 or movie in Totem. You should now hear your audio coming through your Bluetooth headset!
  15. 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.
  16. 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.
  17. Enjoy!

0 comments:

Post a Comment

Followers