Modules.Nfc

A cross-platform Near-Field-Communication (NFC) module for iOS and Android. Download the latest release via Github.

Requires: iOS 11.0 and later Android 4.0 and later

This module provides access to Near Field Communication (NFC) functionality, allowing applications to read and write (Android-only) NFC tags. A "tag" may actually be another device that appears as a tag.

NFC Resources

Requirements

  • Android

    • Android 4.1 (API 16) and later
    • An NFC capable device
  • iOS

    • iOS 11 and later
    • iPhone 7 / iPhone 7 Plus and later

Getting Started

  • View the Using Titanium Modules document for instructions on getting started with using this module in your application.

Configure iOS: Capabilities and Provisioning Profiles

  • Required capabilities:

    <key>com.apple.developer.nfc.readersession.formats</key>
    <array>
        <string>NDEF</string>
    </array>
    
  • Provisioning Profile entitled with the NFC Tag Reading capability

Configure Android: Tag Dispatching and Intent Filters

  • The Android tag dispatch system is responsible for dispatching NFC messages to the appropriate application. In the situation where you are not using foreground dispatching, you will need to define intent-filters in the tiapp.xml file to specify which types of NFC messages the application wants to receive. By using intent-filters in the tiapp.xml file, the application will be automatically started if a matching NFC message is dispatched.

    Add code similar to the following to your tiapp.xml file:

    • Replace occurrences of the activity name (Tagviewer) with your activity name.
    • Add the NFC permissions to your Android configuration
    • Replace the NFC specific intent filters with filters appropriate for your application.

      <android xmlns:android="http://schemas.android.com/apk/res/android">
          <manifest>
              <!-- Required NFC permissions -->
              <uses-permission android:name="android.permission.NFC" />
              <uses-feature android:name="android.hardware.nfc" android:required="true" /> 
      
              <!-- NFC Intent filters -->
              <application>
                  <activity android:name=".TagviewerActivity"
                   android:label="TagViewer" android:theme="@style/Theme.Titanium"
                   android:configChanges="keyboardHidden|orientation">
                      <intent-filter>
                          <action android:name="android.nfc.action.TECH_DISCOVERED"/>
                          <category android:name="android.intent.category.DEFAULT"/>
                      </intent-filter>
      
                      <intent-filter>
                          <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
                          <category android:name="android.intent.category.DEFAULT"/>
                          <data android:mimeType="text/plain" />
                      </intent-filter>
      
                      <intent-filter>
                          <action android:name="android.nfc.action.TAG_DISCOVERED"/>
                          <category android:name="android.intent.category.DEFAULT"/>
                      </intent-filter>
                  </activity>
              </application>
          </manifest>
      </android>
      

    Note that if you are using foreground dispatching you do not need to define intent filters in the application's tiapp.xml file.

Accessing the Module

  • Use require to access this module from JavaScript:

    var nfc = require('ti.nfc');
    

    The nfc variable is a reference to the Module object.

Creating an Adapter

  • The NFC adapter gives you access to the features of the NFC device. The NFC adapter proxy is always associated with the activity that was the current activity when it was created. Therefore, the NFC Adapter should be created after the activity has been opened. You can use the window open event to know when the activity has been opened.

    $.index.addEventListener('open', function(e) {
        nfcAdapter = nfc.createNfcAdapter({
            onNdefDiscovered: handleDiscovery,
            onTagDiscovered: handleDiscovery, // Android-only
            onTechDiscovered: handleDiscovery // Android-only
        });
    
        if (!nfcAdapter.isEnabled()) {
            Ti.API.error('NFC is not enabled on this device!');
            return;
        }
    });
    

Handling Intents (Android)

  • NFC Intents are dispatched to an activity by the Android tag dispatch system. When your activity receives a new intent, it must forward the intent to the onNewIntent method of the NFC adapter for processing.

    Ti.Android.currentActivity.addEventListener('newintent', function (e) {
        nfcAdapter.onNewIntent(e.intent);
    });
    

    If your application is started as the result of an NFC intent, that intent will automatically be processed when the NFC module is loaded.

