gpt4 book ai didi

java - 使用 NfcV tag.transceive 写入标签

转载 作者:行者123 更新时间:2023-12-01 23:27:32 25 4
gpt4 key购买 nike

我正在开发一个有关 NFC 写入的应用程序。我们称之为 Writer...我正在将数据写入 NfcV 标签。

我试图写入的字符串是String test = "this is\ta real\ttestcase\tyou tag";

为了写入数据,我使用 NfcV 的 transceive 方法。这就是我的编写方法:

public static String bytesToHex(byte[] bytes) {
StringBuilder sb = new StringBuilder(bytes.length * 2);
Formatter formatter = new Formatter(sb);
for (byte b : bytes) {
formatter.format("%02X", b);
}
formatter.close();
return sb.toString();
}

public void write(View v) {
try {
String[] tagInfoArray = new String[tagData.size()];
for (int i = 0; i < tagData.size(); i++)
tagInfoArray[i] = tagData.get(i).getText().toString();

String tagInfo = join(tagInfoArray, "\t");
//String test = "this is\ta real\ttestcase\tyou tag";
writeTag(tag, tagInfo);
} catch (NullPointerException e) {
Toast.makeText(this, "How about providing a tag!",
Toast.LENGTH_LONG).show();
} finally {
write.setBackgroundResource(R.layout.bluebutton);
}
}

public String join(String[] input, String delim) {
String output = "";
if (input.length > 0)
output += input[0];
if (input.length > 1)
for (int i = 1; i < input.length; i++)
output += delim + input[i];
return output;
}

public void exitButton(View v) {
this.foreground.disableForeground();
System.exit(0);
}

public void writeTag(Tag tag, String data) {
NfcV myTag = NfcV.get(tag);
try {
myTag.connect();
if (myTag.isConnected()) {
byte[] info = data.getBytes();
int dataLength = info.length;
if (data.length()/4 <= 64){
byte[] args = new byte[15];
args[0] = 0x20;
args[1] = 0x21;
byte[] id = tag.getId();
for (int o=0; o<8; o++)
args[o+2] = id[o];
for (int i = 0; i<64; i++) {
args[10] = (byte) i;
args[11] = 0x00;
args[12] = 0x00;
args[13] = 0x00;
args[14] = 0x00;
byte[] out = myTag.transceive(args);
String out2 = bytesToHex(out);
System.out.println("1:.. " + printHex(out2));
}
for (int i = 0; i<=dataLength/4; i++) {
args[10] = (byte) i;
args[11] = getByte(info, (i*4)+0);
args[12] = getByte(info, (i*4)+1);
args[13] = getByte(info, (i*4)+2);
args[14] = getByte(info, (i*4)+3);
byte[] out = myTag.transceive(args);
String out2 = bytesToHex(out);
System.out.println("2:.. " + printHex(out2));
}
}
}
} catch (IOException e) {
System.out.println(e.getMessage());
} finally {
if (myTag != null) {
try {
myTag.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
}

public static byte getByte(byte[] input, int key){
try {
return input[key];
} catch (Exception e){
return (byte)0x00;
}
}

public String printByte(byte[] input){
try {
return new String(input, "UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}

public String printHex(String input){
return input;
}

所以当我写东西的时候,结果并不是我所期望的。它要么根本不写入,要么只写入部分内容,但不会覆盖标签上之前的内容。

这是输出:

11-05 15:32:33.139: I/System.out(1390): 1:.. 00
11-05 15:32:33.249: I/System.out(1390): 1:.. 00
11-05 15:32:33.349: I/System.out(1390): 1:.. 00
11-05 15:32:33.449: I/System.out(1390): 1:.. 00
11-05 15:32:33.549: I/System.out(1390): 1:.. 00
11-05 15:32:33.649: I/System.out(1390): 1:.. 00
11-05 15:32:33.759: I/System.out(1390): 1:.. 00
11-05 15:32:33.859: I/System.out(1390): 1:.. 00
11-05 15:32:33.959: I/System.out(1390): 1:.. 00
11-05 15:32:34.059: I/System.out(1390): 1:.. 00
11-05 15:32:34.159: I/System.out(1390): 1:.. 00
11-05 15:32:34.259: I/System.out(1390): 1:.. 00
11-05 15:32:34.359: I/System.out(1390): 1:.. 00
11-05 15:32:34.469: I/System.out(1390): 1:.. 00
11-05 15:32:34.569: I/System.out(1390): 1:.. 00
11-05 15:32:34.669: I/System.out(1390): 1:.. 00
11-05 15:32:34.769: I/System.out(1390): 1:.. 00
11-05 15:32:34.869: I/System.out(1390): 1:.. 00
11-05 15:32:34.979: I/System.out(1390): 1:.. 00
11-05 15:32:35.079: I/System.out(1390): 1:.. 00
11-05 15:32:35.179: I/System.out(1390): 1:.. 00
11-05 15:32:35.289: I/System.out(1390): 1:.. 00
11-05 15:32:35.389: I/System.out(1390): 1:.. 00
11-05 15:32:35.489: I/System.out(1390): 1:.. 00
11-05 15:32:35.589: I/System.out(1390): 1:.. 00
11-05 15:32:35.689: I/System.out(1390): 1:.. 00
11-05 15:32:35.789: I/System.out(1390): 1:.. 00
11-05 15:32:35.889: I/System.out(1390): 1:.. 00
11-05 15:32:35.989: I/System.out(1390): Transceive failed

最佳答案

总结第一篇文章中的各种问题:

  • 从超出标记大小的 block 读取或写入将导致标记停止响应而导致 IOException。
  • NfcV.getMaxTransceiveLength() 返回有关在一个命令/一个响应中可以交换的最大字节数的信息。它提供有关标签大小的信息!
  • 不幸的是,没有一种通用的方法来检测标签的实际大小。一种可能的方法是尝试检测标签的类型(许多 NfcV 标签将确切的标签类型编码到其 ID 中,因此您可以使用 Tag.getId() 来获取 ID 并根据标签制造商的数据表)。另一种方法是读取直到第一个 IOException 并从该信息推断标签大小。您可以稍后重新连接标签以继续读取/写入。但是,请记住,任何其他通信中断(例如,用户未正确扫描标签)也可能导致 IOExceptions,并可能因此伪造您的估计标签大小。
  • ISO/IEC 15693 读/写单 block 命令的 block 索引参数以 block 为单位,而不是以字节为单位。

关于java - 使用 NfcV tag.transceive 写入标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19770400/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com