Pausing Banshee using bluetooth proximity

In my continued efforts to control everything with bluetooth proximity, I’ve successfully nailed another target: Banshee media player. Banshee is the default media player supplied with Ubuntu and has some surprisingly useful command line arguments that allow it to be controlled from ruby scripts. I’ve written one that pauses banshee if I leave the room while something is playing and then resumes where it left off as I re-enter the room.

The script will look quite familiar if you’ve read Using text to speech to announce new emails on arrival home; I have slightly changed the isPresent function so that it encapsulates the double check I’ve discovered needs doing each time. Other than that it’s the same old code tracking the state and acting only when it’s changed.

Banshee’s command line arguments –query-current-state, –pause and –play do exactly what you’d expect, this provides the interface with an instance of banshee that is already running. There is a slight hitch insomuch as if there is no current banshee instance, banshee –query-current-state will initiate one; I worked around this by querying ps for a process called banshee before trying banshee –query-current-state.

banshee_bluetooth.rb:

#You'll have to look this up manually by using the hcitool command
BLUETOOTH_ADDRESS='00:26:5F:C6:30:A3'

def isPresent(address)
  #do it twice to double check that we didn't just momentarily loose contact
  if (`hcitool name #{address}`=='' && `hcitool name #{address}`=='')
    return false
  else
    return true
  end
end

previous_state=true
paused=false
while true
  if isPresent(BLUETOOTH_ADDRESS)
    #if the phone wasn't here before but is now, it's just arrived back
    if !previous_state
      if paused
        sleep 3
        system('banshee --play')
        paused=false
      end
    end
    previous_state=true
  else
    #if the phone was here before but isn't now, it's just left
    if !previous_state
      #we only want to pause/restart it if it's playing when we leave, however if we use banshee --query-current-state when banshee isn't running we'll open banshee
      if `ps -e | grep banshee` !='' && `banshee --query-current-state`=='current-state: playing'+"\n"
        system('banshee --pause')
        paused=true
      end
    end
    previous_state=false
  end
end

3 thoughts on “Pausing Banshee using bluetooth proximity

  1. Eric Erichsen

    I’m intrigued with the power and versatility of the new smartphones. I have no formal training, besides the dinosaurs excel and auto cad. But ubuntu and oma files keep showing up on my phone. the latest being your hci tools altering my bluetooth. I am wrestling with the fact that applications take control of my phone. When I found myself signed up to a developer site, and I’m definitely not a developer, I should seek professional help. What can average joe do to protect im self again such attacks??

    Reply
    1. Mike Post author

      I’m still something of a Luddite when it comes to phones; I’m sticking with my indestructible, week-long-battery-life old phone. I’m not sure what’s going on with yours, keeping it turned off when unused and keeping your phone up to date might help.

      Reply
  2. Longwall Quad

    “… pauses banshee if I leave the room while something is playing …”

    No more angry Dreyfus?

    Reply

Leave a Reply