Android NFC无法写入Mifare DesFire卡
我正在尝试将一些数据写入带有Galaxy S3的Mifare DesFire卡,其中包含以下行:Android NFC无法写入Mifare DesFire卡,android,nfc,mifare,Android,Nfc,Mifare,我正在尝试将一些数据写入带有Galaxy S3的Mifare DesFire卡,其中包含以下行: private byte[] wrapMessage (byte command, byte[] parameters) throws Exception { ByteArrayOutputStream stream = new ByteArrayOutputStream(); stream.write((byte) 0x90); stream.write(command)
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是参数_错误,所以我在包装/排列字节时出错了,有没有字节的样本或想法
编辑:我尝试了@nemo推荐的字节数:
{0x3d, fileList[0], 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x41, 0x41}
现在我得到“6700”作为结果字节,这意味着长度错误,文件保持不变,只有0个
上次编辑:我只是通过以下方式创建了一个新的字节数组:
wrapMessage(0x3d, rest of the bytes in the list @nemo recommended)
它终于成功了。我用上面的操作更改了旧的命令。我认为您的
Write
命令有误,但这是瞎猜
根据官方的DESFire文档(尝试搜索M075031
)WriteData
定义如下:
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]
{0x3d, fileList[0], 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x41, 0x41}
甚至可以比52字节多写59字节,但这在这里并不重要
在我看来,您应该为WriteCmd创建一个包含所需数据的新数组,如下所示:
WriteCmd FileNo Offset (3 byte) Length (3 byte) Data (0 to 52 byte)
[0x3D] [0x00] [0x00 0x00 0x00] [0x00 0x00 0x00] [0x00 ... 0x00]
{0x3d, fileList[0], 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x41, 0x41}
它应该向文件列表[0]
标识的文件写入2(0x2)个字节(0x41和0x41)
编辑:更新的偏移量,顺序是LSB到MSB。您不能像那样打印
结果
字节数组。您需要循环遍历各个字节,然后一次打印一个。请检查您的文档,了解错误代码0x9E的含义。感谢您的快速响应。9E是参数_错误。请检查您的文档以了解WriteData命令的确切详细信息。这是我找不到的,字节的确切排列。我将尝试这个链接中的字节:谢谢你的努力。我尝试了你的字节流,结果得到了6700个字节。但是,该文件仍然只有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”。