Connection error while reading Android NfcV tags

I have an Android app that reads NFC tags. Everything works fine on the LG Nexus 4, but on the Samsung Galaxy S5 I only get I / O exceptions (tested on multiple phones).

So, the tags are of type NfcV, and I get an I / O exception when calling connect() on NfcV (further down, this is error code -5 ERROR_CONNECT).

NFC TagInfo by NXP can read the contents of tag memory on SG5S - are there any other ways to read NfcV tags than with connect() and transceive() ?

What differences between NFC chips will lead to the fact that the connection to my application will not work on one phone, but not on another (while other applications read it correctly)? Are there any time intervals that I need to adjust, maybe?

Code snippet:

 NfcV nfcvTag = NfcV.get(intent.getParcelableExtra(NfcAdapter.EXTRA_TAG)); if(nfcvTag!=null){ try { nfcvTag.connect(); //{flags:0x00, read multiple blocks command: 0x23, start at block 0: 0x00, read 9 blocks (0 to 8): 0x08} response = nfcvTag.transceive(new byte[] {(byte)0x00,(byte)0x23,(byte)0x00,byte)0x08}); } catch (IOException e) { Log.d("NFCService", nfcvTag.toString()); } finally { try { nfcvTag.close(); } catch (IOException e) { } } } 
+1
source share
1 answer

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

It seems you cannot use IntentService to handle access to the NFC tag (via the received NFC discovery intent) in a separate thread. In this case, the connect() tag technology object method will not be executed.

This does not mean that you cannot handle access to the tag in a separate (worker) thread. In fact, you should not access the tag in the main (UI) thread, as this will block the application (and its user interface) for the duration of the tag access and may lead to your application being killed by the system in order to be immune. Manually creating a workflow that handles tag access works fine.

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

  • Perhaps this was the case that passing the NFC discovery intent to the IntentService meant a significant delay between the detection of the tag and the actual access to the tag. During this delay, the connection with the tag could drop (for example, due to the user incorrectly aligning the reader and tag antennas, making communication impossible, etc.).
  • (I do not have enough knowledge about the internal components of the Android NFC system service to know if this is even possible :) Perhaps the Tag object is somehow related to the Activity . Because IntentService is another component (although it runs in the same application context), it is not allowed to access the Tag object.

It appears that the NFC controller used in the Galaxy S5 only supports addressed READ ISO / IEC 15693 commands. Using unaddressed commands (that is, Teams that do not have a target flag set and do not contain a tag identifier identifier) ​​will return { 0x02 } from the transceive() method.

It is difficult to say whether this is the case with the NFC controller (PN547? From NXP) in S5 or if this is what is characteristic of how the tag processes addressed or non-addressed commands and responses.

+1
source

Source: https://habr.com/ru/post/982197/


All Articles