Foreground Dispatch

  • The foreground dispatch system allows an activity to intercept an intent and claim priority over other activities that handle the same intent. When using foreground dispatching, you must process the pause and resume events on the activity and enable or disable foreground dispatching.

    var currentActivity = Ti.Android.currentActivity;
    
    currentActivity.addEventListener('resume', function(e) {
        nfcAdapter.enableForegroundDispatch(dispatchFilter);
    });
    currentActivity.addEventListener('pause', function(e) {
        nfcAdapter.disableForegroundDispatch();
    });
    

Example applications

  • Android example applications are located in the example/android folder of the module:

    • TagBeam demonstrates how to use Android Beam to send messages to another NFC capable device.
    • TagForeground demonstrates how to read NFC tags only when the application is in the foreground.
    • TagViewer demonstrates how to receive NFC tag intents even when the application is not running.
    • TagWriter demonstrates how to write to an NFC tag using the Ndef tag technology data format.
  • iOS example applications are located in the example/ios folder of the module:

    • TagViewer demonstrates how to receive NFC tags even when the application is running.

Examples

Creating NFC Adapter (iOS & Android)

This example demonstrates the proper technique for creating an NFC adapter. The NFC Adapter should be created after the window has been opened.

var nfc = require('ti.nfc');
var nfcAdapter = null;

$.index.addEventListener('open', function(e) {
    // Must wait until the activity has been opened before setting up NFC
    // Create the NFC adapter to be associated with this activity. 
    // There should only be ONE adapter created per activity.
    nfcAdapter = nfc.createNfcAdapter({
        onNdefDiscovered: handleDiscovery,
        onTagDiscovered: handleDiscovery, // Android-only
        onTechDiscovered: handleDiscovery // Android-only
    });

    // It's possible that the device does not support NFC. Check it here
    // before we go any further. For iOS, right now this is decided 
    // internally by the system. 
    if (OS_ANDROID) {
        if (!nfcAdapter.isEnabled()) {
            alert('NFC is not enabled on this device');
            return;
        }
        Ti.Android.currentActivity.addEventListener('newintent', function (e) {
            nfcAdapter.onNewIntent(e.intent);
        });

    // iOS needs to be told to scan
    } else if (OS_IOS) {
        nfcAdapter.begin();
    }
});

function handleDiscovery(e) {
    alert(JSON.stringify(e, null, 2));
}

$.index.open();
  • 1.0.0
  • 2.0.0
Defined By

Properties

Modules.Nfc
ACTION_NDEF_DISCOVERED : Stringreadonly

Intent to start an activity when a tag with NDEF payload is discovered.

Intent to start an activity when a tag with NDEF payload is discovered.

  • 1.0.0
Modules.Nfc
ACTION_TAG_DISCOVERED : Stringreadonly

Intent to start an activity when a tag is discovered.

Intent to start an activity when a tag is discovered.

  • 1.0.0
Modules.Nfc
: Stringreadonly
Intent to start an activity when a tag is discovered and activities are registered for the specific technologies on t...

Intent to start an activity when a tag is discovered and activities are registered for the specific technologies on the tag.

  • 1.0.0

The tag has been configured with invalid parameters.

The tag has been configured with invalid parameters.

  • 2.0.0
Modules.Nfc
: Stringreadonly
Used with ndefRecord records. ...

Used with ndefRecord records.
Indicates UTF-16 text encoding.

  • 1.0.0
Modules.Nfc
: Stringreadonly
Used with ndefRecord records. ...

Used with ndefRecord records.
Indicates UTF-8 text encoding.

  • 1.0.0
Modules.Nfc
ERROR_SECURITY_VIOLATION : Numberreadonly

A security violation associated with the reader session has occurred.

A security violation associated with the reader session has occurred.

  • 2.0.0
Modules.Nfc
ERROR_UNSUPPORTED_FEATURE : Numberreadonly

