0

I am new to NFC Android and I have been stuck for days trying to write in NTAG213 after set password to it
but it failed and i don`t know what happen can any one help me updated with more information about code

    // write PACK:
    mifareUltralight
        .writePage(
            pageOffset: 44, data: Uint8List.fromList([pack[0], pack[1], 0, 0]))
        .then((value) {
      setState(() {
        messageTXT = 'write PACK';
      });
    });

    // write PWD:
    mifareUltralight
        .writePage(
            pageOffset: 43,
            data: Uint8List.fromList([pwd[0], pwd[1], pwd[2], pwd[3]]))
        .then((value) {
      setState(() {
        messageTXT = 'write PWD';
      });
    });
    //set PROT
    bool prot =
        false; // false = PWD_AUTH for write only, true = PWD_AUTH for read and write
    int authlim = 0;
    // value between 0 and 7
    mifareUltralight
        .transceive(
            data: Uint8List.fromList([
      0xA2, // WRITE
      42, // page address
      ((object[0] & 0x078) | (prot ? 0x080 : 0x000) | (authlim & 0x007)),
      object[1], object[2], object[3]
    ]))
        .then((value) {
      setState(() {
        messageTXT = 'PWD_AUTH for write only0';
      });
    });

    // set AUTH0
    int auth0 = 0x00;
    // value between 0 and 7

    mifareUltralight
        .writePage(
            pageOffset: 41,
            data: Uint8List.fromList([
              object1[0], // keep old value for byte 0
              object1[1], // keep old value for byte 1
              object1[2],
              auth0
            ]))
        .then((value) {
      setState(() {
        messageTXT = 'PWD_AUTH for write only2';
      });
    });
  }

mifareUltralight
        .transceive(
            data: Uint8List.fromList([0x1B, pwd[0], pwd[1], pwd[2], pwd[3]]))
        .then((value) {
      if ((value != null) && (value.length >= 2)) {
        Ndef ndef = Ndef.from(tag);

        try {
          NdefMessage message = NdefMessage([
            NdefRecord.createUri(Uri.parse(url)),
          ]);
          ndef.write(message).then((value) {
            print('write success');
          });
        } catch (e) {
          print(e.toString());
        }
      }
    });
Andrew
  • 8,198
  • 2
  • 15
  • 35

1 Answers1

0

Update

Now that we see more code, the first place to start is that all these transceive commands will return a value if they are successful or not.

So it would be good to do error checking of all these commands sent to the tag, by checking the return valve to see which bit goes wrong.

From the datasheet for the Tag the ACK and NAK values are

Table 23.ACK and NAK valuesCode (4-bit) ACK/NAK
Ah Acknowledge (ACK)
0h NAK for invalid argument (i.e. invalid page address)
1h NAK for parity or CRC error
4h NAK for invalid authentication counter overflow
5h NAK for EEPROM write error

So the return value should always be Ah

Original

Just setting a password on the tag does not protect anything by default.

You also need to set the AUTH0 bytes (Byte 3 of page 0x29h on ntag213 - see datasheet of tag for details) as by default no memory pages are protected by the password you just set.

You need to change the value from protecting page 0xFFh and upwards (i.e no pages) to protecting at least page 0x04h (or 0x00h) and upwards to have any effect on access to NDEF data.

Changing the value Byte 3 of page 0x29h is done with a standard write command, so read page 0x29h, change the last byte of the 4 bytes read and write back to the page.

You should also be aware of the PROT bit of the Access byte memory address that changes between read and write for the password protection (default is password protect write access), this is also changed by a standard write command to the right memory page.

Andrew
  • 8,198
  • 2
  • 15
  • 35
  • Thank you for your answer....but I do that I set auth0 and set prot as default to can user password protection for write only. L.. What should I do now?! – Mahmoud Ayman Mar 21 '21 at 06:54
  • I think there might be a flaw in your code for testing for write access, based what I've seen in documentation of similar tags because your write test in the same session as an already authenticated (authenticated with no password) you are already classed as authenticated. The tag has to go back the to the `HALT` state before the password is checked again (so the tag needs to leave the field, I've not tried sending the `HALT` command directly or sending a bad command should send it to the `HALT` state). Try removing the Tag from the field then try a write test. – Andrew Mar 21 '21 at 07:50
  • please you can now show my code if any thing wrong please tell me..i updated it now – Mahmoud Ayman Mar 21 '21 at 08:28
  • i do it,the ACK value 10 == Ah,its mean avery thing alright?! – Mahmoud Ayman Mar 21 '21 at 13:00
  • E/flutter (27126): Unhandled Exception: PlatformException(io_exception, null, null, null)..i get this error when try to write after authenticated – Mahmoud Ayman Mar 21 '21 at 16:07
  • You need to check that when you use the authenticate command `0x1B` that you get back the expected PACK value. But as I said before I don't you can go from setting a new password to using the new in the same session i.e. the set password code and then attempt an authenticate command `0x1B` without the Tag being removed from the device. You do some logic so that when the code first sees the card it sets password and then when it sees it a second time it uses the new password. – Andrew Mar 21 '21 at 18:08
  • i try every thing , the password set success and authenticate done without any problem...but the problem here when try to write – Mahmoud Ayman Mar 21 '21 at 18:44
  • now i try idea and its working...the idea is if the tag authenticated done i set the password is 00x0ff and end session after 1 second start new session and write id write done successfully,add the password again...i know this solution not professional but its only solution works with my – Mahmoud Ayman Mar 21 '21 at 18:50
  • i have a question i hope can find answer here...how i can know if this tag has protected before?? – Mahmoud Ayman Mar 21 '21 at 22:41