Question: I'd like to start my music player (Clementine) when my bluetooth headset connects to my computer. How do I detect the bluetooth device connecting so I can run a script to start the player?

Answer: 
To discover a successfully established Bluetooth connection we can run

```
sdptool browse xx:xx:xx:xx:xx:xx

```

By this the SDB connection will be tested for a connection to the given MAC address. It may take considerable time until browsing times out with an error like

```
Failed to connect to SDP server on 00:0C:78:4F:B6:B5: Host is down

```

We don't know the exact purpose of your script, but most likely you wish to play audio via Clementine when a headset was connected.

Then we could just see whether there is a Bluetooth audio sink with

```
pacmd list-sinks | grep xx_xx_xx_xx_xx_xx

```

Where `xx_xx_xx_xx_xx_xx` is the MAC address (`:` needs to be replaced with `_`). The output will then tell you whether there is a Bluetooth audio sink available or nothing if not.

See [this answer](https://askubuntu.com/a/72076/3940) on how to switch audio to this sink.

---

### [Stream2ip](https://launchpad.net/stream2ip)

With **stream2ip** we can define a shell command or a script to run after a connection was established. There also is an option to automatically start a supported media player after a connection was established:

![enter image description here](https://i.stack.imgur.com/EiNvK.png)

Stream2ip will also try to reconnect the currently running playback stream to the Bluetooth audio device in case the connection was interrupted.
Here is another example for monitoring all Bluetooth devices. It does not need to specify a specific MAC address. This approach makes the xinput setting persistent even when login/out, suspend/wake and connect/dis-connect your bluetooth device.

I have a Thinkpad compact Bluetooth Keyboard, and I wish to run an xinput command whenever the keyboard is connected to adjust the speed of trackpoint. Here are the steps.

1. Download code from Github [bluetooth-ruunner](https://github.com/linuxthink/bluetooth-runner). Credits given to [here](https://www.raspberrypi.org/forums/viewtopic.php?f=91&t=85101) who first wrote this for Raspberry Pi. **Modify** the following section of the code to run your custom comamnds.

```
subprocess.call(['xinput', 'set-prop',
                 'ThinkPad Compact Bluetooth Keyboard with TrackPoint',
                 'Device Accel Constant Deceleration', '0.6'])

```

In my case, this is equivalent to calling from the terminal.

```
$ xinput set-prop 'ThinkPad Compact Bluetooth Keyboard with TrackPoint' 'Device Accel Constant Deceleration' 0.6

```
2. Save the modification. Try running your scripts by

```
$ python bluetooth-runner.py

```

Connect and disconnect your Bluethooth device. You should see the corresponding message printed on screen.
3. Now, make your file executable and copy it in one of the directory in your `$PATH`, say `~/bin/`.

```
$ chmod +x bluetooth-runner.py
$ mkdir ~/bin # if you dont have it yet
$ cp bluetooth-runner.py ~/bin

```
4. Now, make sure you can run the script from anywhere in the terminal (make sure it is in your search path).
5. Fire up the `Startup Applications` from the ubuntu menu. Add your scripts to the startup.

![Add startup applications](https://i.stack.imgur.com/yheK7.png)
6. Now, there is only one problem left, at the time when you login, the scripts might not catch the **very first** bluetooth event. This is because your bluetooth device might be connected before your script is initialized in the background. 

To solve this, add your custom command directly in `Startup Applications`. In my case, it is the follow command:

```
 xinput set-prop 'ThinkPad Compact Bluetooth Keyboard with TrackPoint' 'Device Accel Constant Deceleration' 0.6

```

And now you shall be able to enjoy your Bluetooth device with Ubuntu.