Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Background: I'm using my bluetooth headset as audio output. I managed to get it working by the long list of instructions on BluetoothHeadset community documentation, and I have automated the process of activating the headset as default audio output into a script, thanks to another question.

However, since I use the bluetooth headset with both my phone and computer (and the headset doesn't support two input connections) in order for the phone not to "steal" the connection when handset is turned on, I force the headset into a discovery mode when connecting to the computer (phone gets to connect to it automatically).

So even though the headset is paired ok and would in "normal" scenario autoconnect, I have to always use the little bluetooth icon in the notification area to actually connect to my device (see screenshot).

What I want to avoid: This GUI for connecting to a known and paired bluetooth device:

Connecting to Bluetooth headset using icon

What I want instead: I'd want to make the bluetooth do exactly what the clicking the connect item in the GUI does, only by using command line. I want to use command line so I can make a single keypress shortcut for the action, and would't need to navigate the GUI every time I want to establish a connection to the device.

The question: How can I attempt to connect to a specific, known and paired bluetooth device from command line?

Further question: How do I tell if the connection was successful or not?

share|improve this question

I use this script to connect my Bluetooth Audio Device. If your headset is already paired, you should be able to connect your headset in the same way using org.bluez.Headset.Connect/Disconnect in place of org.bluez.Audiosink.Connect/Disconnect.



BT_ADAPTER=`dbus-send --system --print-reply --dest=org.bluez / \
org.bluez.Manager.DefaultAdapter|awk '/object path/ {print $3}'`

echo "Connecting to $BT_ADAPTER..."

if [ "$1" == "on" ]; then
    dbus-send --print-reply --system --dest=org.bluez $BT_ADAPTER org.bluez.AudioSink.Connect
elif [ "$1" == "off" ]; then
    dbus-send --print-reply --system --dest=org.bluez $BT_ADAPTER org.bluez.AudioSink.Disconnect


share|improve this answer
Thanks, this script worked for me today :) – wim Apr 9 '14 at 23:52

Bluetooth daemon

In the default installation a daemon (bluetoothd) runs in the background (run from the file /etc/init.d/bluetooth). This daemon takes care on recognizing and connecting to known bluetooth devices and may be cofigured with configuration files in /etc/bluetooth. For autoconneting a headset the following line in audio.conf should be uncommented (remove #):


To restart the daemon type sudo /etc/init.d/bluetooth restart.

Remark: Using the command line tool sudo hcitool cc <MAC-Adress> did not lead to a stable connection to a known device in the test environment here when the daemon was running.


In order to connect a disconnected but physically present and paired headset we can use D-Bus from a script. Here's an example in python:

# Toggles headset connection

import dbus
from dbus.mainloop.glib import DBusGMainLoop

dbus_loop = DBusGMainLoop()
bus = dbus.SystemBus(mainloop=dbus_loop)

#Get dbus interface for headset
manager = bus.get_object('org.bluez', '/')
iface_m = dbus.Interface(manager, 'org.bluez.Manager')
adapterPath = iface_m.DefaultAdapter()
adapter = bus.get_object('org.bluez', adapterPath)
iface_a = dbus.Interface(adapter, 'org.bluez.Adapter')
devicePath = iface_a.ListDevices()[0]  # assuming first device
device = bus.get_object('org.bluez', devicePath)
iface_h = dbus.Interface(device, 'org.bluez.Headset')

#Check state of connection
connected = iface_h.IsConnected()
print 'Toggling connection. Please wait'
# toggle connection
if not connected:
        print 'Connecting: ', devicePath
        print 'Device not found'
    print 'Disconnecting: ', devicePath

In case we have more than one Bluetooth device we will have to adapt the devicePath appropriately, of course. The example above will connect a Headset. Change the interface to a different protocol for any other service (e.g. AudioSink).


If you know the MAC adress of your Bluetooth device you can connect it as an output sink for pulseaudio by:

pacmd set-default-sink bluez_sink.xx_xx_xx_xx_xx_xx

Where xx_xx_xx_xx_xx_xx is the MAC address (replace ':' by '_' for pulseaudio to recognize it).

See also this answer for more details.

share|improve this answer
I do know the address, and I replaced the xx with it. I only get Sink bluez_sink.xx_xx_xx_xx_xx_xx does not exist. Tried both uppercase and lowercase. – Ilari Kajaste Jun 10 '11 at 8:56
Yes, pulseaudio-module-bluetooth is isntalle. No, nothing matching bt or blue is listed in pacmd list-sinks. (It reports only 1 sink available.) – Ilari Kajaste Jun 10 '11 at 9:29
So it's not recognized. This is a prerequisite to be able to connect by commandline. Try restarting BT or try restarting pulsaudio. I've not yet found out why it's sometimes not detetcted. – Takkat Jun 10 '11 at 9:38
Huh? So it can be in a state that it's possible to connect from GUI, but not from CLI? – Ilari Kajaste Jun 10 '11 at 9:46
@Takkat Oh, yes, good point. My bad! I'm using the headset in discovery more, so it won't autoconnect. I edited the question to reflect this. Sorry for leading you down to a wrong path. – Ilari Kajaste Jun 10 '11 at 10:44

Your Answer


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.