Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top