Tag/tag.png

Unsupported Version
This article applies to an unsupported version of Ubuntu. More info...

Tag/tag.png

Needs Updating
This article needs updating to include the latest versions of Ubuntu. More info...

Purpose

This guide should help you redirect music, audio and media players to your Bluetooth headset.

In Ubuntu 10.10 and later

From Ubuntu 10.10 on it should be very simple:

  • Go to System -> Preferences -> Bluetooth and pair the Bluetooth device

  • Go to System -> Preferences -> Sound and select the Bluetooth device as your output device.

Autoconnecting

To autoconnect and autoactivate your bluetooth headset you can use the program "stream2ip" - download and install the .deb from Launchpad: https://launchpad.net/stream2ip

In Ubuntu Jaunty 9.04


To make this work properly all audio will have to be redirected through the PulseAudio Server

Thanks to HyRax1 on the Overclockers forum.

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. You will NOT have success with Bluetooth adapters that are only compliant with the 1.0 or 1.1 specification.

  • Ubuntu Jaunty 9.04.
  • Internet access or other such access to the Ubuntu repositories to install extra software from.
  • Suitable Bluetooth headset (before purchasing please research your choice, to ensure that it is indeed supported and works in Ubuntu)

Instructions

  1. 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.
  2. Turn on your Bluetooth headset.
  3. Switch your headset into pairing mode (refer to your headset's manual).
  4. 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.

  5. Once paried, open a terminal, and type in the following:
    • 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 any other Bluetooth-enabled devices that are in range). The output will look something like:
      $ hcitool scan
      Scanning ...
              00:11:22:AA:BB:CC       Nokia N95
              00:33:44:DD:EE:FF       BT81
      In this example, hcitool has found a Nokia mobile phone and a Bluetooth headset and is displaying 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 that case switch the headset to pairing mode and then run the scan again.

  6. Highlight and copy the MAC address of your headset.
  7. Then type in the following:
    • nano ~/.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 it with nano so you can add to it. The file is hidden because of the leading period.

  8. In the text editor, type in the following, replacing the MAC address with the one you copied earlier:
    • pcm.btheadset {
              type bluetooth
              device 00:33:44:DD:EE:FF
              profile “auto”
      }
  9. Press CTRL+X to exit nano, save the file by pressing "y" then enter
  10. Enable sound drivers by typing the following into a terminal:
    • sudo hciconfig hci0 voice 0x0060
      This will enable your Bluetooth adapter to carry Bluetooth audio.
  11. Now we need to tell PulseAudio that your Bluetooth headset exists:

    • pactl load-module module-alsa-sink device=btheadset
      pactl load-module module-alsa-source device=btheadset

      Note: 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 can NOT have these commands run automatically during system startup. It will cause PulseAudio to fail.

      Caution: The second pactl command 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.

  12. 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:
    • $ aplay -D btheadset -f s16_le /usr/share/sounds/ubuntu/stereo/dialog-question.wav
      For Ubuntu 9.10 Karmic Koala use this:
      $ aplay -D btheadset -f s16_le /usr/share/sounds/alsa/Front_Center.wav
      This will attempt direct communication with your headset. Within a second or so you should suddenly hear the familiar Ubuntu "login ready" drum sound play through your headset! If you didn't hear 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 at the moment. All other sounds such as from rhythmbox or totem 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.

      To fix this we need to make use of the PulseAudio Server which can redirect all output to another device.

      The PulseAudio Server is already installed by default in Jaunty, so we just need to install some tools to manipulate it.

  13. Go back to your terminal and type in the following:
    • $ sudo apt-get install paprefs paman padevchooser

      This will install the PulseAudio Preferences app, the PulseAudio Manager app and the PulseAudio Device Chooser app.

  14. Once installed open the PulseAudio Device Chooser by going to Applications | Sound & Video | PulseAudio Device Chooser. This will add a black microphone jack icon to your system tray.

  15. Open the PulseAudio Manager by left-clicking on the jack icon and choosing Manager.

  16. If not already connected, click on the Connect button to connect to your local PulseAudio server. When connected, you will see details about it listed.

  17. Click the Devices tab and under Sinks you should see an entry for alsa_output.btheadset. This is picked up directly from your .asoundrc file.

  18. 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 sound through your headset. This proves to us that PulseAudio can play through your Bluetooth headset (but this is NOT the redirection - this is just a test).

  19. Close the PulseAudio Manager

  20. Left-click on the mic jack icon in your system tray.
  21. Go to Default Sink and then choose Other from the sub-menu. In the new window that opens type in:

    • alsa_output.btheadset
      Click OK
  22. Test the redirection by playing an audio or video file. You should now hear sound 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.

  23. To switch back to your speakers simply click on the mic jack icon, in your system tray, choose Default Sink and then choose Default from the sub-menu. The next audio stream played will go back through your speakers.

  24. 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.

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 restart Ubuntu to get it running again.

  • 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. 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).

