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?
I didn't like the polling approach, so I did some digging on bluez and DBus. I ended up writing the following script:
To discover a successfully established Bluetooth connection we can run
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
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
See this answer on how to switch audio to this sink.
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:
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.
And now you shall be able to enjoy your Bluetooth device with Ubuntu.