The reader session does not support this feature.

The reader session does not support this feature.

  • 2.0.0
Modules.Nfc
INVALIDATION_ERROR_FIRST_NDEF_TAG_READ : Numberreadonly

The first NDEF tag read by this session is invalid.

The first NDEF tag read by this session is invalid.

  • 2.0.0

The reader session terminated unexpectedly.

The reader session terminated unexpectedly.

  • 2.0.0
Modules.Nfc
INVALIDATION_ERROR_SESSION_TIMEOUT : Numberreadonly

The reader session timed out.

The reader session timed out.

  • 2.0.0
Modules.Nfc
INVALIDATION_ERROR_SYSTEM_IS_BUSY : Numberreadonly

The reader session failed because the system is busy.

The reader session failed because the system is busy.

  • 2.0.0
Modules.Nfc
INVALIDATION_ERROR_USER_CANCELED : Numberreadonly

The user canceled the reader session.

The user canceled the reader session.

  • 2.0.0
Modules.Nfc
: Numberreadonly
Used with TECH_MIFARE_CLASSIC tag technology. ...

Used with TECH_MIFARE_CLASSIC tag technology. Size of a MIFARE CLassic block (in bytes).

  • 1.1.0
Modules.Nfc
: Numberreadonly
Used with TECH_MIFARE_CLASSIC tag technology. ...

Used with TECH_MIFARE_CLASSIC tag technology. Tag contains 16 sectors, each with 4 blocks.

  • 1.1.0
Modules.Nfc
: Numberreadonly
Used with TECH_MIFARE_CLASSIC tag technology. ...

Used with TECH_MIFARE_CLASSIC tag technology. Tag contains 32 sectors, each with 4 blocks.

  • 1.1.0
Modules.Nfc
: Numberreadonly
Used with TECH_MIFARE_CLASSIC tag technology. ...

Used with TECH_MIFARE_CLASSIC tag technology. Tag contains 40 sectors.

  • 1.1.0
Modules.Nfc
: Numberreadonly
Used with TECH_MIFARE_CLASSIC tag technology. ...

Used with TECH_MIFARE_CLASSIC tag technology. Tag contains 5 sectors, each with 4 blocks.

  • 1.1.0
Modules.Nfc
: Numberreadonly
Used with TECH_MIFARE_CLASSIC tag technology. ...

Used with TECH_MIFARE_CLASSIC tag technology. A MIFARE Classic tag.

  • 1.1.0
Modules.Nfc
: Numberreadonly
Used with TECH_MIFARE_CLASSIC tag technology. ...

Used with TECH_MIFARE_CLASSIC tag technology. A MIFARE Plus tag.

  • 1.1.0
Modules.Nfc
: Numberreadonly
Used with TECH_MIFARE_CLASSIC tag technology. ...

Used with TECH_MIFARE_CLASSIC tag technology. A MIFARE Pro tag.

  • 1.1.0
Modules.Nfc
: Numberreadonly
Used with TECH_MIFARE_CLASSIC tag technology. ...

Used with TECH_MIFARE_CLASSIC tag technology. A MIFARE Classic compatible card of unknown type.

  • 1.1.0
Modules.Nfc
: Numberreadonly
Used with TECH_MIFARE_ULTRALIGHT tag technology. ...

Used with TECH_MIFARE_ULTRALIGHT tag technology. Size of a MIFARE Ultralight page (in bytes).

  • 1.1.0
Modules.Nfc
: Numberreadonly
Used with TECH_MIFARE_ULTRALIGHT tag technology. ...

Used with TECH_MIFARE_ULTRALIGHT tag technology. A MIFARE Ultralight tag.

  • 1.1.0
Modules.Nfc
: Numberreadonly
Used with TECH_MIFARE_ULTRALIGHT tag technology. ...

Used with TECH_MIFARE_ULTRALIGHT tag technology. A MIFARE Ultralight C tag.

  • 1.1.0
