【问题标题】:Android NFC Cannot Write to Mifare DesFire CardAndroid NFC 无法写入 Mifare DesFire 卡
【发布时间】:2012-12-14 17:35:53
【问题描述】:

我正在尝试使用 Galaxy S3 将一些数据写入 Mifare DesFire 卡,其中包含以下几行:

private byte[] wrapMessage (byte command, byte[] parameters) throws Exception {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();

    stream.write((byte) 0x90);
    stream.write(command);
    stream.write((byte) 0x00);
    stream.write((byte) 0x00);
    if (parameters != null) {
        stream.write((byte) parameters.length);
        stream.write(parameters);
    }
    stream.write((byte) 0x00);

    return stream.toByteArray();
}

boolean isoDepWrite(Tag tag) {
      IsoDep idTag = IsoDep.get(tag);
      idTag.setTimeout(5000);

      String info = "";
      DesfireProtocol dfp = new DesfireProtocol(idTag);
      try {
          idTag.connect();
          info += "Connected to IsoDep Tag...\n";

          int[] appList = dfp.getAppList();
          dfp.selectApp(appList[0]);
          info += "Selected app no: " + appList[0] + "..\n";

          int[] fileList = dfp.getFileList();
          info += "Selected file no: " + fileList[0] + "\n";

          byte[] params = {(byte)fileList[0], 
                           (byte)0x0, (byte)0x0, (byte)0x0, 
                           (byte)0x2, (byte)0x0, (byte)0x0,
                           (byte)0x41, (byte)0x41};
          byte[] message = wrapMessage((byte) 0x3d, params);

          byte[] result = idTag.transceive(message);
          info += "Result bytes: " + convertByteArrayToHexString(result) + "\n";

          toast(info);
          return true;
      } catch (IOException e) {
          info += "Could not connect to IsoDep Tag...\n";
      } catch (Exception e) {
          info += "Error messages: " + e.getMessage() + " -- " + e.getLocalizedMessage() + "\n";
      }

      toast(info);
      return false;
  }

沟通后得到的信息是:

Connected to IsoDep tag...
Selected app no: 1109742 // that shows I connected to an Application
Transceieve result bytes: 91 9e  // PARAMETER ERROR

我可以连接并读取该应用程序的文件,但在我尝试写入后,文件中的字节为 0。 0x9E 是 PARAMETER_ERROR,所以我在包装/排列字节时做错了什么,有任何字节样本或想法吗?

编辑:我尝试了@nemo 推荐的字节:

{0x3d, fileList[0], 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x41, 0x41}

现在我得到“67 00”作为结果字节,这意味着 LENGTH ERROR 并且文件保持不变,只有 0。

最后编辑:我只是通过以下方式创建了一个新的字节数组:

wrapMessage(0x3d, rest of the bytes in the list @nemo recommended)

它终于奏效了。我用上面的工作改变了旧的。

【问题讨论】:

  • 您不能像这样打印result 字节数组。您需要遍历各个字节并一次打印一个。
  • 请检查您的文档,了解错误代码 0x9E 的含义。
  • 感谢您的快速回复。 9E 是 PARAMETER_ERROR。
  • 请查看您的文档以获取 WriteData 命令的确切详细信息。
  • 这就是我找不到的,字节的精确排列。我将尝试此链接中的字节:pastebin.com/UtQQZ1xN

标签: android nfc mifare


【解决方案1】:

我认为你的 Write 命令有误,但这是在黑暗中的一枪。

根据DESFire官方文档(尝试搜索M075031WriteData定义如下:

WriteData(FileNo, Offset, Length, Data)

作为一个字节流,它看起来像这样:

WriteCmd FileNo  Offset (3 byte)  Length (3 byte)  Data (0 to 52 byte)
[0x3D]   [0x00]  [0x00 0x00 0x00] [0x00 0x00 0x00] [0x00 ... 0x00]

甚至可以比 52 字节多写 59 字节,但这在这里并不重要。

IMO 你应该使用 WriteCmd 所需的数据创建一个新数组,如下所示:

{0x3d, fileList[0], 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x41, 0x41}

应该将 2 (0x2) 个字节(0x41 和 0x41)写入由fileList[0] 标识的文件。

编辑:更新偏移量,顺序为 LSB 到 MSB。

【讨论】:

  • 感谢您的努力。我尝试了您的字节流并得到 67 00 作为结果字节。但是,该文件仍然只有 0。
  • 有什么错误吗?同样的错误?你读过规范吗?介意更新问题吗?
  • 0x67 根据规范,是“错误长度”的错误代码。我可能弄错了长度的字节顺序,尝试摆弄它并报告它是否有效,请:)
  • 尝试了 0x2 MSB->LSB,但没有运气。你能写下你想要的这个规范吗?
  • 尝试了“0x20 0x0 0x0”、“0x0 0x0 0x2”、“0x0 0x0 0x02”,但又没有运气。尝试将读取的文件写回(长度为 15),还有“0xf0 0x0 0x0”、“0xf 0x0 0x0”、“0x0 0x0 0xf”和“0x0 0x0 0x0f”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多