为什么我收到IOException异常时,使NFC标签只读 [英] Why I'm getting IOException when making NFC tag read only
问题描述
我做的AsyncTasks doInBackground我NFC写作。当我写NdefRecord效果很好,但是当我试图让只读我得到IOException异常标记。这里是异常发生时code:
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
}
这makeReadOnly适用于超轻的Mifare(MF0ICU1)标签。
This makeReadOnly works with Mifare Ultralight (MF0ICU1) tags.
推荐答案
浏览通过Android 4.4.2源(我没有与旧版本检查)显示,你总是会得到一个 IOException异常
如果 makeReadOnly()
失败,无论出于何种原因。所以,你发现了一个bug在Android或至少与API文档不匹配。
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.
原因是 android.nfc.tech.Ndef
(见<一href=\"https://android.googlesource.com/platform/frameworks/base/+/android-4.4.2_r1/core/java/android/nfc/tech/Ndef.java\"相对=nofollow>此处,开始在线383)预计NFC的服务来回报错误codes.SUCCESS
成功锁定,错误codes.ERROR_INVALID_PARAM
失败的锁定和错误codes.ERROR_IO
上的任何IO相关的错误。但是,如果锁定成功的NFC服务将返回错误codes.SUCCESS(见<一href=\"https://android.googlesource.com/platform/packages/apps/Nfc/+/android-4.4.2_r1/src/com/android/nfc/NfcService.java\"相对=nofollow>这里,线路1438)和错误codes.ERROR_IO如果锁定任何原因而失败(请参阅这里,行1440)。 错误codes.ERROR_INVALID_PARAM
似乎并没有被所有返回,因此 makeReadOnly()
方法通常应该永远返回假
。
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
.
这篇关于为什么我收到IOException异常时,使NFC标签只读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!