0

I'm having a weird problem. I'm having a Xamarin Android application and Enabling foreground dispatch for NFC Scanning like this:

Intent intent = new Intent(mainActivity.ApplicationContext, mainActivity.Class);
        intent.SetFlags(ActivityFlags.SingleTop);
        intent.PutExtra("NFCIntent", true);

        PendingIntent pendingIntent = PendingIntent.GetActivity(mainActivity.ApplicationContext, 0, intent, 0);

   
            nfcAdapter.EnableForegroundDispatch(mainActivity, pendingIntent, null, null);

So when the intent filter and tech list as null, it is expected that the OS dispatches the tag scanned to our application irrespective of the action (NDEF_DISCOVERED, TECH_DISCOVERED, TAF_DISCOVERED). I'm scanning the same NDEF tag multiple times. Most of the times the action is NDEF_DISCOVERED and the NFC tag is dispatched to our app via New Intent. However, sometimes the action is TECH_DISCOVERED (as per the Android device logs) and in this case, the NFC tag is not dispatched the our application and the default Android Tag scanned page is displayed.

I have also tried to add all the actions explicitly in the intent filter along with the tech list like below but the behavior is the same. It is also weird that I have 2 devices and this issue occurs only on one device

Intent intent = new Intent(mainActivity.ApplicationContext, mainActivity.Class);
        intent.SetFlags(ActivityFlags.SingleTop);
        intent.PutExtra("NFCIntent", true);
        var tagDetected = new IntentFilter(NfcAdapter.ActionTagDiscovered);
        var ndefDetected = new IntentFilter(NfcAdapter.ActionNdefDiscovered);
        var techDetected = new IntentFilter(NfcAdapter.ActionTechDiscovered);
        var filters = new[] { ndefDetected, tagDetected, techDetected };
        PendingIntent pendingIntent = PendingIntent.GetActivity(mainActivity.ApplicationContext, 0, intent, 0);

        var techlist = new string[][] {new string[]
        {
            "android.nfc.tech.IsoDep",
            "android.nfc.tech.NfcA",
            "android.nfc.tech.NfcB",
            "android.nfc.tech.NfcF",
            "android.nfc.tech.NfcV",
            "android.nfc.tech.Ndef",
            "android.nfc.tech.NdefFormatable",
            "android.nfc.tech.MifareClassic",
            "android.nfc.tech.MifareUltralight"
        }
        };

         NDEF action and MimeTypePlainText OnNewIntent
            nfcAdapter.EnableForegroundDispatch(mainActivity, pendingIntent, filters, techlist);

Any leads will be appreciated. Thanks in advance!

  • Have you tried using `enableReaderMode` instead of `enableForegroundDispatch`? this is a much better API and does not have some of the negative side effects of `enableForegroundDispatch` – Andrew Feb 02 '22 at 14:04
  • Thanks for the idea. Will try and update! – Sahana M G Feb 03 '22 at 07:39
  • My thought are as to why this is happening, different devices have different aerial shapes and chips (some with bugs). There are a number of possible reason for the failures, the TECH discovered instead of Ndef might because it failed to read the Ndef message before it went out of range because of a small aerial, etc. The undelivered Intent might be trying to deliver it when your app is not in the foreground due to being paused to receive another Intent. Some Qualcom chips have a bug of polling too frequently causing failures for longer operations. `enableReaderMode` helps with some of these. – Andrew Feb 03 '22 at 08:54

0 Answers0