flutterflutter-dependenciesnfc

How do i read NTAG424 NFC Tags in my flutter app, i keep getting an "NDEF not found error"


I am working on a flutter project using flutter_nfc_kit to scan(read) an nfc tag for a uri payload and then make use of the payload for other activities i am not doing any writing, the tag type in focus is the NTAG424 NFC TAG, i have no issues when working with TYPE 2 TAGS.

The problem is the functionality works for Type 2 tags (NTAG213, 216 etc), but when i try to scan a NTAG424 tag it returns an "ndef not found error", I use flutter_nfc_kit: ^3.5.2, i have tested with nfc tools like "NFC tools" and "NXP TagWriter", so i confirm the tag is functional and i can see the encoded uri. Below is the relevant code for the scanning functionality.

Future<void> _startNfcScan(BuildContext context) async {
  try {
    // Show the bottom sheet for Android (iOS already has a native NFC prompt)
    if (!Platform.isIOS) {
      await _showReadyToScanSheet(context);
    }

    // Start the NFC polling session
    var tag = await FlutterNfcKit.poll(timeout: Duration(seconds: 10));

    // Check if NDEF is available and read NDEF content
    if (tag.ndefAvailable == true) {
      // Read decoded NDEF records
      List<ndef.NDEFRecord> ndefRecords = await FlutterNfcKit.readNDEFRecords();

      if (ndefRecords.isNotEmpty) {
        var firstRecord = ndefRecords.first;

        // Ensure the payload is not null
        if (firstRecord.payload != null && firstRecord.payload!.isNotEmpty) {
          // Manually decode the compressed URI
          String decodedUrl = _decodeUriPayload(firstRecord.payload!);

          // Log or print the full decoded URL
          print('Decoded URL from NFC tag: $decodedUrl');

          // Now you have the full URL,do something with it
          await _doSomething(context, decodedUrl);

          // Show the "Scan Complete" bottom sheet after scanning
          if (!Platform.isIOS) {
            await _showScanCompleteSheet(context);
          }
        } else {
          ScaffoldMessenger.of(context).showSnackBar(
            SnackBar(content: Text('No payload found on the NFC tag.')),
          );
        }
      } else {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: Text('No NDEF records found on the tag.')),
        );
      }
    } else {
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text('NDEF not available on this tag.')),
      );
    }
  } catch (e) {
    // Handle errors during NFC scanning
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(content: Text('Failed to read NFC tag: $e')),
    );
  } finally {
    // Finish the NFC session
    await FlutterNfcKit.finish(iosAlertMessage: 'Scan Complete');
  }
}

String _decodeUriPayload(List<int> payload) {
  // The first byte in the URI payload is the prefix byte
  final int prefixByte = payload[0];

  // The rest of the payload is the actual URI content
  final uriContent = utf8.decode(payload.sublist(1));

  // Map the prefix byte to the correct URL scheme
  const prefixMap = {
    0x00: '', // No prefix
    0x01: 'http://www.',
    0x02: 'https://www.',
    0x03: 'http://',
    0x04: 'https://',
    // 
  };

  // Get the prefix based on the prefix byte
  final prefix = prefixMap[prefixByte] ?? '';

  // Return the full URL by combining the prefix with the URI content
  return prefix + uriContent;
}


Solution

  • I'm posting this as an answer since this is what worked for me:

    So it seems to be more of an incomplete setup issue, creaed an article on it for those interested in the full story. https://medium.com/@timidev34/solving-the-ndef-not-available-error-when-working-with-ntag424-nfc-tags-on-ios-in-flutter-26928edd717f