How can I write to a MIFARE Classic tag?
I have written this code, but writeBlock
results in the error "java.io.IOException: transceive failed".
How can this be solved?
MifareClassic mfc = MifareClassic.get(mytag);
boolean auth = false;
mfc.connect();
auth = mfc.authenticateSectorWithKeyA(1,MifareClassic.KEY_DEFAULT);
if (auth) {
String text = "Hello, World!";
byte[] value = text.getBytes();
byte[] toWrite = new byte[MifareClassic.BLOCK_SIZE];
for (int i=0; i<MifareClassic.BLOCK_SIZE; i++) {
if (i < value.length) toWrite[i] = value[i];
else toWrite[i] = 0;
}
mfc.writeBlock(2, toWrite);
}