flutter-nfc-manager icon indicating copy to clipboard operation
flutter-nfc-manager copied to clipboard

Question : Read NDEF from tag

Open Antoigne opened this issue 2 years ago • 1 comments

The problem is, when I read the tag, i'm not getting the 'Hello World!' String that I wrote on the tag. How I'm suppose to get that string? From the exemple :

void _tagRead() { NfcManager.instance.startSession(onDiscovered: (NfcTag tag) async { result.value = tag.data; NfcManager.instance.stopSession(); }); }

void _ndefWrite() { NfcManager.instance.startSession(onDiscovered: (NfcTag tag) async { var ndef = Ndef.from(tag); if (ndef == null || !ndef.isWritable) { result.value = 'Tag is not ndef writable'; NfcManager.instance.stopSession(errorMessage: result.value); return; }

  NdefMessage message = NdefMessage([
    NdefRecord.createText('Hello World!'),
    NdefRecord.createUri(Uri.parse('https://flutter.dev')),
    NdefRecord.createMime(
        'text/plain', Uint8List.fromList('Hello'.codeUnits)),
    NdefRecord.createExternal(
        'com.example', 'mytype', Uint8List.fromList('mydata'.codeUnits)),
  ]);

  try {
    await ndef.write(message);
    result.value = 'Success to "Ndef Write"';
    NfcManager.instance.stopSession();
  } catch (e) {
    result.value = e;
    NfcManager.instance.stopSession(errorMessage: result.value.toString());
    return;
  }
});

}

Antoigne avatar Aug 04 '23 15:08 Antoigne

You have to read the individual ndef-records of the message - you are writing 4 records to the tag - by reading the individual records you can find your "Hello World" This example shows how to do this: https://github.com/okadan/nfc-manager/blob/master/lib/view/tag_read.dart

philitell avatar Aug 10 '23 06:08 philitell