A bluetooth started song on MPD server

This is a very simple Python script that I put together some time ago. I had only two goals on my mind when writing this: see if I can and fun. Both goals were achieved.

The script uses bluetooth module to scan for bluetooth device in surroundings and compares it with those provided in script itself. If it encounters a matching device then it will attempt to connect to the MPD server and play selected song. If the song is not on the current play list then it will try to find it in the library, add i to the play list and play.

I ran this script at my work desktop (Ubuntu with bluetooth dongle) and triggered it with my phone’s bluetooth (by enabling it and making visible) for couple of days when entering the office I felt like the Lord Vader with imperial march was started upon my arrival.

Full script can be obtained from my Bitbucket Git repository, below are only few “configuration” lines:

Define device and music track:

user3 = {
'title':"The Empire Strikes Back",

The “bt_addr” is of course the address of the bluetooth device that will trigger the action. The “song” holds a full path to the selected track from point of view of the MPD’s media library where “title” is a piece of title of the song by which we would be able to locate this one specific song in the whole MPD library – it will be used to find and add the track to the current playlist shouldn’t it be there already. “start_second” tells the MPD where to start the track – our track my start from specific moment for greater effect.

All that’s left is to ensure that it will be loud enough to bring an attention:


And start playing at selected moment:


The only issue I had with this is that it usually takes some time for the bluetooth device to be discovered and therefore it’s hard to synchronise with the music. Nevertheless it was fun for me and I hope it will be some use to you also.

Full script can be cloned from with following repository:


Posted in Programming | Tagged | Leave a comment