Java 使用NfcV tag.transecive写入标记

Java 使用NfcV tag.transecive写入标记,java,android,nfc,rfid,iso-15693,Java,Android,Nfc,Rfid,Iso 15693,我正在写一个关于NFC写作的应用程序。 我们叫它作家吧。。。我正在将数据写入NfcV标记 我试图编写的字符串是string test=“this\ta real\ttestcase\tyou标记” 为了写入数据,我使用了NfcV的transceive方法。 这就是我的写作方法: public static String bytesToHex(byte[] bytes) { StringBuilder sb = new StringBuilder(bytes.length * 2);

我正在写一个关于NFC写作的应用程序。 我们叫它作家吧。。。我正在将数据写入NfcV标记

我试图编写的字符串是
string test=“this\ta real\ttestcase\tyou标记”

为了写入数据,我使用了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;
}

要在最初的帖子中总结各种问题:

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

要在最初的帖子中总结各种问题:

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

要在最初的帖子中总结各种问题:

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

要在最初的帖子中总结各种问题:

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

    • 对于Infineon技术不知道,但是对于NXP ICODE和TI标签(ISO15693),存在
      获取系统信息
      命令。我将附加一个带有响应字节的图像。在此示例中,块数为0x3F+1。0x03+1是每个块的字节数。所以3f=63和03=3,总内存=(63+1)*(3+1)=256字节

      我这样使用它(我发送一个寻址命令,命令中包含标记id,但这不是必需的):


      不知道Infineon技术的情况,但对于NXP ICODE和TI标签(ISO15693),存在
      Get System Information
      命令。我将附加一个带有响应字节的图像。在本例中,n
      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
      
              nfcV = NfcV.get(tag);
              byte[] id = tag.getId();
      
              if (nfcV != null) {
      
                  byte[] infoCmd = new byte[2 + id.length];
                  // set "addressed" flag
                  infoCmd[0] = 0x20; 
                  // ISO 15693 Get System Information command byte
                  infoCmd[1] = 0x2B;
                  //adding the tag id
                  System.arraycopy(id, 0, infoCmd, 2, id.length); 
      
                  int memoryBlocks = null; 
                  try {
                      nfcV.connect();
                      byte[] data = nfcV.transceive(infoCmd);
      
                      memoryBlocks = Integer.parseInt(String.format("%02X", data[data.length-3]), 16);
      
                  }
                  catch (IOException e) {
                      e.printStackTrace();
                  } 
                  finally {
                      try {
                          nfcV.close();
                      } 
                      catch (IOException e) {
                          e.printStackTrace();
                      }
                  }