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

三世轮回 提交于 2019-12-11 12:12:04

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/21529387/why-im-getting-ioexception-when-making-nfc-tag-read-only

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!