Android NFC读取ISO15693 RFID标签 [英] Android NFC read ISO15693 RFID Tag
问题描述
我正在尝试使用nfc android库读取ISO15693 RFID标签:
I am trying to read an ISO15693 RFID tag with the nfc android library:
有关该标记的更多信息: http://img42.com/gw07d+
Here is more info on the Tag: http://img42.com/gw07d+
可以正确读取标签ID,但不能正确读取标签中的数据.
The Tag ID is read correctly but the data in the tag is not.
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 }
)的单个字节.
userdata
is contains a single byte with value 0x02
({ 0x02 }
) right after the transceive method finished.
推荐答案
因此,您从transceive
方法中收到了{ 0x02 }
的值.如此线程中所述,当您使用未寻址的命令时,可能会发生这种情况.因此,您应该始终通过NfcV
发送已寻址的命令(因为Android设备上的所有NFC芯片组似乎都支持此命令).在您的情况下,您可以使用类似的方法来生成寻址的READ MULTIPLE BLOCKS命令:
So you receive a value of { 0x02 }
from the transceive
method. As found in this thread this may happen when you use unaddressed commands. Hence, you should always send addressed commands through NfcV
(as this seems to be supported across all NFC chipsets on Android devices). In your case, you could use something like this to generate an addressed READ MULTIPLE BLOCKS command:
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);
这篇关于Android NFC读取ISO15693 RFID标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!