4

I am wondering when exactly the NFC Service is started and stopped. The source code for android 4.0.3 seems to state that the polling is dependent on a single constant (located in NfcService.java)

/** minimum screen state that enables NFC polling (discovery) */
static final int POLLING_MODE = SCREEN_STATE_ON_UNLOCKED;

I would interpret this as "the screen light is on, therefore the nfc service is active". BUT when the screen is locked, a NFC Tag wont be recognized, altough the screen is lit.

So I am curious: Is the NFC Service already deactivated when the lock screen appears, or is it still running but not processing the Tags?

hamena314
  • 2,969
  • 5
  • 30
  • 57
  • I am also having the same problem and searching for result. Check my question http://stackoverflow.com/questions/10733723/launching-nfc-when-screen-is-locked-in-android – Venky May 25 '12 at 10:26
  • 1
    I am pretty sure that the screen has to be on AND unlocked (like the constant suggests: "...ON_UNLOCKED". But I could not find any official affirmation. Therefore I still hope that someone could help me there. A workaround that I am considering is to make your own homescreen / launcher and put in your own Lock Screen. This will be a pseudo one but the NFC Service should still be running. I think you have to avoid using the keyguard, as it also seems to use the constant. – hamena314 May 25 '12 at 10:30
  • We need to handle polling_mode when screen goes locked – Venky May 25 '12 at 10:34
  • 1
    From this code it seems there's a force boolean override. How do we set this externally? Build prop? Reflection? – mcollard Aug 12 '12 at 02:53

2 Answers2

3

Actually, I do not think that NFC Service is deactivated. When the screen has lower value then SCREEN_STATE_ON_UNLOCKED a device stops to ask NFC tags around. You can see this from this code:

    // configure NFC-C polling
    if (mScreenState >= POLLING_MODE) {
        if (force || !mNfcPollingEnabled) {
            Log.d(TAG, "NFC-C ON");
            mNfcPollingEnabled = true;
            mDeviceHost.enableDiscovery();
        }
    } else {
        if (force || mNfcPollingEnabled) {
            Log.d(TAG, "NFC-C OFF");
            mNfcPollingEnabled = false;
            mDeviceHost.disableDiscovery();
        }
    }

But NFC-EE routing is enabled util screen state is higher then SCREEN_STATE_ON_LOCKED:

    // configure NFC-EE routing
    if (mScreenState >= SCREEN_STATE_ON_LOCKED &&
            mEeRoutingState == ROUTE_ON_WHEN_SCREEN_ON) {
        if (force || !mNfceeRouteEnabled) {
            Log.d(TAG, "NFC-EE ON");
            mNfceeRouteEnabled = true;
            mDeviceHost.doSelectSecureElement();
        }
    } else {
        if (force ||  mNfceeRouteEnabled) {
            Log.d(TAG, "NFC-EE OFF");
            mNfceeRouteEnabled = false;
            mDeviceHost.doDeselectSecureElement();
        }
    }

The service itself is started and stopped in other parts of this class.

Yury
  • 20,618
  • 7
  • 58
  • 86
  • Thanks Yury! I've seen some versions to lock the screen by turning its brightness under a certain level. Now that makes sense. Do you see a possibility to start the service manually? I am talking about a non-rooted phone and not a personal crafted ROM. – hamena314 May 25 '12 at 15:03
  • The NFC service is always running, irrespective of whether NFC is on or off. – NFC guy May 25 '12 at 16:12
  • I also think so. Just I did not want to check this. That's why I wrote "I do not think." ) Thanks for clarification! – Yury May 25 '12 at 19:50
1

See related http://forum.xda-developers.com/showthread.php?t=1712024&page=14

mcollard
  • 161
  • 1
  • 12