Featured image of post Android NFC Screen Lock (Part 1)

Android NFC Screen Lock (Part 1)

Cover image courtesy of VampishBowl via Wikimedia Commons

This post is more about the saga of my NFC screen lock problems. For the TL;DR version with technical details, check out this page instead.

So I had this idea the other day for unlocking my phone with an NFC token. I like to use strong passwords everywhere I can, and since my phone is no exception, I spend a lot of time typing out my password. Wouldn’t it be great if you could bypass that so long as you have a special key? I thought, surely this can’t be the first time someone thought about doing something like this. As it turns out, I was right. There is a neat feature with Android phones (kit-kat and later) called “Trusted Devices”. This is part of the Smart Lock feature that gives users the ability to unlock their phone with things like their face, or voice, or even when their phone is in a certain geographical location. NFC tags are really cheap, so I went ahead and bought a 10 pack of wristbands for $15. I chose the wristbands because I felt that it was unlikely I would lose my phone at the same time my wristband fell off my wrist spontaneously without my noticing it. Also, it is in a really convenient place for unlocking your phone.

You can imagine my frustration when I set up trusted devices with smart lock and… nothing happened. It doesn’t work. At all. OK, no problem, I can fix this, right? I verified I had Google Play services set enabled as a trust agent. I made sure NFC was enabled (yes, it works with the screen on, so I know it and my tag both work together). I tried rebooting. I tried rebooting after turning NFC off and on. I tried rebooting after turning Smart Lock on and off. The list goes on. After several hours of dissilusionment, I got it into my head that I will make this work. As with many technical problems I have faced during my career, I would not let this simple thing defeat me. So with that resolution, I proceeded to bring out the big guns.

Step one was to eliminate the custom ROM from Verizon/HTC that I really didn’t like anyway. I have long since tired of looking at the NFL app in my apps list, especially since I will never use it. Yet I can’t remove it. That and I absolutely hate the HTC contacts list, photo app, keyboard, pretty much everything about HTC Sense from the day I bought this phone and I cannot supplant it with the default Google apps that are much better anyway. My phone is S-ON, meaning I can’t flash the bootloader, so I needed to find a way to rid myself of this annoyance. Enter Sunshine. Sunshine is a paid app that does a surprisingly good job at jailbreaking your phone. I was wary about paying for this app, but at this point I was already invested so I pulled the trigger. Sure enough, it did exactly as advertised. Win!

As an aside here, it is worth noting that I have never actually been inclined to root my phone before this incident. I guess I didn’t like the prospect of bricking the phone and having to buy a new one, or maybe there just wasn’t enough of an incentive. Anyway, back to the story.

Now what I asked myself? Well, first step is to install Team Win Recovery Project in the recovery partition on my phone. This would allow me to load custom ROMs, make backups of my existing OS, and so on. It is really a great tool and I am very happy that I have finally taken the plunge to modify my phone at this point. With TWRP happily installed on my recovery partition, I grabbed the latest version of the Google Play Edition ROM for my phone and installed it. The ROM I found had several options, stock with no root, rooted with traditional method, rooted with systemless method. At this point I thought the issue was with HTC’s nightmare customizations, so I didn’t root the phone. Eagerly I configured trusted devices and… nothing, yet again.

This time I broke out ADB and decided to do some digging. Obviously something was wrong here. Running logcat, it became readily apparent what the problem was. On my phone, for some unknown reason, whenever the screen locked, but was still on, the NFC functionality was disabled; you can see the NFC radio being shut off in the log messages from logcat. So obviously it wasn’t going to work, even if trusted devices was working as expected. The problem is, I could not find any way to disable this behavior. To my knowledge, this is either a bug, or a poorly designed security feature. Either way, at this point I was in a borderline murderous rage at the amount of time I had already sunk into getting this simple feature working with absolutely no results and no idea where to go next.

Back to TWRP I went, this time to root my phone for lack of a better option. I know almost almost as little about Android as your average consumer (a blindspot I was beginning to regret around this time) and I was hoping my linux knowledge plus root would bail me out. As I was rooting my phone, I noticed an option for installing the Xposed framework with a little blurb about modules and modifying core behavior, etc. “Hey, this might be the ticket” I thought, so I Googled it. Low and behold, not only is it this super cool framework for modding your phone, but someone had already written a module for fixing this exact problem! So away I went, installing the new OS with root and the Xposed framework, downloaded the module and rebooted my phone and… of course it didn’t fucking work.

I did some spelunking on the god-awful forums that comprise the online Android community. Seriously, they’re awful: 80/20 advertisements and bogged down with idiot consumers wanting to know how they can root their phone so they can be 1337 haxxors with modded phones, or something like that. Anyway, after some time I came across an updated version of the Xposed module I had found before from a developer named Nephiel. Don’t be surprised, this module also didn’t work either, BUT, it was updated enough that I thought I could use it as a starting point for the work I needed to do. So I had to learn yet another new technology, Android Studio.

In order to fix the code that was broken and make the changes I needed, I had to use Android studio to package up the application as an APK that could be installed on my phone. I haven’t used an IDE like Android Studio since college, which was the last time I touched Java too.

Built with Hugo
Theme Stack designed by Jimmy