Modules.Nfc
: Numberreadonly
Used with TECH_MIFARE_ULTRALIGHT tag technology. ...

Used with TECH_MIFARE_ULTRALIGHT tag technology. A MIFARE Ultralight compatible tag of unknown type.

  • 1.1.0
Modules.Nfc
: Stringreadonly
Used with ndefRecord records. ...

Used with ndefRecord records.
RTD Alternative Carrier type. For use with TNF_WELL_KNOWN.

  • 1.0.0
Modules.Nfc
: Stringreadonly
Used with ndefRecord records. ...

Used with ndefRecord records.
RTD Handover Carrier type. For use with TNF_WELL_KNOWN.

  • 1.0.0
Modules.Nfc
: Stringreadonly
Used with ndefRecord records. ...

Used with ndefRecord records.
RTD Handover Request type. For use with TNF_WELL_KNOWN.

  • 1.0.0
Modules.Nfc
: Stringreadonly
Used with ndefRecord records. ...

Used with ndefRecord records.
RTD Handover Select type. For use with TNF_WELL_KNOWN.

  • 1.0.0
Modules.Nfc
: Stringreadonly
Used with ndefRecord records. ...

Used with ndefRecord records.
RTD Smart Poster type. For use with TNF_WELL_KNOWN.

  • 1.0.0
Modules.Nfc
: Stringreadonly
Used with ndefRecord records. ...

Used with ndefRecord records.
RTD Text type. For use with TNF_WELL_KNOWN.

  • 1.0.0
Modules.Nfc
: Stringreadonly
Used with ndefRecord records. ...

Used with ndefRecord records.
RTD URI type. For use with TNF_WELL_KNOWN.

  • 1.0.0
Modules.Nfc
: Stringreadonly
Used with TECH_NDEF tag technology. ...

Used with TECH_NDEF tag technology. NDEF on MIFARE Classic.

  • 1.1.0
Modules.Nfc
: Stringreadonly
Used with TECH_NDEF tag technology. ...

Used with TECH_NDEF tag technology. NFC Forum Tag Type 1.

  • 1.1.0
Modules.Nfc
: Stringreadonly
Used with TECH_NDEF tag technology. ...

Used with TECH_NDEF tag technology. NFC Forum Tag Type 2.

  • 1.1.0
Modules.Nfc
: Stringreadonly
Used with TECH_NDEF tag technology. ...

Used with TECH_NDEF tag technology. NFC Forum Tag Type 3.

  • 1.1.0
Modules.Nfc
: Stringreadonly
Used with TECH_NDEF tag technology. ...

Used with TECH_NDEF tag technology. NFC Forum Tag Type 4.

  • 1.1.0
Modules.Nfc
TECH_ISODEP : Numberreadonly

Available tag technology used with getTechList and AdapterProxy.

Available tag technology used with getTechList and AdapterProxy.

  • 1.0.0
Modules.Nfc
TECH_MIFARE_CLASSIC : Numberreadonly

Available tag technology used with getTechList and AdapterProxy.

Available tag technology used with getTechList and AdapterProxy.

  • 1.0.0
Modules.Nfc
TECH_MIFARE_ULTRALIGHT : Numberreadonly

Available tag technology used with getTechList and AdapterProxy.

Available tag technology used with getTechList and AdapterProxy.

  • 1.0.0
Modules.Nfc
TECH_NDEF : Numberreadonly

Available tag technology used with getTechList and AdapterProxy.

Available tag technology used with getTechList and AdapterProxy.

  • 1.0.0
Modules.Nfc
TECH_NDEFFORMATABLE : Numberreadonly

Available tag technology used with getTechList and AdapterProxy.

Available tag technology used with getTechList and AdapterProxy.

  • 1.0.0
Modules.Nfc
TECH_NFCA : Numberreadonly

Available tag technology used with getTechList and AdapterProxy.

Available tag technology used with getTechList and AdapterProxy.

  • 1.0.0
Modules.Nfc
TECH_NFCB : Numberreadonly

