NFC tags come in a wide array of technologies and can also have data written to them in many different ways. Android and iOS has the most support for the NDEF standard, which is defined by the NFC Forum.
NDEF data is encapsulated inside a message (
NdefMessage) that contains one or more records (
NdefRecord). Each NDEF record must be well-formed according to the specification of the type of record that you want to create.
Your application will get notified about NFC tag detection events via the
NFCEvent. This is independent of the mode. The most relevant event is the
NFCEvent.ACTION_NDEF_DISCOVERED which is dispatched whenever an applicable NDEF NFC tag is detected.
NFCEvent will contain a
Tag object in the
tag variable. This contains all the information available about the detected tag, including
NdefRecords contained in the tag.
NFC.service.addEventListener( NFCEvent.ACTION_NDEF_DISCOVERED, discoveredHandler );
function discoveredHandler( event:NFCEvent ):void
trace( "discoveredHandler" );
for each (var message:NdefMessage in event.tag.messages)
for each (var record:NdefRecord in message.records)
trace( "\t"+record.toString() + "::"+record.payload.toString() +" url:"+record.url );
NFC.Instance.OnNdefDiscovered += Instance_OnNdefDiscovered;
void Instance_OnNdefDiscovered(NFCEvent e)
foreach (NdefMessage message in e.tag.messages)
foreach (NdefRecord record in message.records)
Log(" record.payload: " + record.payload);
There are two modes of scanning for tags, which we reference as:
- "dispatch mode", where you are listening for system dispatched events;
- "reader mode", where you are actively reading tags;
Both are very similar, and will communicate via the same events as above. The main differences being that:
- reader mode active reads tags and can read a tag multiple times if required, however;
- reader mode mode interferes with other NFC functionality disabling any peer-to-peer (Android Beam) and card-emulation modes of the NFC adapter on this device;
- reader mode is only available while your application is active, i.e only dispatch mode supports background scanning and launching your application from a tag.
As a general rule, you should use dispatch mode, unless you really need to read a tag multiple times.
There is also no reason that you can't combine the two, using dispatch mode to register your application for launches from tags and using reader mode to actively scan while your application is in use.