Aftab Bhadki
Aftab Bhadki

Reputation: 181

Getting exception in NTAG213

I'm using the following code to set AUTH0 (first page that requires password verification) on an NTAG213 NFC tag:

try {
    result = nfca.transceive(new byte[]{
            (byte) 0xA2,  // Command: WRITE
            (byte) 0x29,  // Address: AUTH0
            (byte) 0x00   // starting address
    });
} catch (IOException e) {
    e.printStackTrace();
}

However, when I'm writing 00h (as starting address) on AUTH0, I always get the exception: "Transceive failed".

Could you tell me what could possibly go wrong here?

Upvotes: 2

Views: 519

Answers (1)

Michael Roland
Michael Roland

Reputation: 40831

NTAG213 (just as well as other NTAG and MIFARE Ultralight chips) use a page size of 4 bytes. The WRITE command (0xA2) can only be used to write a whole page. Thus, the data argument of the WRITE command needs to consist of 4 bytes.

The easiest way would be to just overwrite the whole configuration page:

result = nfca.transceive(new byte[]{
        (byte) 0xA2,  // Command: WRITE
        (byte) 0x29,  // Address: CONFIG0
        (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00
});

But keep in mind that this would also overwrite other configuration parameters (mirror byte and mirror page). If you want to set those other parameters to their default values, you could simply use this:

result = nfca.transceive(new byte[]{
        (byte) 0xA2,  // Command: WRITE
        (byte) 0x29,  // Address: CONFIG0
        (byte) 0x04, (byte) 0x00, (byte) 0x00, (byte) 0x00
});

However, if you want to leave those other values at what they currently contain, you might want to read the page first, and then use those values to update the page (only setting AUTH0 to 0x00):

byte[] currentData = nfca.transceive(new byte[]{
        (byte) 0x30,  // Command: READ
        (byte) 0x29,  // Address: CONFIG0
});

result = nfca.transceive(new byte[]{
        (byte) 0xA2,  // Command: WRITE
        (byte) 0x29,  // Address: CONFIG0
        currentData[0], currentData[1], currentData[2], (byte) 0x00
});

Upvotes: 1

Related Questions