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: 
@Erigami Your answer helped a lot but to make it work I'd to do some changes. I'm using ubuntu 14.04.

```
#!/usr/bin/python

import dbus
from dbus.mainloop.glib import DBusGMainLoop
import gobject

import subprocess

# ID of the device we care about
DEV_ID = 'CC:C3:EA:A5:16:90'.replace(":", "_")

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

# Figure out the path to the headset
man = bus.get_object('org.bluez', '/')
iface = dbus.Interface(man, 'org.bluez.Manager')
adapterPath = iface.DefaultAdapter()

print(adapterPath + '/dev_' + DEV_ID)
headset = bus.get_object('org.bluez', adapterPath + '/dev_' + DEV_ID)
# ^^^ I'm not sure if that's kosher. But it works.

def cb(*args, **kwargs):
    is_connected = args[-1]
    if isinstance(is_connected, dbus.Boolean) and is_connected:
        print("Connected")
    elif isinstance(is_connected, dbus.Boolean) and not is_connected:
        print("Disconnected")

headset.connect_to_signal("PropertyChanged", cb, interface_keyword='iface', member_keyword='mbr', path_keyword='path')

loop = gobject.MainLoop()
loop.run()

```

Still if this does not work then use and monitor system dbus.

```
dbus-monitor --system

```

`d-feet` can be used further. It is GUI tool to watch dbus objects.
If none of the previous solutions worked for you for any reason, I wrote a Python script using `pydbus` package. `dbus-python` as in @Erigami, @Hitul and @Yixing answers was giving me hard times, so I switched to this one.

<https://gist.github.com/altbrace/52ae1783b31257021520673fadb95b6e>

This particular script changes the sound profile of the Bluetooth device to mSBC, but you can alter the `subprocess` call to do whatever you wish. If so, you can also remove the `time.sleep()` call.

Add this to background execution as described by @Yixing or however else.