0

Moving from the enableForgroundDispatch (I don't seem to get it working on api 32) intent based way of reading NFC-tags to enableReaderMode. The latter one works well, but every now and then the NFC Service app is opening, showing 'emtpy tag' message. Is there a way to disable this, in code? It's true that the tags are empty, only reading and using the tag Id. Code look like the following;

  Bundle opts = new Bundle();
  opts.putInt(NfcAdapter.EXTRA_READER_PRESENCE_CHECK_DELAY, 250);
  getNfcAdapter().enableReaderMode(this, tag -> {
            // passing tag to get tag.getId()   
            myViewModel.setTag(tag);

  }, NfcAdapter.FLAG_READER_NFC_A |
     NfcAdapter.FLAG_READER_NFC_B |
     NfcAdapter.FLAG_READER_NFC_F |
     NfcAdapter.FLAG_READER_NFC_V |
     NfcAdapter.FLAG_READER_NFC_BARCODE |
     NfcAdapter.FLAG_READER_SKIP_NDEF_CHECK |
     NfcAdapter.FLAG_READER_NO_PLATFORM_SOUNDS
  , opts);
Dan
  • 173
  • 2
  • 18
  • 1
    I use `enableReaderMode` in my app and scanned 1000's of tags myself and my user have scanned tens of thousand more and never had this problem. What OS version are you seeing it on, I guess android 12 where as I've only used it on Android 8,9 and 10. Also when do you enable/disable this in your App (`onResume`/`ònPause`)? as like `enableForgroundDispatch` `enableReaderMode` only works when you app is in the Foreground and if the System NFC service does not think your app is in the Foreground then it will find another App or show some Tag details itself. – Andrew Oct 20 '22 at 17:55
  • I'm using targetSdkVersion 32 and I've seen this issue so far on Android 8.1 and Android 13. I'm enabling i `onResume` and disable in `onPause`. The old version of the app was using `enableForgroundDispatch` with targetSdkVersion 30, didn't see the problem there. – Dan Oct 20 '22 at 18:36
  • The reason for the old `enableForgroundDispatch`solution not working was that I'd set the `PendingIntent` flag `PendingIntent.FLAG_IMMUTABLE` instead of `PendingIntent.FLAG_MUTABLE` for sdk 32! Didn't realize `FLAG_MUTABLE` was default before sdk 31 when not set. Now it's working again and no service app is triggered occasionally when scanning a tag. When using `enableReaderMode` maybe you'd have to use it together with `enableForgroundDispatch` in my case, as suggested in some older posts. – Dan Oct 21 '22 at 08:46

0 Answers0