Back

NFC

Please make sure you have a phone that supports NFC.

This plugin enables your device to communicate with NFC/RFID devices.

In order to use NFC, you must first load the plugin at the top of your script using the LoadPlugin method like this:

 app.LoadPlugin( "NFC" );

Then you create the NFC object like this:

 nfc = app.CreateNfc();

You can watch for various types of tags using the following methods:

 nfc.SetOnTag( callback );
 nfc.SetOnNdef( callback );

Note: To stop watching for a tag, set the callack to null

Less information is returned when using the SetOnTag method compared with the SetOnNdef method, so use SetOnNdef if you are only interested in NDEF tags. If you only want the tag id then using the SetOnTag method might be best option because a wider range of tags types will be supported.

Use the SetOnError method to detect errors and the GetNfcStatus method to check the state of the NFC reader (returns: "NFC_OK", "NO_NFC" or "NFC_DISABLED").

 nfc.SetOnError( callback );
 nfc.GetNfcStatus();

Example - Watch for any type of tag

app.LoadPlugin( "NFC" );

function OnStart()
{
  lay = app.CreateLayout( "Linear", "VCenter,FillXY" );

  txt = app.CreateText( "Please Scan Your Tag", 0.8,0.8, "MultiLine" );
  lay.AddChild( txt );

  app.AddLayout( lay );

  nfc = app.CreateNfc();
  nfc.SetOnError( OnError );
  nfc.SetOnTag( OnTag );

}

function OnTag( data )
{
  txt.Log( JSON.stringify(data) );
  app.ShowPopup( nfc.ToHex(data.tag.id,":") );
}

function OnError( msg )
{
  app.ShowPopup( msg );
}

  Copy   Copy All    Run   

Example - Watch for an NDEF tag

app.LoadPlugin( "NFC" );

function OnStart()
{
  lay = app.CreateLayout( "Linear", "VCenter,FillXY" );

  txt = app.CreateText( "Please Scan Your Tag", 0.8,0.8, "MultiLine" );
  lay.AddChild( txt );

  app.AddLayout( lay );

  nfc = app.CreateNfc();
  nfc.SetOnNdef( OnTag );

}

function OnTag( data )
{
  txt.Log( JSON.stringify(data) );
  app.ShowPopup( nfc.ToHex(data.tag.id,":") );
}

  Copy   Copy All    Run