Is it possible to perform multiple read and write operations on an NFC Tag from an Android device

StackOverflow https://stackoverflow.com/questions/22957142

  •  30-06-2023
  •  | 
  •  

Question

I have been researching on whether it is possible to perform multiple read and write operations on an NFC device using an Android application. I have found that Android Beam is used for a peer-to-peer (P2P) type exchange of data between two devices, but does this mean that the other non-Android device has to be a P2P NFC tag. Because the tag that I am currently working with is a Type 4 tag using NDEF.

Is it possible to perform multiple read and write operations with an NFC device by holding the phone in the same place? Because at the moment I can only get it working where you place the phone on the NFC tag and it reads it and then you have to move the phone away and do it again to perform the transaction again. This is obviously the natural process of reading an NFC tag.

Please can anyone help me with this dilemma or give me any tips. Thank you in advance.

Was it helpful?

Solution

First of all, there is no such thing as a peer-to-peer NFC tag. A device can be either a peer-to-peer device or an NFC tag, but not both at the same time. (Note, that a device could still support both modes, but not communicate in both modes at the same time.)

So if you communicate with an NFC tag (like your NFC Forum Type 4 tag), there is no peer-to-peer mode (thus no Beam) involved. On Android you can communicate (real, bidirectional communication) with an NFC tag or contactless smartcard.

In case of your Type 4 tag, you would start by retrieving the NFC intent when the tag is discovered. You can do that by registering for a specific NDEF record or tag type in your app's manifest or by using the foreground dispatch system (i.e. if you want to detect tags while your activity is already in the foreground). The next step is to get the tag handle (Tag object) from the intent:

Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

Then you have two options for a Type 4 tag. If you want to communicate with the tag using NDEF message read/write operations, you would get an Ndef object for the tag:

Ndef ndefTag = Ndef.get(tag);

You can then use the connect() method to connect to the tag, the getNdefMessage() to read the current NDEF message from the tag and the writeNdefMessage(...) method to write a new NDEF message to the tag.

Or if you want to perform low-level communication with the tag, you can connect using the IsoDep technology:

IsoDep isoDep = IsoDep.get(tag);

You can the use the connect() method to connect to the tag and the transceive(...) method to exchange ISO 7816-4 APDUs (or possibly proprietary commands) with the tag.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top