M.Salomaa M.Salomaa - 1 year ago 85
Android Question

Why I'm getting IOException when making NFC tag read only

I'm doing my

writing in the
. When I write NdefRecord it works well but when I'm trying to make tag read only I'm getting
. 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.

Answer Source

Browsing through the Android 4.4.2 source (I did not check with older versions) reveals that you will always get an IOException if makeReadOnly() 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 return ErrorCodes.SUCCESS for successful locking, ErrorCodes.ERROR_INVALID_PARAM for failed locking and ErrorCodes.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 the makeReadOnly() method should typically never return false.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download