How to read an NFC tag which is already attached to the device

白昼怎懂夜的黑 提交于 2019-12-11 14:36:17

问题


My requirement is to read an NFC tag which is already attached to the device.

I observed the following in my application:

An NFC tag is already attached to the device and NFC is off. When I then launch my Android app it will ask to turn on the NFC and, after turning on NFC, my app will read the NFC tag that was already attached to the device.

An NFC tag is already attached to the device and NFC is on. When I then launch my app manually, it will not detect the NFC tag.

Why is the NFC tag not detected by the app in the latter case? Is there any way to detect and read a tag that was already attached to the device before the app is started (manually)?


回答1:


In general that's not possible. Android only dispatches a tag to an app if the tag is newly discovered (i.e. the tag has just been tapped). Thus, apps cannot access tags that were already in the field of the NFC reader when the app is launched.

Since the Android NFC API does not allow the NFC feature to be disabled and re-enabled from within an app (this can only be done through the Settings app), its also not possible to temporarily disable the NFC reader to trick Android into detecting the tag as freshly tapped.

However, on Android version >= 4.4 and < 5.1 (or was that < 6.0?) there is a bug (undocumented feature?) in the implementation of the NFC reader-mode API (enableReaderMode() that causes tags to be redecteded after enabling reader-mode. Consequently, on these Android versions you could use enableReaderMode() to rediscover already-attached tags. Be warned though that while this works in AOSP, some device manufacturer-specific modifications of Android might not contain this bug/feature.



来源:https://stackoverflow.com/questions/36981787/how-to-read-an-nfc-tag-which-is-already-attached-to-the-device

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