I can write my data on card successfully. I want that no one can write on my card, but that they can only read. Is this possible to achieve? Or is there any key where I can assign some password protection before writing data on my card?
To write data using:
public void writeTag(Tag tag) {
MifareUltralight ultralight = MifareUltralight.get(tag);
String[] mg= getParts(s, 4);
try {
ultralight.connect();
int j=4;
for (int i = 0; i < mg.length; i++) {
ultralight.writePage(j, mg[i].getBytes(Charset.forName("US-ASCII")));
j++;
}
Toast.makeText(getApplicationContext(), "Write Done!", Toast.LENGTH_LONG).show()
} catch (IOException e) {
Log.e(TAG, "IOException while closing MifareUltralight...", e);
} finally {
try {
ultralight.close();
} catch (IOException e) {
Log.e(TAG, "IOException while closing MifareUltralight...", e);
}
}
}
Any help would be appreciated!
The lock bits for MIFARE Ultralight are located on bytes 2-3 in page 2 (note that Ultralight C and Ultralight derivates like NTAG may have additional lock bits on other pages, see the datasheet for their location). Each of the lock bits controls the lock state of certain pages of the memory area. In order to activate locking, you have to set the lock bit to '1'
by issuing a write command for the pages containing the lock bits. So for the simplest scenario of locking the whole tag, you could do something like this:
// write all-ones to the lock bits on page 0x02
byte[] result;
result = ultralight.transceive(new byte[]{
(byte)0xA2, // Command: WRITE
(byte)0x02, // Address: page 0x02 (2)
(byte)0x00, (byte)0x00, (byte)0xFF, (byte)0xFF // Data: set bytes 2-3 to all '1'
});