Ognyan
Ognyan

Reputation: 13600

How to disable NDEF tag discovery permanently in an Android app not just for an Activity?

We have an app that is used to simulate POS terminal, i.e. it communicates with debit/credit cards via NFC. In all the app activities I am using:

    nfcAdapter.enableReaderMode(this,
            nfcCallback,
            NfcAdapter.FLAG_READER_NFC_A |
                    NfcAdapter.FLAG_READER_NFC_B |
                    NfcAdapter.FLAG_READER_NFC_F |
                    NfcAdapter.FLAG_READER_NFC_V |
                    NfcAdapter.FLAG_READER_NO_PLATFORM_SOUNDS |
                    NfcAdapter.FLAG_READER_SKIP_NDEF_CHECK,
            null
    );

in order to disable NDEF discovery because it interferes with the card communication. It works well, but ONLY in the activity, i.e. when starting new activity or finishing one that was called with startActivityForResult there is obviously this intermediate state where the OS is sending one NDEF tag discovery message like:

2018-11-14 09:07:11.794 802-3109/? D/NxpNciX: len =  16 > 00000D00A4040007D276000085010100
2018-11-14 09:07:11.794 802-3109/? D/NxpTml: PN54X - I2C Write successful.....

The main problem is that this is not repeatable, i.e. sometimes it happens, sometimes not. This is a big problem.

Is there a way to disable the NDEF tag discovery on app level, not just for activity?

OR

Is there a workaround for this problem?

Upvotes: 3

Views: 945

Answers (1)

Inti
Inti

Reputation: 3751

A very late reply but noting it here for future searchers.

As of API 24 there is a new function on the nfcAdapter instance to ignore repeated scans of the same tag for a period of time.

Documentation

I have had success adding this after a successful nfc read inside the nfc callback (before switching Activity with startActivityForResult ).

// Ignore rescans of the same tag for 3 seconds in an attempt to avoid the tag being
// rescanned and having the default response while we are switching activities (while navigating)
// 3 seconds chosen as 1 second sometimes isn't enough and 3 seconds seems appropriate. Not too long or short...
nfcAdapter.ignore(tag, 3000, null, null);

Upvotes: 2

Related Questions