使用Android读取恩智浦ICODE SLI-L标签

时间:2013-03-28 18:32:24

标签: android nfc iso-15693

我正在尝试在我的Android应用程序中阅读恩智浦开发的NFC标签。可以使用Android阅读标记:App by NXPone other正确读取标记。

确切的标签类型是“ICODE SLI-L(SL2ICS50)”,RF技术是“Type V / ISO 15693”(从这些工作应用中获取的数据)。存储器由2个页面组成,每个页面有4个块,每个块有4个字节 - 我只想将整个数据存储在存储器中。

标记必须使用Android的NfcV类进行处理,标记的数据表为available here,但很难找到使用NfcV的任何有效代码示例。我尝试了几个由数据表自己总结的东西,我尝试了来自this PDF I found with Google的通信样本,但没有任何作用。

我的活动中的相应方法(我使用NFC Foreground Dispatch)如下所示:

public void onNewIntent(Intent intent) {
    android.nfc.Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    NfcV tech = NfcV.get(tag);
    try {
        tech.connect();
        byte[] arrByt = new byte[9];
        arrByt[0] = 0x02;
        arrByt[1] = (byte) 0xB0;
        arrByt[2] = 0x08;
        arrByt[3] = 0x00;
        arrByt[4] = 0x01;
        arrByt[5] = 0x04;
        arrByt[6] = 0x00;
        arrByt[7] = 0x02;
        arrByt[8] = 0x00;
        byte[] data = tech.transceive(arrByt);
        // Print data
        tech.close();
    } catch (IOException e) {
        // Exception handling
    }
}

当我将手机放在标记上时,会正确调用该方法,但transceive()对象的NfcV方法始终会抛出IOException:android.nfc.TagLostException: Tag was lost.。这是我尝试的所有字节数组的结果(上面的那个不太可能是正确的,但是在过去的几天里,我尝试了一堆其他所有这些都导致相同的行为。

根据我在互联网上看到的内容,我得出结论,错误的发生是因为我向标签发送了错误的命令 - 但我无法想出正确的命令。有什么想法吗?

1 个答案:

答案 0 :(得分:6)

ISO 15693定义了不同的读命令,制造商也可以定义专有的读命令。所有ICODE标签都支持ISO 15693单块读取命令。您可以按如下方式发送:

public static void processNfcIntent(Intent intent){
    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    if(tag != null){
      // set up read command buffer
      byte blockNo = 0; // block address
      byte[] readCmd = new byte[3 + id.length];
      readCmd[0] = 0x20; // set "address" flag (only send command to this tag)
      readCmd[1] = 0x20; // ISO 15693 Single Block Read command byte
      byte[] id = tag.getId();
      System.arraycopy(id, 0, readCmd, 2, id.length); // copy ID
      readCmd[2 + id.length] = blockNo; // 1 byte payload: block address

      NfcV tech = NfcV.get(tag);
      if (tech != null) {
        // send read command
        try {
          tech.connect();
          byte[] data = tech.transceive(readCmd); 
        } catch (IOException e) {
          e.printStackTrace();
        } finally {
          try {
            tech.close();
          } catch (IOException e) {
            e.printStackTrace();
          }
        }
      }
    }
}
相关问题