Available tag technology used with getTechList and AdapterProxy.

Available tag technology used with getTechList and AdapterProxy.

  • 1.0.0
Modules.Nfc
TECH_NFCF : Numberreadonly

Available tag technology used with getTechList and AdapterProxy.

Available tag technology used with getTechList and AdapterProxy.

  • 1.0.0
Modules.Nfc
TECH_NFCV : Numberreadonly

Available tag technology used with getTechList and AdapterProxy.

Available tag technology used with getTechList and AdapterProxy.

  • 1.0.0
Modules.Nfc
: Numberreadonly
Used with ndefRecord records. ...

Used with ndefRecord records. Indicates the type field contains an absolute-URI BNF construct defined by RFC 3986.

Modules.Nfc
: Numberreadonly
Used with ndefRecord records. ...

Used with ndefRecord records. Indicates the record is empty.

Modules.Nfc
: Numberreadonly
Used with ndefRecord records. ...

Used with ndefRecord records. Indicates the type field contains an external type name.

Modules.Nfc
: Numberreadonly
Used with ndefRecord records. ...

Used with ndefRecord records.
Indicates the type field contains a media-type BNF construct, defined by RFC 2046.

Modules.Nfc
: Numberreadonly
Used with ndefRecord records. ...

Used with ndefRecord records.
Indicates the payload is an intermediate or final chunk of a chunked NDEF Record.

Modules.Nfc
: Numberreadonly
Used with ndefRecord records. ...

Used with ndefRecord records.
Indicates the payload type is unknown.

Modules.Nfc
: Numberreadonly
Used with ndefRecord records. ...

Used with ndefRecord records.
Indicates the type field contains a well-known RTD type name.

Modules.Nfc
TRANSCEIVE_ERROR_RETRY_EXCEEDED : Numberreadonly

Too many retries have occurred.

Too many retries have occurred.

  • 2.0.0
Modules.Nfc
TRANSCEIVE_ERROR_TAG_CONNECTION_LOST : Numberreadonly

Connection to the tag has been lost.

Connection to the tag has been lost.

  • 2.0.0
Modules.Nfc
TRANSCEIVE_ERROR_TAG_RESPONSE_ERROR : Numberreadonly

The tag has responded with an error.

The tag has responded with an error.

  • 2.0.0
apiName : Stringreadonly

The name of the API that this proxy corresponds to.

The name of the API that this proxy corresponds to.

The value of this property is the fully qualified name of the API. For example, Button returns Ti.UI.Button.

  • 3.2.0
  • 3.2.0
  • 3.2.0

The Window or TabGroup whose Activity lifecycle should be triggered on the proxy.

The Window or TabGroup whose Activity lifecycle should be triggered on the proxy.

If this property is set to a Window or TabGroup, then the corresponding Activity lifecycle event callbacks will also be called on the proxy. Proxies that require the activity lifecycle will need this property set to the appropriate containing Window or TabGroup.

  • 3.6.0
Defined By

Methods

Modules.Nfc
( [parameters] ) : Modules.Nfc.NdefMessage
Creates and returns an instance of Modules.Nfc.NdefMessage. ...

Creates and returns an instance of Modules.Nfc.NdefMessage.

  • 1.0.0
  • 6.3.0

Parameters

Returns

Creates and returns an instance of Modules.Nfc.NdefRecordApplication. ...

Creates and returns an instance of Modules.Nfc.NdefRecordApplication.

  • 1.0.0

Parameters

Returns

Creates and returns an instance of Modules.Nfc.NdefRecordEmpty. ...

Creates and returns an instance of Modules.Nfc.NdefRecordEmpty.

  • 1.0.0

Parameters

Returns

Creates and returns an instance of Modules.Nfc.NdefRecordExternal. ...

Creates and returns an instance of Modules.Nfc.NdefRecordExternal.

  • 1.0.0

Parameters

Returns

Creates and returns an instance of Modules.Nfc.NdefRecordMedia. ...

