我正在尝试使用 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)
它终于奏效了。我用上面的工作改变了旧的。