How can I read an `ISO 14443-3A` NFC card using `nfc_manager` in Flutter?

687 Views Asked by At

I'm Currently running a test on an ISO 14443-3A NFC card, I'm using the nfc_manger from flutter's pub.dev, how do i read data from a tag like ISO 14443-3A and any other cardd supported by the device, that is my code below

if (ndef == null ||
            felica == null ||
            iso7816 == null ||
            iso15693 == null ||
            miFare == null ||
            nfca == null ||
            nfcb == null ||
            nfcf == null ||
            nfcv == null ||
            isoDep == null ||
            miFareClassic == null ||
            miFareUltralight == null ||
            ndefFormattable == null) {
          error = 'Tag is not Ndef Readable, switch to QR option';
          NfcManager.instance.stopSession(errorMessage: error);
          return;
        }

        // Handle NFC commands according to which NFC tag is been read
        if (ndef != null) {
          // read data from NDEF
          handleNDEF(ndef);
        } else if (felica != null) {
          // read Data from FeliCa Tag using commands
          handleFelica(felica);
        } else if (iso7816 != null) {
          // read Data from Iso7816 Tag using commands
          // handleIso7816(iso7816);
        } else if (iso15693 != null) {
          // read Data from Iso15693 Tag using commands
        } else if (miFare != null) {
          // read Data from MiFare Tag using commands
        } else if (nfca != null) {
          // read data from NfcA Tag using commands
          
        } else if (nfcb != null) {
          // read data from NfcB Tag using commands
        } else if (nfcf != null) {
          // read data from NfcF Tag using commands
        } else if (nfcv != null) {
          // read data from NfcV Tag using commands
        } else if (isoDep != null) {
          // read data from IsoDep Tag using commands
        } else if (miFareClassic != null) {
          // read data from MiFareClassic Tag using commands
        } else if (miFareUltralight != null) {
          // read data from MifareUltraLight using commmands
        } else if (ndefFormattable != null) {
          // Convert data into NDEF format and read data from NdefFormatable using NDEF
        }

i just need to know how to handle tags read data from the cart using the handleIso7816 methods, i've tried reading the documentation, but it's not enough, i'm given the method .transcieve() but i dont understand how it works

1

There are 1 best solutions below

2
MendelG On

To read an NFC card using nfc_manager, you can read the cachedMessage.

Setup

I initially had problems setting up the correct permissions for IOS, see if Flutter NFC Manager issue on ios helps you

Reading an NFC card

try {
      NfcManager.instance.startSession(onDiscovered: (NfcTag tag) async {
        final ndef = Ndef.from(tag);
        if (ndef == null) {
          print('Invalid card.');
          return;
        }

        final ndefRecords = ndef.cachedMessage?.records ?? [];
        for (var record in ndefRecords) {
          print('message: ${record.payload}');
          // assumes that the message is a list of integers representing valid UTF-16 code units. 
          // If you want to read the payload as a string, you can decode it here
          final cardMessage = String.fromCharCodes(record.payload);
          print('message: $cardMessage');
        }
      });
    } catch (e) {
      print('An error occurred: $e');
    }

Writing to an NFC card

  NfcManager.instance.startSession(
        onDiscovered: (NfcTag tag) async {
          final ndef = Ndef.from(tag);
          if (ndef != null && ndef.isWritable) {
            final payload = NdefRecord.createText('StackOverflow');

            final message = NdefMessage([payload]);
            await ndef.write(message);
            print('Successfully wrote message to the card');
          }
        },
      );
    } catch (e) {
      print('An error occurred: $e');
    }