Connecting Bluetooth devices from command line

Posted: February 28, 2011 in Linux Sound
Tags: ,

Sometimes it may be desirable to connect a bluetooth audio device, such as headset, to the Linux audio service from the commandline. This isn’t well documented, especially since it wasn’t possible in some older versions of bluez, and just recently became possible again.

Firstly, as far as I know you need atleast bluez 4.87 to do this. Well, it doesn’t work in bluez 4.66.

Firstly, to do this we need to get the dbus address of the bluetooth adapter, which changes with restarts.

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

This will output the address of the bluetooth device.

Next, we will use bluez-simple-agent to pair the device. My device like many can be paired with the passphrase “0000”. So I pipe that to the bluez-simple-agent.

echo "0000" | bluez-simple-agent hci0 00:12:A1:90:39:A9

With the device paired and the address of the bluetooth adapter, we can now connect the device to the audio sink.

dbus-send --print-reply --system --dest=org.bluez BT_ADAPTER/dev_00_12_A1_90_39_A9 org.bluez.AudioSink.Connect

This command requires pulseaudio-module-bluetooth to be installed (I’m big on pulseaudio). You can see where I have the mac address and the bluetooth adapter address.

To disconnect the device from the audio sink you can simple run the same command again, but replace “Connect” with “Disconnect”.

You can also use the following command to “unpair” the bluetooth device.

bluez-test-device remove 00:12:A1:90:39:A9

So, just remember to use the mac address of your own device in substitution for mine.

Also, note that none of these commands need to be run as root!

Here is a script I wrote which automates the process. If I can find the script that I referenced, I will credit it. “pulsespeak” is a program I wrote as a wrapper to espeak.

#!/bin/bash

### The Mac of the device I'm connecting to is
### 00:12:A1:90:39:A9

### I should make a variable
### MAC_ADD=00:12:A1:90:39:A9

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

if [ "$1" == "off" ]; then
dbus-send --print-reply --system --dest=org.bluez $BT_ADAPTER/dev_00_12_A1_90_39_A9 org.bluez.AudioSink.Connect
#echo "0000" | bluez-simple-agent hci0 00:12:A1:90:39:A9 remove
bluez-test-device remove 00:12:A1:90:39:A9
fi

if [ "$1" == "on" ]; then

echo "0000" | bluez-simple-agent hci0 00:12:A1:90:39:A9

dbus-send --print-reply --system --dest=org.bluez $BT_ADAPTER/dev_00_12_A1_90_39_A9 org.bluez.AudioSink.Connect
#pulsespeak "Bluetooth is now connected"
fi
About these ads
Comments
  1. [...] Maybe this will help. The command to pair via commandline is displayed in the article. http://zitzlinux.wordpress.com/2011/…-command-line/ [...]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s