Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Using a bluetooth headset that does Stereo (out only), mono (in / out) and speakers. Switching between the 3 is a lot of clicks. What I want to do: - run Stereo either on Speaker or headset - when I make or accept a voip call (Skype, gTalk etc): switch to thr Bluetooth profile with input enabled - switch back after the call

.or. Have a simple menu (indicator?) that allows me to switch between preset combinations of in/out

How do I do that?

share|improve this question
1  
Update: There is a Sound Switcher Indicator (v1.2.2 as of date of this comment) that allows to quickly switch profiles. Written by Dmity Kann yktoo.com/software/indicatorSoundSwitcher/installation –  stwissel Mar 3 at 8:18

2 Answers 2

up vote 1 down vote accepted

To my knowledge there is not a simple one-click solution that would give us a fast switching from internal audio to Bluetooth A2DP audio profile, and Bluetooth HSP headset profile.

However we can change the sound output from command line that gives us the opportunity to create a script to switch the audio output. The commands we need are the following:

  • pacmd list-cards
    will list the cards available. As soon as a headset is connected we will have access to the Bluetooth audio properties. In addition we can now find out the pulseaudio card index, and possible profiles for the following commands.

  • pacmd set-card-profile <index> <profile>
    changes the profile for a card number <index>. For a Bluetooth card the profile in question will be a2dp for audio profile, and hsp for headset telephony profile.

  • pacmd set-default-sink and pacmd set-default-source
    will change the output resp. input used by pulseaudio. This can not be done during playback. See this answer for details on how to do that.

Below there is an example script that may be used to switch the sound output for a Bluetooth headset:

#!/bin/bash
# CLI options:  `a2dp': Audio Profile
#               `hsp':  Telephony Profile
#               <Index> Default Sink (try `0' or `1')

SINK=$( pacmd list-cards | grep -B 1 bluez )
INDEX=${SINK:10:2}
SINK=$( pacmd list-cards | grep bluez )
MAC=${SINK:19:17}
BT_SINK="bluez_sink.$MAC"
BT_SOURCE="bluez_source.$MAC"

if [ $1 = a2dp ]; then
    echo Setting A2DP audio sink $BT_SINK
    pacmd set-card-profile $INDEX a2dp
    pacmd set-default-sink $BT_SINK
elif [ $1 = hsp ]; then
    echo Setting HSP headset sink $BT_SOURCE
    pacmd set-card-profile $INDEX hsp
    pacmd set-default-sink $BT_SINK
    pacmd set-default-source $BT_SOURCE
else
    echo Resetting to internal audio
    pacmd set-default-sink $1
    pacmd set-default-source $1
fi

Copy the script, save it as switch-sink, and grant it executable permission. In a terminal change to the script directory and issue the script with the following options:

switch-sink a2dp    # switches to audio profile
switch-sink hsp     # switches to headset/telephony profile
switch-sink <index> # switches to sink <index>, e.g. 0 depending on our default

Give the full path to the script when executing from a keyboard shortcut or from a .desktop file (for desktop or launcher).


Additional note on sound card INDEX:

In case there are more than 99 sound cards registered with pulseaudio this script here will not work. This may become an issue as the card INDEX will be increased with every power ON or reconnect of a Bluetooth device. As a workaround we may change the last else condition to reset the pulseaudio server (which will then also load our default audio sink):

else
    pulseaudio -k
fi
share|improve this answer
    
Cool. In Unity I can associate such script with a right click menu and I have a right/left click solution. Thx a lot –  stwissel Jul 4 '12 at 4:19
    
Yeah, that's the advantage of CLI & scripts. You can do it all - even wrap a GUI or an AppIndicator around it. A minor issue is that we can not have more than 99 audio card indices with the primitve method here to get the card INDEX - see edit for workaround :) –  Takkat Jul 4 '12 at 6:28

I think I found a bug with this solution. In my case only 1 was set as index so I've got this error >>> No card found by this name or index.

$ pacmd list-cards | grep -B 1 bluez
    index: 10
    name: <bluez_card.00_0D_44_A6_7D_85>
--
        device.string = "00:0D:44:A6:7D:85"
        device.api = "bluez"
--
        device.form_factor = "headset"
        bluez.path = "/org/bluez/1085/hci0/dev_00_0D_44_A6_7D_85"
        bluez.class = "0x240404"
        bluez.name = "Logitech Wireless Headset"
--
    sinks:
        bluez_sink.00_0D_44_A6_7D_85/#11: Logitech Wireless Headset
    sources:
        bluez_sink.00_0D_44_A6_7D_85.monitor/#22: Monitor of Logitech Wireless Headset

I've changed your script and now it's working good. (It's not maybe best solution)

This is what I changed

#SINK=$( pacmd list-cards | grep -B 1 bluez | head -1 )
INDEX=$( pacmd list-cards | grep -B 1 bluez | head -1 | awk ' { print $2 } ' )
#SINK=$( pacmd list-cards | grep bluez )
MAC=$( pacmd list-cards | grep bluez | head -1 | awk -F . ' { print substr($2,0,length($2)) }' )
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.