Creates and returns an instance of Modules.Nfc.NdefRecordMedia.

  • 1.0.0

Parameters

Returns

Creates and returns an instance of Modules.Nfc.NdefRecordSmartPoster. ...

Creates and returns an instance of Modules.Nfc.NdefRecordSmartPoster.

  • 1.0.0

Parameters

Returns

Creates and returns an instance of Modules.Nfc.NdefRecordText. ...

Creates and returns an instance of Modules.Nfc.NdefRecordText.

  • 1.0.0

Parameters

Returns

Creates and returns an instance of Modules.Nfc.NdefRecordUnknown. ...

Creates and returns an instance of Modules.Nfc.NdefRecordUnknown.

  • 1.0.0

Parameters

Returns

Modules.Nfc
( [parameters] ) : Modules.Nfc.NdefRecordUri
Creates and returns an instance of Modules.Nfc.NdefRecordUri. ...

Creates and returns an instance of Modules.Nfc.NdefRecordUri.

  • 1.0.0

Parameters

Returns

Modules.Nfc
( [parameters] ) : Modules.Nfc.NfcAdapter
Creates and returns an instance of Modules.Nfc.NfcAdapter. ...

Creates and returns an instance of Modules.Nfc.NfcAdapter.

  • 1.0.0
  • 1.0.0

Parameters

Returns

Creates and returns an instance of Modules.Nfc.NfcForegroundDispatchFilter. ...

Creates and returns an instance of Modules.Nfc.NfcForegroundDispatchFilter.

  • 1.0.0

Parameters

Returns

Creates and returns an instance of Modules.Nfc.TagTechnologyIsoDep. ...

Creates and returns an instance of Modules.Nfc.TagTechnologyIsoDep.

  • 1.1.0

Parameters

Returns

Creates and returns an instance of Modules.Nfc.TagTechnologyMifareClassic. ...

Creates and returns an instance of Modules.Nfc.TagTechnologyMifareClassic.

  • 1.1.0

Parameters

Returns

Creates and returns an instance of Modules.Nfc.TagTechnologyMifareUltralight. ...

Creates and returns an instance of Modules.Nfc.TagTechnologyMifareUltralight.

  • 1.1.0

Parameters

Returns

Creates and returns an instance of Modules.Nfc.TagTechnologyNdef. ...

Creates and returns an instance of Modules.Nfc.TagTechnologyNdef.

  • 1.1.0

Parameters

Returns

Creates and returns an instance of Modules.Nfc.TagTechnologyNdefFormatable. ...

Creates and returns an instance of Modules.Nfc.TagTechnologyNdefFormatable.

  • 1.1.0

Parameters

Returns

Creates and returns an instance of Modules.Nfc.TagTechnologyNfcA. ...

Creates and returns an instance of Modules.Nfc.TagTechnologyNfcA.

  • 1.1.0

Parameters

Returns

Creates and returns an instance of Modules.Nfc.TagTechnologyNfcB. ...

Creates and returns an instance of Modules.Nfc.TagTechnologyNfcB.

  • 1.1.0

Parameters

Returns

Creates and returns an instance of Modules.Nfc.TagTechnologyNfcF. ...

Creates and returns an instance of Modules.Nfc.TagTechnologyNfcF.

  • 1.1.0

Parameters

Returns

Creates and returns an instance of Modules.Nfc.TagTechnologyNfcV. ...

Creates and returns an instance of Modules.Nfc.TagTechnologyNfcV.

  • 1.1.0

Parameters

Returns

Gets the value of the apiName property. ...

Gets the value of the apiName property.

  • 3.2.0
  • 3.2.0
  • 3.2.0

Returns

  • String
Gets the value of the lifecycleContainer property. ...

Gets the value of the lifecycleContainer property.

  • 3.6.0

Returns

Sets the value of the lifecycleContainer property. ...

Sets the value of the lifecycleContainer property.

  • 3.6.0

Parameters

Returns

  • void