1

I have ntag213. I'm trying to understand how work blocks, sections of memory of my tag. I found project https://github.com/lepunk/react-native-nfc-demo/blob/master/RNNFCDemo/App.js but cannot understand what is agruments for transceive method:

let text = this.state.text;
        let fullLength = text.length + 7;
        let payloadLength = text.length + 3;

        let cmd = Platform.OS === 'ios' ? NfcManager.sendMifareCommandIOS : NfcManager.transceive;

        resp = await cmd([0xA2, 0x04, 0x03, fullLength, 0xD1, 0x01]); // 0x0C is the length of the entry with all the fluff (bytes + 7)
        resp = await cmd([0xA2, 0x05, payloadLength, 0x54, 0x02, 0x65]); // 0x54 = T = Text block, 0x08 = length of string in bytes + 3

What it the arguments in every cmd?

user377055
  • 91
  • 1
  • 9

1 Answers1

1

You need to read Section 10 of the datasheet for the card e.g. https://www.nxp.com/docs/en/data-sheet/NTAG213_215_216.pdf

Basically with the write command (the 0xA2 first byte of the array), then you need to give it the block address, in first command this is block 4 and then block 5 (note block 4 is the first one you can write to) and then the next 4 bytes of data (as you can only write one 4 byte block at once).

So basically the commands are

[Write Command, Block Address, Data1 Byte, Data2 Byte, Data3 Byte, Data4 Byte]

So overall that code is encoding some text in a custom format to the card. Blocks 4 and 5 are header blocks for the custom format, then it writes the text in 4 byte chucks to blocks 6 onwards all with no checking that each write was a success (other than logging the response)

Update: to answer question

for

let currentPage = 6;
            let currentPayload = [0xA2, currentPage, 0x6E];

            for(let i=0; i<text.length; i++){
                currentPayload.push(text.charCodeAt(i));
                if (currentPayload.length == 6){
                    resp = await cmd(currentPayload);
                    currentPage += 1;
                    currentPayload = [0xA2, currentPage];
                }
            }

This loop starts at the next free page after the headers (page/block 6)

A valid transceive command is 6 bytes long as detailed before.

It defines a currentPayload as partial command of 3 bytes long.
Then it iterates over the text string in a loop adding characters one at a time until the transceive command reaches 6 bytes long and then sends it. After that it increases the page number and resets the base partial command to 2 bytes long.

payload.length == 6 is how it detects a write command has all the data it can take ( 1 byte command + 1 byte address + 4 bytes data = 6 bytes in length)

This is a method of splitting a variable length text string to fit in to multiple 6 byte write commands (which have 4 bytes of data).

On further thought this possibly looks like it is trying to write a NDEF text record but I would need to check the NDEF spec in more detail to confirm it.

Andrew
  • 8,198
  • 2
  • 15
  • 35
  • Can you also explain for loop here https://github.com/lepunk/react-native-nfc-demo/blob/master/RNNFCDemo/App.js ? Why it making cmd request every time when payload == 6? – user377055 May 19 '20 at 11:04
  • Updated to answer your question. – Andrew May 19 '20 at 11:41
  • Hello, can you explain how works authentication by password? as I understand firstly I need to authenticate first with command 0x1B to 0x2B and my password: 0xFF1FFFFF `let cmd = Platform.OS === 'ios' ? NfcManager.sendMifareCommandIOS : NfcManager.transceive; resp = await cmd(0x1B,0x2B,0xFF1FFFFF); ` But im getting error: https://i.imgur.com/rzfazYP.png – user377055 May 24 '20 at 16:46
  • I suggest you ask another question instead of trying to overload this one. – Andrew May 25 '20 at 08:39