I'm new in NFC Technology!
I have difficulty understanding how to manipulate blocks.
byte[] cmd = new byte[] {
(byte)0x20, //FLAG
(byte)0x21, //WRITE SINGLE BLOCK COMMAND
(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, //UID
(byte)0x00, //OFFSET
(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 //DATA
};
My questions about code above:
What is a FLAG and what is its function?
What is a UID and what is its function? in code the UID line has 8 "index", is it possible to increase or decrease the size? instead of 8 as in code, decrease to 6 or increase to 10?
What is a OFFSET and what is its function?
In line 6 of the commented code as DATA, is this where I define the byte size of my block? In code, it has 4 indexes, does it mean that my data stored in the block will have 4 bytes? can i increase or decrease?
Lets go suppose, i have a 4 bytes data myData = "ABCD"
, and i want to write this data to block 04 of my tag, how would I do it according to the code mentioned above?
I'm not an NfcV expert but below is what I have learnt about Tags and low level access
What is the Flag
byte mean? - Unknown BUT http://www.ti.com/lit/an/sloa141/sloa141.pdf Section 4.1 has details of ISO 15693 Flag meanings
But one of the Flag means using addressed or un-addressed mode, this leads on to the UID
What is UID
bytes - Most tags have a serial number or Unique IDentifier number
In addressed mode you have to provide the correct UID of the card you are reading or writing from for it to succeed. This means you won't write or reading from the wrong card. There is a command to read the UID from the card first.
In un-addressed mode the UID is provided as zeros
You have worked out the the second byte is 0x21
for an write command.
0x20
for read command
http://www.ti.com/lit/an/sloa141/sloa141.pdf Section 4.2 has details of ISO 15693 Command values and as you can see must of them are Optional
or Custom
and support and what they do depends on the chip.
The OFFSET
as you put it is the memory block offset from the first block or better described as the memory address (think of this like the page number in a book). Most chips split the memory in to blocks of a set size. There are some chips that use a single byte as the memory address and there are some that use 2 bytes.
Each block is a set number of bytes, it's common for it to be 4 bytes, but I've seen chip specs where it is 128 bytes.
The data structure you gave in your question is usually used as a template of a well formed command for chip you are trying to communicate to.
The DATA
4 bytes in your example are just place holders for the actual data your want to write, you should copy in to the template the actual 4 bytes you want to write before the send the command.
So when you are using it for write you have to adjust the OFFSET
/Memory Address
to be the "right page of the book" and copy in the right number of "letters" that can be written on the page in to the DATA
part of the template
Some examples of NfcV code for Android from a chip manufacturer can be seen at the end of https://www.st.com/content/ccc/resource/technical/document/application_note/group0/76/0e/00/a0/1b/04/4c/f2/DM00103491/files/DM00103491.pdf/jcr:content/translations/en.DM00103491.pdf
So the last question Lets go suppose, i have a 4 bytes data myData = "ABCD", and i want to write this data to block 04 of my tag
example of constructing the command
// Command Template
byte[] cmd = new byte[] {
(byte)0x20, //FLAG
(byte)0x21, //WRITE SINGLE BLOCK COMMAND
(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, //UID
(byte)0x00, //OFFSET
(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 //DATA
};
// The data to be written
String myData = "ABCD";
// Get the data as bytes
byte[] data = myData.getBytes();
// Change the "OFFSET" / "Block number" to the the fourth Block
// If that what was meant by "block 04"
// The addresses start at Zero and the byte array starts at zero
// So the "Block Number" is the 11th byte in the command
cmd[10] = (byte)((3) & 0x0ff);
// Copy in 4 bytes of data in to bytes 11 to 15
// Starting at byte 0 in the data array
System.arraycopy(data, 0, cmd, 11, 4);
For Reference what the arraycopy
Parameters https://docs.oracle.com/javase/7/docs/api/java/lang/System.html#arraycopy(java.lang.Object,%20int,%20java.lang.Object,%20int,%20int)