我正在尝试使用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",意思是长度错误,文件仍然保持不变,只有0。 最后编辑:我只需创建一个新的字节数组:
wrapMessage(0x3d, rest of the bytes in the list @nemo recommended)
最终它起作用了。我用上述能工作的版本替换了旧版本。
result
字节数组。你需要循环遍历每个字节并一个一个地打印。 - NFC guy