【问题标题】:Android application to read and write data in an nfcAndroid 应用程序在 nfc 中读取和写入数据
【发布时间】:2019-10-29 17:55:43
【问题描述】:

我是 NFC 新手,我正在开发一个 android 应用程序来在 nfc 中读取和写入数据,但我遇到了一些问题。

这是我正在使用的代码(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;
            }
        }
    }
}

当我在应用程序 TagInfo 中读取标签时,输出为:

[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 [。 . . .]

。 . .

这个输出正确吗?

如果'NOT',我哪里错了?

【问题讨论】:

  • 你真的需要使用NfcV格式吗?因为这在 Android 中的支持非常差,甚至某些 Android 手机中的某些硬件对这种格式的支持有限。如果您可以使用支持更好的 NDEF 格式。
  • 是的,我需要使用 NfcV !!
  • 我唯一能建议的就是你看看stackoverflow.com/a/37098162/2373819 并使用“命令的地址版本”

标签: android android-studio tags nfc iso-15693


【解决方案1】:

在我看来,这看起来不对,但不是 NfcV 专家只使用 NDEF nfc 卡。

[00] 。 41 42 43 44 [ABCD]

[01] 。 45 46 47 48 [EFGH]

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

正如你真正想要做的事情

我认为问题出在System.arraycopy(data, i, cmd, 11, 4);

您正在从源数据数组中复制 4 个字节的数据,但仅将起始位置增加 1 个字节的数据,因此下一个块稍后从字母开始。

我认为System.arraycopy(data, i*4, cmd, 11, 4); 会产生你想要的结果。

因为这会将源数据中 arraycopy 的开头增加您已存储的字节数。

由于你有 12 个字节的数据,每个块存储 4 个字节,你只需要使用 3 个块,所以只需通过设置 int blocks = 3; 循环 3 次,否则你将用完数据复制到 cmd 发送到卡从arraycopy生成IndexOutOfBoundsException

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

【讨论】:

  • 所以,我使用了System.arraycopy(data, i*4, cmd, 11, 4);,但它会导致IndexOutOfBoundsException。我不知道怎么解决!首先,我想在一个块中保存 12 个字节的数据。这可能吗?还是这些块只能接受 4 个字节?
  • 假设您的测试数据是 12 个字符,将 int blocks = 8; 更改为 int blocks = 3; 应该可以工作,但从长远来看,您需要计算出数据的大小以及需要多少块,并且然后调整最后一个块以匹配块大小。至于块大小,您需要标签上芯片的数据表或购买 V 型卡的 NFC 规范 (nfc-forum.org/our-work/specifications-and-application-documents/…)
  • 查看其他代码,这种类型的块是 4 个字节
  • 我认为st.com/content/ccc/resource/technical/document/application_note/…是这些卡的一个制造商芯片的规格,块大小可以是4或128字节,具体取决于芯片。
  • 我很难理解如何操作块。让我们假设,我想写入单个块 4 字节数据。在块[04]上写“abcd”,我该怎么做?我在哪里定义它?你能为我做一个示例代码吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多