1

I had a code able to detect an already present NFC tag when I start my NFCadapter. Which is very nice since it means you don't need to move the tag to detect and read it :)

Since I update a Samsung phone to 5.1.1 form 4.4.4, this is not working any more. Moving the tag to close contact does fire onTagDiscovered() but it used to be fire instantly.

AFAIK, Google changes around NFC should not have impacted my workflow : http://developer.android.com/sdk/api_diff/21/changes.html displays only added methods. And changes from 21 to 22 does not seem to have impact nfc am I right ?

Here is my call :

this.nfcAdapter.enableReaderMode(this.activity, this, NfcAdapter.FLAG_READER_NFC_A, Bundle.EMPTY);

Any idea about why the behaviour is degraded ? Any hints to work toward ?

I plan on testing this to other devices in 5.1.1 to check if it is samsung related only or Lollipop based. Finding such devices might take some time.

Thanks.

Poutrathor
  • 1,990
  • 2
  • 20
  • 44
  • I've recently had problems with NFC on some Samsung devices. It may be an issue with Lollipop. Try an app from the Play store such as NFC Tools and see if it behaves the same. – Pztar Jan 28 '16 at 19:13
  • good idea, i tried it, but nfc tools is not able to read tags already approached to the phone, it needs you to "approach an nfc tag" to detect it. So it does not help me to solve my issue :( Can u upvote my question, I think I will need some visibility here :/ – Poutrathor Jan 28 '16 at 19:19
  • 1
    I can't say why in 5.1.1 what used to worked in 4.4.4 stopped working, but I can point you to an open source library that solves the issue, so you can check how they do it: https://github.com/fidesmo/nordpol This is the class that takes care of not having to re-tap the phone with the tag: https://github.com/fidesmo/nordpol/blob/d951708bcc876ef6fce25e77e0b2552833c04419/android/src/main/java/nordpol/TagArbiter.java – mictter Jan 29 '16 at 08:31
  • this promises much, good job to u or/and ur team for having implement that ! Question : "From an end user perspective, this class enables the end user to * present their device to the phone once and keep it there while * switching between different activities." does it includes the following behaviour : if the device (tag) is already there when the application starts, the tag can be used (no movement required) ? – Poutrathor Jan 29 '16 at 10:29
  • Hello, I deployed your test application HelloFidesmo on a 4.4.4 device and a 5.1.1 device. Indeed, your test application on 4.4.4 is able to read a tag when it is already there. But it fails just as mine to do so on a 5.1.1 device. Which issue exactly are you referring to when you say "an open source library that solves the issue" ? – Poutrathor Feb 01 '16 at 18:04

1 Answers1

0

I coded a fix tonight that solves my issue temporary.

You need a Samsung Knox licence to implement this fix (or to be root I guess).

The fix adds 2 to 4 seconds compared with my previous workflow to read and check a password against the card, which is quite significant. Thus it is only temporary. I will update with a better solution in time.

My code and Fidesmo's code both detect tag when already pressed against the phone for 4.4.4 but fails for 5.1.1. Funnily enough, if you lock/unlock the phone, the phone detects the tag and both applications (Fidesmo's and mine) receive the onTagDiscovered callback. That comes from Android "switching off" NFC when screen is off (security reasons I think). From this constatation, the fix is obvious :

Fix : stop and start NFC + set up a receiver to listen to the NFC turn on/off. Do whatever your implementation was doing before.

        IntentFilter filter = new IntentFilter("android.nfc.action.ADAPTER_STATE_CHANGED");
        BroadcastReceiver receiver = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {
                Toast.makeText(Start.this, "broadcast received : "+ NfcAdapter.getDefaultAdapter(Start.this).isEnabled(), Toast.LENGTH_SHORT).show();
                // My code for communication with NFC Card :
                nfcCardApi= new nfcCardApi(Start.this);
                nfcCardApi2= new APICardNFC(nfcCardApi);
                APICardNFC.initForNFC(Start.this, nfcTypeCard);
                APICardNFC.startWaitingCard();
                tvInfo.setText(getResources().getString(R.string.login_pass_card));
            }
        };
        registerReceiver(receiver, filter);

        // restart NFC to try to grab the tag :
        disableEnableNFC();

and disableEnableNFC() :

    protected void  disableEnableNFC() {

    DeviceSettingsPolicy mDeviceSettings = DeviceSettingsPolicy.getInstance(context);
    try {
        if ( mDeviceSettings.startNFC(false)) {
            if (Params.tagFgDebug && fgDebugLocal){Log.i(Params.tagGen, tagLocal + "mDeviceSettings.startNFC(false) : true NFC disable " );};
        } else {
            if (Params.tagFgDebug && fgDebugLocal){Log.i(Params.tagGen, tagLocal + "mDeviceSettings.startNFC(false) : false NFC disable FAILED " );};
        }

        if ( mDeviceSettings.startNFC(true)) {
            if (Params.tagFgDebug && fgDebugLocal){Log.i(Params.tagGen, tagLocal + "mDeviceSettings.startNFC(true) : true NFC enable " );};

        } else {
            if (Params.tagFgDebug && fgDebugLocal){Log.i(Params.tagGen, tagLocal + "mDeviceSettings.startNFC(true) : false NFC enable FAILED " );};
        }

    } catch (SecurityException e) {
        new TePVException(tagLocal, "disableEnableNFC", "SecurityException: " + e.getLocalizedMessage());
    }

}
Poutrathor
  • 1,990
  • 2
  • 20
  • 44