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:
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?
Best Answer
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 inaudio.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.DBus
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:
In case we have more than one Bluetooth device we will have to adapt the
devicePath
appropriately, of course. The example above will connect aHeadset
. Change the interface to a different protocol for any other service (e.g.AudioSink
).Pulseaudio
If you know the MAC adress of your Bluetooth device you can connect it as an output sink for pulseaudio by:
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.