0

I'm new in NFC and i am developing an android application to read and write data in an nfc, but i'm having some problems.

it's code i'm using (WRITE):

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    if (intent.hasExtra(NfcAdapter.EXTRA_TAG)) {
        Toast.makeText(this, R.string.message_tag_detected, Toast.LENGTH_SHORT).show();
    }

    Tag currentTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    byte[] id = currentTag.getId();
    String myData = "ABCDEFGHIJKL";

    for (String tech : currentTag.getTechList()) {
        if (tech.equals(NfcV.class.getName())) {
            NfcV tag5 = NfcV.get(currentTag);
            try {
                tag5.connect();
                int offset = 0;  
                int blocks = 8;  
                byte[] data = myData.getBytes();
                byte[] cmd = new byte[] {
                        (byte)0x20,
                        (byte)0x21, 
                        (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, 
                        (byte)0x00,
                        (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00     
                };
                System.arraycopy(id, 0, cmd, 2, 8);

                for (int i = 0; i < blocks; ++i) {
                    cmd[10] = (byte)((offset + i) & 0x0ff);
                    System.arraycopy(data,  i, cmd, 11, 4);

                    response = tag5.transceive(cmd);
                }

            }
            catch (IOException e) {
                Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
                return;
            }
        }
    }
}

When i read a tag in app TagInfo, the output is:

[00] . 41 42 43 44 [ABCD]

[01] . 42 43 44 45 [BCDE]

[02] . 43 44 45 46 [CDEF]

[03] . 44 45 46 47 [DEFG]

[04] . 45 46 47 48 [EFGH]

[05] . 46 47 48 49 [FGHI]

[06] . 47 48 49 4A [GHIJ]

[07] . 48 49 4A 4B [HIJK]

[08] . 00 00 00 00 [. . . .]

. . .

Is this output correct?

If 'NOT', where am i going wrong?

cazzruan
  • 25
  • 6
  • Do you really need to use NfcV format? as this is very poorly supported in Android and even some of the Hardware in some Android phones have limited support for this format. If you can use the better supported NDEF format. – Andrew Oct 29 '19 at 21:34
  • Yes, i need use NfcV !! – cazzruan Oct 30 '19 at 12:11
  • The only thing I can suggest is you look at https://stackoverflow.com/a/37098162/2373819 and use "addressed version of commands" – Andrew Oct 30 '19 at 12:57

1 Answers1

0

To me this looks wrong but not an expert in NfcV only used NDEF nfc cards.

[00] . 41 42 43 44 [ABCD]

[01] . 45 46 47 48 [EFGH]

[02] . 49 4A 4B 4C [IJKL]

As the what actually your are wanting to do

I think the problem lies with System.arraycopy(data, i, cmd, 11, 4);

You are copying 4 bytes of data from your source data array but only incrementing the start position by 1 byte of data hence the next block start on letter later.

I think System.arraycopy(data, i*4, cmd, 11, 4); would produce the results you want.

As this increments the start of the arraycopy in the source data by the number of bytes you have already stored.

As you 12 bytes of data and each block stores 4 bytes you only need to use 3 blocks, so only loop 3 times by setting int blocks = 3; otherwise you will run out of data to copy in to cmd to send to the card generating IndexOutOfBoundsException from arraycopy

If you don't have a multiple of 4 bytes of data you will have to pad the data with zeros to be a multiple of 4 bytes OR handle a IndexOutOfBoundsException from arraycopy to correctly copy the remaining bytes.

Community
  • 1
  • 1
Andrew
  • 8,198
  • 2
  • 15
  • 35
  • So, i used `System.arraycopy(data, i*4, cmd, 11, 4);` but it causes an `IndexOutOfBoundsException`. i dont know how to solve! First, I would like to save 12 byte data in a block. Is this possible? or can the blocks only accept 4 bytes? – cazzruan Oct 30 '19 at 15:50
  • Given your test data is 12 characters changing `int blocks = 8; ` to `int blocks = 3;` should work, but in the long run you would need to work out the size of the data and how many blocks are needed and and then adjust the last block to match the block size. As to the block size, you would need the datasheet for the chip on the tag or purchase the NFC specification for V type cards (https://nfc-forum.org/our-work/specifications-and-application-documents/specifications/specification-releases/) – Andrew Oct 30 '19 at 16:33
  • Looking at some other code a block for this type is 4 bytes – Andrew Oct 30 '19 at 16:43
  • I think https://www.st.com/content/ccc/resource/technical/document/application_note/62/ec/78/b4/5b/00/49/67/DM00029842.pdf/files/DM00029842.pdf/jcr:content/translations/en.DM00029842.pdf is the spec of one manufacturers chips for these cards and the block size can be 4 or 128 bytes depending on the chip. – Andrew Oct 30 '19 at 16:48
  • I have difficulty understanding how to manipulate blocks. Lets go suppose, i want to write in a single block 4 byte data. write "abcd" on block[04], how would I do that? where do i define it? can u do a example code for me ? – cazzruan Oct 30 '19 at 16:48
  • Sorry cannot do an example, as I said before I really only do NDEF cards because anything else you have to know what you are doing with datasheets of the card in question and NFC specifications, BUT as you can see from https://stackoverflow.com/a/37098162/2373819 some of the bytes of the command you send are the block address to write the data to. – Andrew Oct 30 '19 at 16:56
  • You can also see from that ST doc I linked that some chips do 1 bytes addressing and some do 2 byte addressing – Andrew Oct 30 '19 at 17:04
  • i opened another question detailing my doubts better!! https://stackoverflow.com/q/58630929/12289446 , can u help me? – cazzruan Oct 31 '19 at 15:04
  • Yes I saw the question, but wanted to get this one answered before answering another. So does the change to the arraycopy and the block variable generate the data correctly? As I have no way of testing this and based on generic java and NFC knowledge gained when choosing to only handle NDEF cards in my App – Andrew Oct 31 '19 at 16:13
  • I don't know how to answer your question, the code I'm using, I took another post and used it in my project, I don't know what each line of code does, the only thing I understand is that it's writing in 8 blocks and each block 4 bytes. and I don't know how to read to analyze if it's correct – cazzruan Oct 31 '19 at 16:37
  • What I would expect to be correct is the Taginfo app to show the data as in the blockquote at the beginning of my answer. – Andrew Oct 31 '19 at 16:42
  • I was able to write and read the tag correctly!! thanks for the attention and guidance. Last thing I need to do is choose which block I want to write the data to, for example, write the data from block 05 – cazzruan Oct 31 '19 at 17:48
  • Please set the question as answered as you say it worked, so that other people know the info I gave was correct – Andrew Oct 31 '19 at 19:53