我们有一些过去使用 RFID 阅读器读取的 ISO15693 标签。今天我开始在 Android 上开发一个示例应用程序,以使用NfcV
Android 6 (API 23) 读取相同的标签。
我能够从标签中读取一些数据,但数据中有一些意外字符。这是我使用的代码:
private void readTagData(Tag tag) throws Exception {
byte[] id = tag.getId();
String strTag = new String(id, "UTF-8");
boolean techFound = false;
for (String tech : tag.getTechList()) {
if (tech.equals(NfcV.class.getName())) {
techFound = true;
NfcV nfcvTag = NfcV.get(tag);
try {
nfcvTag.connect();
} catch (IOException e) {
Toast.makeText(this, "IO Exception", Toast.LENGTH_LONG).show();
return;
}
try {
int offset = 0;
int blocks = 19;
byte[] cmd = new byte[]{
(byte)0x60,
(byte)0x23,
(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, // placeholder for tag UID
(byte)(offset & 0x0ff),
(byte)((blocks - 1) & 0x0ff)
};
System.arraycopy(id, 0, cmd, 2, 8);
byte[] response = nfcvTag.transceive(cmd);
response = Arrays.copyOfRange(response, 0, 96);
String strData = new String(response, "UTF-8");
mTextView.setText("TAG:" + strTag + " DATA:" + strData);
} catch (IOException e) {
Toast.makeText(this, "An error occurred while reading", Toast.LENGTH_SHORT).show();
return;
}
try {
nfcvTag.close();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "Unable to close the connection!", Toast.LENGTH_SHORT).show();
return;
}
}
}
}
输出
标签 ID(UTF-8 解码):{��WP�</p>
数据(UTF-8 解码):����1ead��1234��5678��5000��00B1��2345��6181��5064��1602��2016��1603��2016��1602。 �2018��0011��8899��0002��0920��16����
十六进制表示的数据字节:
0000316561640031 3233340035363738 0035303030003030 4231003233343500 3631383100353036 3400313630320032 3031360031363033 0032303136003136 3032003230313800 3030313100383839 3900303030320030 3932300031360000
现在部分数据是正确的,但我不确定为什么这些“�”字符在那里。标签 ID 也不正确。
另外,我尝试将字节数组“响应”和标记 ID 转换为十六进制字符串,然后转换为具有相同结果的 ASCII。