Connection error when reading Android NfcV tags

半城伤御伤魂 提交于 2019-11-29 12:48:48

So, to summarize what we found out during the discussion in the above comments:

It seems that you cannot use an IntentService to handle access to an NFC tag (through a received NFC discovery intent) in a separate thread. In that case the tag technology object's connect() method will fail.

This does not mean that you cannot handle access to a tag in a separate (worker) thread. In fact you should not access a tag on the main (UI) thread as this will block the app (and its UI) for the duration of the tag access and may cause your app to be killed by the system for being unresponsive. Manually spawning a worker thread that handles access to a tag works just fine.

My personal and unverified ideas/wild guesses why the IntentService could not handle the tag:

  1. It could have been the case that passing the NFC discovery intent to the IntentService introduced a significant delay between the tag discovery and the actual access to the tag. During this delay communication with the tag could have dropped (e.g. due to the user misaligning the reader and tag antennas making communication impossible, etc)
  2. (I do not have enough knowledge about the internals of the Android NFC system service to know if this would even be possible:) It might be that the Tag object is somehow bound to the Activity. As the IntentService is a different component (though being executed in the same app context), it might not be permitted to access the Tag object.

It seems that the NFC controller used in the Galaxy S5 only supports addressed ISO/IEC 15693 READ commands. Using unaddressed commands (i.e. commands that do not have the addressed flag set and do not contain the tag's UID) lead to { 0x02 } being returned from the transceive() method.

It is difficult to say if this is generally the case with the NFC controller (PN547? from NXP) in the S5 or if that's something that is specific to how the tag treats addressed versus unaddressed commands and responses.

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