Possible autorun script

Create a file called autorun_bt.sh:

  • gedit autorun_bt.sh

Put the following into the file:

  •  #!/bin/bash
    echo "Enable sound drivers by typing the following into a terminal"
    clear
    gksudo hciconfig hci0 voice 0x0060
    sleep 1
    
    echo "Now we need to tell PulseAudio that your Bluetooth headset exists"
    pactl load-module module-alsa-sink device=btheadset
    sleep 8
    pactl load-module module-alsa-source device=btheadset
    sleep 2
    
    echo "Ready"
    sleep 2

Save the file and exit GEdit

Type the following to make the file executable:

  • chmod a+x autorun_bt.sh

Double click on the file.

Alternatively, run the script from a terminal:

  • ./autorun_bt.sh

The script do a "sleep" or delay between the "pactl" instructions, due to the fact that the audio buffer may be working and we need to delay this. PulseAudio will crash if you run the second instruction too quickly, requiring you to kill it to start again.

Instructions 21-25 are not automated, you will need to set up your headset as the default sink every time you re-connect your Bluetooth headset.

Automate source change

I wrote a little bash using dbus to detect bt changes and switch default source.

PS. You can change anytime pacmd set-default-source to pacmd set-default-sink to change sink instead source.

Micro

#TeixeiraSoft
set -o nounset

#Variables
declare -a blExistent
declare -a default

TEMP="/tmp";

function detect()
{
        echo "At detecting mode"
        dbus-monitor "path=/org/ayatana/NotificationItem/bluetooth_manager/Menu" |\
        awk '
               /member=ItemPropertyUpdated/ { getline;getline;getline; system( "./micro start "$3 ); }
        '

}
function start_head
{

        echo "Detected bt event $1"
        sleep 10s
        if [ $1 != $(echo "Connected") ]
        then
                return 0
        fi
        default=$( pacmd info | grep "Default source name" )
        default=$( echo $default | cut -d ":" -f 2 | sed 's/<//g'| sed 's/>//g' ) 
        bl=$( pacmd list-sources|grep bluez_source )
        if [ "$bl"  != "" ] 
        then 
                blExistent=$( echo $bl| cut -d ":" -f 2 | sed 's/<//g'| sed 's/>//g'  )
                
                if [ "$blExistent" == "$default" ] 
                then
                        echo "Already working with the device"
                        return 1
                fi

                echo "Default device:$default"
                echo $default >"$TEMP/old.pid"          
                echo "BT Source found: $blExistent"
                pacmd set-default-source $blExistent
        else
                echo "There is no bluetooth headset ready to pair"
        fi
}
export -f start_head
typeset -F
if [[ $# != 0 ]] 
then
        case $1 in 
                "start") start_head $2;;        
        esac
else
        detect
fi

Since the new Bluetooth manager already detects that there is a new sinks and update pulse after pairing the first time you can use this one instead:

#TeixeiraSoft
set -o nounset

#Variables
declare -a blExistent
declare -a default

TEMP="/tmp";

function detect()
{
        echo "At detecting mode"
        #dbus-monitor "path=/org/ayatana/NotificationItem/bluetooth_manager/Menu" |\
        dbus-monitor "path=/org/ayatana/indicator/sound/service, interface=org.ayatana.indicator.sound" |\
        awk '
               /member=SinkAvailableUpdate/ { getline; system( "./micro start " ); }
        '


}
function start_head
{
        echo "Detected bt event"
        default=$( pacmd info | grep "Default source name" )
        default=$( echo $default | cut -d ":" -f 2 | sed 's/<//g'| sed 's/>//g' ) 
        bl=$( pacmd list-sources|grep bluez_source )
        if [ "$bl"  != "" ] 
        then 
                blExistent=$( echo $bl| cut -d ":" -f 2 | sed 's/<//g'| sed 's/>//g'  )
                
                if [ "$blExistent" == "$default" ] 
                then
                        echo "Already working with the device"
                        return 1
                fi

                echo "Default device:$default"
                echo $default >"$TEMP/old.pid"          
                echo "BT Source found: $blExistent"
                pacmd set-default-source $blExistent
        else
                echo "There is no bluetooth headset ready to pair"
        fi
}
export -f start_head
typeset -F
if [[ $# != 0 ]] 
then
        case $1 in 
                "start") start_head;;   
        esac
else
        detect
fi


CategoryBluetooth

BluetoothHeadset (last edited 2017-09-08 03:01:27 by ckimes)