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

Android NFC Screen Lock (Part 2)

Cover image courtesy of Andreas Lakso via Wikimedia Commons

In case you need a TL;DR from the first part of this story.

Logs are life

First, like any good sysadmin, I love logs. This was no exception. I installed adb on my laptop and dumped the logs like so:

adb logcat | grep NfcService

Now that we can see what’s really happening here, let’s find out what happens when we lock the screen

02-24 23:13:24.636 9863 10145 D NfcService: applyRouting - enter force = false mScreenState = 1
02-24 23:13:24.636 9863 10145 D NfcService: applyRouting
02-24 23:13:24.637 9863 10145 D NfcService: setScreenState=OFF 0 02-24 23:13:24.655 9863 10145 D NfcService: computeEmulationParameters
02-24 23:13:24.655 9863 10145 D NfcService: configure EE routing
02-24 23:13:24.655 9863 10145 D NfcService: configure HCE routing
02-24 23:13:24.655 9863 10145 D NfcService: NFC-HCE OFF
02-24 23:13:24.678 9863 10145 D NfcService: configure routing table false
02-24 23:13:24.678 9863 10145 D NfcService: computeDiscoveryParameters
02-24 23:13:24.678 9863 10145 D NfcService: NFC-C OFF

As expected, the logs show that when the screen gets turned off, the NFC polling features also get turned off. NFC-C and NFC-HCE are both off now. So now let’s find out what happens when the screen is on, but locked.

02-24 23:22:40.862 9863 16366 D NfcService: applyRouting - enter force = false mScreenState = 2
02-24 23:22:40.862 9863 16366 D NfcService: applyRouting
02-24 23:22:40.862 9863 16366 D NfcService: setScreenState=ON_LOCKED
02-24 23:22:40.890 9863 16366 D NfcService: computeEmulationParameters
02-24 23:22:40.890 9863 16366 D NfcService: configure EE routing
02-24 23:22:40.890 9863 16366 D NfcService: configure HCE routing
02-24 23:22:40.890 9863 16366 D NfcService: configure routing table false
02-24 23:22:40.890 9863 16366 D NfcService: computeDiscoveryParameters
02-24 23:22:40.890 9863 16366 D NfcService: Discovery configuration equal, not updating.

Interestingly, we don’t see any indication that the NFC polling features are turned on. Also, the “Discovery configuration equal, not updating” message seems to indicate the polling state should remain the same. So finally, let’s see what happens when the screen is turned on.

02-24 23:27:19.930 9863 9973 D NfcService: applyRouting - enter force = false mScreenState = 3
02-24 23:27:19.930 9863 9973 D NfcService: applyRouting
02-24 23:27:19.931 9863 9973 D NfcService: setScreenState=ON_UNLOCKED
02-24 23:27:19.931 9863 9973 D NfcService: computeEmulationParameters
02-24 23:27:19.931 9863 9973 D NfcService: configure EE routing
02-24 23:27:19.931 9863 9973 D NfcService: configure HCE routing
02-24 23:27:19.931 9863 9973 D NfcService: NFC-HCE ON
02-24 23:27:20.003 9863 9973 D NfcService: configure routing table false
02-24 23:27:20.003 9863 9973 D NfcService: lockscreen ON_UNLOCKED, force NFC enableDiscovery()
02-24 23:27:20.003 9863 9973 D NfcService: computeDiscoveryParameters
02-24 23:27:20.003 9863 9973 D NfcService: NFC-C ON, shouldRestart=false

Clearly, NFC is now on. What this tells me is that the smart lock feature would likely work, if only NFC polling were enabled when the screen is in a locked, but on state. There are some clues as to what methods and classes we need to look at here, so the next step is to track this down in the source code.

Spelunking in Java code

You could start by downloading the Android source tree, but that is a bit overkill for this fix. Instead, I found the source in a handy online browser here.

There are all kinds of methods you could use to track down a problem in a large code base, but I find the most reliable way is to locate the source of an error by grepping for the print statement that generates the debug messages we see above.

The Fix

Update from 2024: At some point I must have stopped writing this blog or lost the rest of what I intended to write. Which is really too bad considering I did end up finding the source code for my fix, which did definitely work. Unfortunately, that was 7 years ago me and I remember nothing about that guy. And apparently he was some kind of savant because the code is completely incomprehensible. But in case you want to peruse it yourself, I published it on Bitbucket.

Built with Hugo
Theme Stack designed by Jimmy