简体   繁体   English

Android 应用程序在 nfc 中读取和写入数据

[英]Android application to read and write data in an nfc

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.我是 NFC 新手,我正在开发一个 android 应用程序来读取和写入 nfc 中的数据,但我遇到了一些问题。

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:当我在应用程序 TagInfo 中读取标签时,output 是:

[00]. [00]。 41 42 43 44 [ABCD] 41 42 43 44 [ABCD]

[01]. [01]。 42 43 44 45 [BCDE] 42 43 44 45 [BCDE]

[02]. [02]。 43 44 45 46 [CDEF] 43 44 45 46 [CDEF]

[03]. [03]。 44 45 46 47 [DEFG] 44 45 46 47 [DEFG]

[04]. [04]。 45 46 47 48 [EFGH] 45 46 47 48 [EFGH]

[05]. [05]。 46 47 48 49 [FGHI] 46 47 48 49 [FGHI]

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

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

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

. . . . . .

Is this output correct?这个 output 是否正确?

If 'NOT', where am i going wrong?如果“不是”,我哪里错了?

To me this looks wrong but not an expert in NfcV only used NDEF nfc cards.对我来说,这看起来是错误的,但不是 NfcV 专家只使用 NDEF nfc 卡。

[00]. [00]。 41 42 43 44 [ABCD] 41 42 43 44 [ABCD]

[01]. [01]。 45 46 47 48 [EFGH] 45 46 47 48 [EFGH]

[02]. [02]。 49 4A 4B 4C [IJKL] 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);我认为问题出在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.您正在从源数据数组中复制 4 个字节的数据,但仅将起始 position 增加 1 个字节的数据,因此下一个块稍后从字母开始。

I think System.arraycopy(data, i*4, cmd, 11, 4);我认为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.因为这会将源数据中 arraycopy 的开头增加您已经存储的字节数。

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;由于您有 12 个字节的数据并且每个块存储 4 个字节,因此您只需要使用 3 个块,因此只需通过设置int blocks = 3; otherwise you will run out of data to copy in to cmd to send to the card generating IndexOutOfBoundsException from arraycopy否则你将用完数据复制到 cmd 发送到从arraycopy生成IndexOutOfBoundsException的卡

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.如果您没有 4 字节的倍数数据,则必须用零填充数据以成为 4 字节的倍数,或者处理来自arraycopyIndexOutOfBoundsException以正确复制剩余字节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM