【问题标题】:Android NFC read ISO15693 RFID TagAndroid NFC 读取 ISO15693 RFID 标签
【发布时间】:2015-02-09 08:27:55
【问题描述】:

我正在尝试使用 nfc android 库读取 ISO15693 RFID 标签:

这里是标签的更多信息:http://img42.com/gw07d+

标签ID被正确读取,但标签中的数据不正确。

onCreate方法:

// initialize NFC
    nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    nfcPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,   this.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

onNewIntent方法:

if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction()) || NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())) {

        currentTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        byte[] id = currentTag.getId();
        Tag_data_TextDisplay.setText("TagId:" + Common.getHexString(id));

        for (String tech : currentTag.getTechList()) {

            if (tech.equals(NfcV.class.getName())) {
                NfcV nfcvTag = NfcV.get(currentTag);

                try {
                    nfcvTag.connect();
                    txtType.setText("Hello NFC!");
                } catch (IOException e) {
                    Toast.makeText(getApplicationContext(), "Could not open a connection!", Toast.LENGTH_SHORT).show();
                    return;
                }

                try {
                    byte[] cmd = new byte[]{
                            (byte) 0x00, // Flags
                            (byte) 0x23, // Command: Read multiple blocks
                            (byte) 0x00, // First block (offset)
                            (byte) 0x04  // Number of blocks
                    };
                    byte[] userdata = nfcvTag.transceive(cmd);

                    userdata = Arrays.copyOfRange(userdata, 0, 32);
                    txtWrite.setText("DATA:" + Common.getHexString(userdata));

                } catch (IOException e) {
                    Toast.makeText(getApplicationContext(), "An error occurred while reading!", Toast.LENGTH_SHORT).show();
                    return;
                }
            }
        }
    }

userdata 在收发方法完成后包含一个值为 0x02 ({ 0x02 }) 的单个字节。

【问题讨论】:

  • 应该读什么?
  • @RandykaYudhistira 我在哪里读到标签img42.com/mHBPa+
  • 如果您能向我们展示nfcvTag.transceive(cmd); 的实际结果(不填充到 32 个字节),将会很有帮助。此外,您能否确保您的字节数组到十六进制转换将每个字节填充为两个十六进制数字(否则将难以解释该值)。
  • @MichaelRoland 我用字节到十六进制转换器更新了问题:这是 .transceive img42.com/7c30r+ 的结果
  • 你能说说你的普通班发生了什么吗?你能分享一下你的普通类的代码吗

标签: android nfc rfid iso-15693


【解决方案1】:

因此,您会从 transceive 方法中收到 { 0x02 } 的值。正如在this thread 中发现的那样,当您使用未寻址的命令时可能会发生这种情况。因此,您应该始终通过NfcV 发送寻址命令(因为这似乎在 Android 设备上的所有 NFC 芯片组中都受支持)。在你的情况下,你可以使用这样的东西来生成一个寻址的 READ MULTIPLE BLOCKS 命令:

int offset = 0;  // offset of first block to read
int blocks = 1;  // number of blocks to read
byte[] cmd = new byte[]{
        (byte)0x60,                  // flags: addressed (= UID field present)
        (byte)0x23,                  // command: READ MULTIPLE BLOCKS
        (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,  // placeholder for tag UID
        (byte)(offset & 0x0ff),      // first block number
        (byte)((blocks - 1) & 0x0ff) // number of blocks (-1 as 0x00 means one block)
};
System.arraycopy(id, 0, cmd, 2, 8);
byte[] response = nfcvTag.transceive(cmd);

【讨论】:

  • System.arraycopy(id, 0, cmd, 2, 8); 是做什么的?
  • 如 Java 文档中所述:“将数组 [id] 中的 [8] 个元素复制到数组 [cmd] 中,从偏移量 [0] 开始在偏移量 [2]。"
  • 在这种情况下,它会将标签(您之前使用byte[] id = currentTag.getId(); 检索到的)的 UID(地址)复制到命令帧中(在占位符字节上)。
猜你喜欢
  • 2022-09-30
  • 1970-01-01
  • 1970-01-01
  • 2015-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多