0

I develop an app with NFC Tags interaction. Most of time application must ignore tag discovery events. And other apps should not catch tag discoery events when my app is foreground. So I used EnableForegroundDispatch to handle that.

This works fine. I handle excess TAG_DISCOVERED intents into my activity's onNewIntent methods.

The problem: When my activity is launched from another app, enableForegroundDispatch is not working. I receive TAG_DISCOVERED intents in new activity's onCreate method. Here is my foregroundDispatch

if (nfcAdapter == null) {
            Log.w(TAG, "enableForegroundNfcDispatch: no nfcAdapter", new Exception());
            return;
        }
        Log.d(TAG, "enableForegroundNfcDispatch: ");
        if (!nfcMonopolyMode) {
            if (nfcTagDiscoveryPendingIntent != null) {
                nfcAdapter.enableForegroundDispatch(this, nfcTagDiscoveryPendingIntent, nfcTagDiscoveryFilter, null);

                return;
            }
            IntentFilter discovery = new IntentFilter(ACTION_TAG_DISCOVERED);
            nfcTagDiscoveryFilter = new IntentFilter[]{discovery};

            Intent nfcProcessIntent = new Intent(getBaseContext(), getClass())
                    .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);


            nfcTagDiscoveryPendingIntent = PendingIntent.getActivity(getApplicationContext(), REQUEST_CODE_NFC_DISCOVERED_TAG, nfcProcessIntent, 0);

            nfcMonopolyMode = true;
            nfcAdapter.enableForegroundDispatch(this, nfcTagDiscoveryPendingIntent, nfcTagDiscoveryFilter, null);
        }

May be there is something to do with flags? Please help

  • In what method do you enable enableForegroundDispatch? OnResume, OnCreate? Plus I would never use enableForegroundDispatch unless targeting below API 19 `enableReaderMode` is a much better API – Andrew May 25 '20 at 19:10
  • Hi Andrew! Thank you for your answer! I use EnableForegroundDispatch only to AVOID other apps to catch TAG_DISCOVERED intent. For reading tag purposes I use enableReaderMode. I use foregroundDispatch inside onResume – Anton Klimenko May 26 '20 at 08:10
  • My experience is that if you `enableReaderMode` with all the flags (including skip NDEF check) then basically detection of any card type is sent to your App if running in the foreground. As well as in `onResume` I have Broadcaster Receiver to `enableReaderMode` of NFC service State change. Checking a number of Different Tags this seems to work BUT they are all NfcA based of varying formats. As confirmed by https://stackoverflow.com/questions/33633736/whats-the-difference-between-enablereadermode-and-enableforegrounddispatch but it seems Android 10 might have a timing issues with Kiosk Mode – Andrew May 26 '20 at 11:21
  • Andrew thanks. It works for me too. Moved to nfcAdapter.enableReaderMode inside onResume method and everything became fine. How can I mark your answer as answer here? – Anton Klimenko May 27 '20 at 08:35
  • Moved it to an answer for you. – Andrew May 27 '20 at 09:43

1 Answers1

0

My experience is that if you enableReaderMode with all the flags (including skip NDEF check) then basically detection of any card type is sent to your App if running in the foreground.

As well as enableReaderModein onResume I have Broadcaster Receiver to enableReaderMode of NFC service State change.

Checking a number of Different Tags this seems to work BUT they are all NfcA based of varying formats. As confirmed by stackoverflow.com/questions/33633736/… but it seems Android 10 might have a timing issues with Kiosk Mode

Andrew
  • 8,198
  • 2
  • 15
  • 35