Headphone controls on Android – disabling the pickup button

Tomorrow I’m running in the Birmingham Great Run and am hoping to listen to a few motivational tunes along the way.

Recently I got some fancy new headphones that don’t drop out and are supposed to be sweat proof. However, I sweat. A lot. Consequently I manage to break the sweat-proof-barrier at around 45 minutes when the music will randomly pause and restart. Worse still after about an hour it will start randomly launching Android’s equivalent to Siri.

This is not good.

To stop it I have to fumble around, check the settings and then try concentrating on the running again.

I realised that this was a headphone problem when running on the treadmill and watching something on Netflix on the ipad. Just as I hit 45 minutes up popped Siri, delightfully prompting me for some instructions.

It turns out that the headphone controls in Android changed with the release of Ice Cream Sandwich. Apps that used to control the behaviour of the headset “hook” button stopped working (such as the apparently very useful Headset Blocker)

Instead I found this thread over at XDA Develpers. You need to be root to disable the keys, but the instructions boil down to finding the keylayout file for the headset, changing the value for HEADSETHOOK and then rebooting.

On my Nexus 4 running Android 4.3 I did the following:

Launch ADB:

macbookpro:platform-tools steve$ ./adb shell
shell@mako:/ $ su -

Go into the keylayout folder:

root@mako:/ # cd /system/usr/keylayout/

Look for files that include “226” – the keycode sent from the hook button.

root@mako:/system/usr/keylayout # grep 226 *
Generic.kl:key 226   MEDIA_NEXT
Vendor_22b8_Product_093d.kl:# key 226  tbd reserved key
droidmote.kl:key 226   HEADSETHOOK
hs_detect.kl:key 226   HEADSETHOOK       WAKE

I edited the hs_detect.kl file to change the 226 to 163 – apparently key code 163 will make applications that control the headset buttons work, such as Headset Button Controller.

Why the hs_detect.kl file? The instructions in the specific post says that you should edit the Generic.kl and sec_jack.kl files. The sec_jack.kl file didn’t exist on my phone, so first use “grep 226 *” to find the files that might be needed, then look for the one most likely. For me I knew that droidmote was an app that I’d installed, I had no idea what Vendor_22b8… was so I chose hs_detect.kl.

Before editing the file make the filesystem read-writable and take a backup. I just copied it:

root@mako:/system/usr/keylayout # mount -o rw,remount /system
root@mako:/system/usr/keylayout # cp hs_detect.kl hs_detect.kl.safe

and edited the file so it looked like this:

root@mako:/system/usr/keylayout # cat hs_detect.kl                             
key 163   HEADSETHOOK       WAKE
key 116   POWER             WAKE
key 107   ENDCALL           WAKE_DROPPED

After the phone is rebooted, the middle button stops working and can be managed using something else.

Now, hopefully, I won’t hit a technical brick wall at around 45 minutes. Sadly there’s nothing I can do about the rain.