Wednesday, June 2, 2010

Headset Blocker

If you own a Nexus One, you may be familiar with this scenario: you load up your N1 with some rocking tunes. You plug in your headphones and go out exercising with it. Suddenly, the tunes start skipping. Why?

The problem is that the headset jack is interpreting what you are doing as a control signal. The exact cause hasn't been shown to me yet; there are a number of theories from a lack of software noise filtering to the difference between TRS and TRRS connectors. Regardless, it's a serious issue - some people can't even listen to music in their cars with the N1 because it skips so much.

Luckily, in the last few days I was shown an answer. The control commands from headsets are actually sent out as a chain broadcast, MEDIA_BUTTON. As with any chain broadcast, your receiver can abort the broadcast. So the simple solution is to make a BroadcastReceiver with an ultra-high priority that intercepts and aborts the MEDIA_BUTTON broadcast.

To that end, I've put a small app on the Market, Headset Blocker. It's a small, free widget that allows you to enable/disable blocking of the MEDIA_BUTTON broadcast. You can get it here:

QR code for Headset Blocker

The source code is here.

One interesting choice I had to make when writing this app was how to toggle the blocking. I had one of two options:

1. Use SharedPreferences to track when blocking is enabled. Capture all MEDIA_BUTTON broadcasts, but only abort when the preference is enabled.

2. Enable/disable my app's BroadcastReceiver. It will always abort when enabled, but is only enabled via the widget.

I went with the latter option, as I felt that would save just a little bit more battery when disabled (as the BroadcastReceiver itself would be disabled).

4 comments:

  1. Mate, just gotta say thanks so much for this. Been driving me mad! Just took delivery of my brand new HTC Desire (Bravo) and all was fine until i wanted to listen to a podcast when walking my dog! Here, have some Kudos :)

    ReplyDelete
  2. Awesome. I've almost thrown my Nexus out of the car window on numerous occasions. You have saved not only my phone, but also my sanity.

    One million internets to you sir.

    ReplyDelete
  3. Daniel
    Your widget sounds great...just what i need. I have a pulse mini phone and want to connect it to a PA speaker. I have tried a normal stereo mini jack but there is always hum or feedback. I have tried to download from the market and other sites but dont seem to be able to. Any chance of emailing me a private link... i really could use your widget!!
    Thanks
    Carl

    ReplyDelete
  4. Fantastic mate. I sent my phone back 6 times for repair, causing me endless grief and months without a phone. this fixes it perfectly.

    Thanks very much

    ReplyDelete