Platform Error when attempting to write to nfc tag

51 views Asked by At

im working on NFC using the nfc_manager package and mifare classic tags. On my onTagDiscovered method i call the following write method:

Future<void> _ndefWrite(NfcTag tag) async {

    final ndef = Ndef.from(tag);

    final formattable = NdefFormatable.from(tag);



    final message = NdefMessage([

      NdefRecord.createText('1'), // ID

      NdefRecord.createText('John'), // Name

      NdefRecord.createText('Doe'), // Surname

    ]);



    if (ndef != null) {

      if (!ndef.isWritable) {

        showErrorNotification(

            context: context, errorMessage: 'This tag is not writable.');

      }



      final tech = MifareClassic.from(tag);



      if (tech == null) {

        log('Tag is not Mifare Classic');

      }



      //define a list of access key A

      final listKeyDefault = [

        0xff,

        0xff,

        0xff,

        0xff,

        0xff,

        0xff,

      ];

      final dataKeyDefault = Uint8List.fromList(listKeyDefault);



      //define the data you want to write

      const String fullName = 'Sakhanya swartbooi';

      //encode the the data as  Uin8List

      final Uint8List data = utf8.encode(fullName) as Uint8List;



      try {

        tech?.authenticateSectorWithKeyA(sectorIndex: 2, key: dataKeyDefault);

        tech?.authenticateSectorWithKeyB(sectorIndex: 2, key: dataKeyDefault);



        await ndef.write(message);

        log('Successfully wrote to the Mifare Classic EV1 tag');

      } on PlatformException catch (e) {

        log(e.message ?? 'Some error has occurred.');

      } on Exception catch (e) {

        log(e.toString() ?? 'Some error has occurred.');

      }

    } else if (formattable != null) {

      try {

        final tech = MifareClassic.from(tag);



        if (tech == null) {

          log('Tag is not Mifare Classic');

        }



        //define a list of access key A

        final listKeyDefault = [

          0xff,

          0xff,

          0xff,

          0xff,

          0xff,

          0xff,

        ];

        final dataKeyDefault = Uint8List.fromList(listKeyDefault);

        tech?.authenticateSectorWithKeyA(sectorIndex: 2, key: dataKeyDefault);

        tech?.authenticateSectorWithKeyB(sectorIndex: 2, key: dataKeyDefault);

        await formattable.format(message);

      } on PlatformException catch (e) {

        log(e.toString());

        showErrorNotification(

            context: context, errorMessage: e.message.toString());

      }

    }

  }

When i hit the ndef.write line i get PlatformException (PlatformException(invalid_parameter, Tag is not found, null, null)), how can i fix this? The tag itself is writable and its empty, i tested it out first by using the NFC Tools app.

0

There are 0 answers