I'm doing my NFC
writing in the AsyncTasks
doInBackground()
. When I write NdefRecord it works well but when I'm trying to make tag read only I'm getting IOException
. Here is the code where exception occurs:
if (readOnly && !ndef.canMakeReadOnly()) {
throw new NdefCantMakeReadOnlyException(R.string.cant_make_read_only);
} else if (readOnly) {
ndef.makeReadOnly(); //IOException
}
This makeReadOnly works with Mifare Ultralight (MF0ICU1) tags.
Browsing through the Android 4.4.2 source (I did not check with older versions) reveals that you will always get an
IOException
ifmakeReadOnly()
fails for whatever reason. So you found a bug in Android or at least a mismatch with the API documentation.The cause is that
android.nfc.tech.Ndef
(see here, starting on line 383) expects the NFC service to returnErrorCodes.SUCCESS
for successful locking,ErrorCodes.ERROR_INVALID_PARAM
for failed locking andErrorCodes.ERROR_IO
on any IO related error. However, the NFC service returns ErrorCodes.SUCCESS if locking succeeds (see here, line 1438) and ErrorCodes.ERROR_IO if locking fails for any reason (see here, line 1440).ErrorCodes.ERROR_INVALID_PARAM
seems not to be returned at all, thus themakeReadOnly()
method should typically never returnfalse
.