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.
Android
iOS
Required capabilities:
<key>com.apple.developer.nfc.readersession.formats</key>
<array>
<string>NDEF</string>
</array>
Provisioning Profile entitled with the NFC Tag Reading capability
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:
Tagviewer
) with your activity name.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.
Use require
to access this module from JavaScript:
var nfc = require('ti.nfc');
The nfc
variable is a reference to the Module object.
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;
}
});
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.
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();
});
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.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();
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.
Intent to start an activity when a tag is discovered.
Intent to start an activity when a tag is discovered.
Intent to start an activity when a tag is discovered and activities are registered for the specific technologies on the tag.
The tag has been configured with invalid parameters.
The tag has been configured with invalid parameters.
Used with ndefRecord records.
Indicates UTF-16 text encoding.
Used with ndefRecord records.
Indicates UTF-8 text encoding.
A security violation associated with the reader session has occurred.
A security violation associated with the reader session has occurred.
The reader session does not support this feature.
The reader session does not support this feature.
The first NDEF tag read by this session is invalid.
The first NDEF tag read by this session is invalid.
The reader session terminated unexpectedly.
The reader session terminated unexpectedly.
The reader session timed out.
The reader session timed out.
The reader session failed because the system is busy.
The reader session failed because the system is busy.
The user canceled the reader session.
The user canceled the reader session.
Used with TECH_MIFARE_CLASSIC tag technology. Size of a MIFARE CLassic block (in bytes).
Used with TECH_MIFARE_CLASSIC tag technology. Tag contains 16 sectors, each with 4 blocks.
Used with TECH_MIFARE_CLASSIC tag technology. Tag contains 32 sectors, each with 4 blocks.
Used with TECH_MIFARE_CLASSIC tag technology. Tag contains 40 sectors.
Used with TECH_MIFARE_CLASSIC tag technology. Tag contains 5 sectors, each with 4 blocks.
Used with TECH_MIFARE_CLASSIC tag technology. A MIFARE Classic tag.
Used with TECH_MIFARE_CLASSIC tag technology. A MIFARE Plus tag.
Used with TECH_MIFARE_CLASSIC tag technology. A MIFARE Pro tag.
Used with TECH_MIFARE_CLASSIC tag technology. A MIFARE Classic compatible card of unknown type.
Used with TECH_MIFARE_ULTRALIGHT tag technology. Size of a MIFARE Ultralight page (in bytes).
Used with TECH_MIFARE_ULTRALIGHT tag technology. A MIFARE Ultralight tag.
Used with TECH_MIFARE_ULTRALIGHT tag technology. A MIFARE Ultralight C tag.
Used with TECH_MIFARE_ULTRALIGHT tag technology. A MIFARE Ultralight compatible tag of unknown type.
Used with NdefSmartPoster records (RTD_SMART_POSTER).
Used with NdefSmartPoster records (RTD_SMART_POSTER).
Used with NdefSmartPoster records (RTD_SMART_POSTER).
Used with NdefSmartPoster records (RTD_SMART_POSTER).
Used with NdefSmartPoster records (RTD_SMART_POSTER).
Used with NdefSmartPoster records (RTD_SMART_POSTER).
Used with NdefSmartPoster records (RTD_SMART_POSTER).
Used with NdefSmartPoster records (RTD_SMART_POSTER).
Used with ndefRecord records.
RTD Alternative Carrier type. For use with TNF_WELL_KNOWN.
Used with ndefRecord records.
RTD Handover Carrier type. For use with TNF_WELL_KNOWN.
Used with ndefRecord records.
RTD Handover Request type. For use with TNF_WELL_KNOWN.
Used with ndefRecord records.
RTD Handover Select type. For use with TNF_WELL_KNOWN.
Used with ndefRecord records.
RTD Smart Poster type. For use with TNF_WELL_KNOWN.
Used with ndefRecord records.
RTD Text type. For use with TNF_WELL_KNOWN.
Used with ndefRecord records.
RTD URI type. For use with TNF_WELL_KNOWN.
Used with TECH_NDEF tag technology. NDEF on MIFARE Classic.
Used with TECH_NDEF tag technology. NFC Forum Tag Type 1.
Used with TECH_NDEF tag technology. NFC Forum Tag Type 2.
Used with TECH_NDEF tag technology. NFC Forum Tag Type 3.
Used with TECH_NDEF tag technology. NFC Forum Tag Type 4.
Available tag technology used with getTechList and AdapterProxy.
Available tag technology used with getTechList and AdapterProxy.
Available tag technology used with getTechList and AdapterProxy.
Available tag technology used with getTechList and AdapterProxy.
Available tag technology used with getTechList and AdapterProxy.
Available tag technology used with getTechList and AdapterProxy.
Available tag technology used with getTechList and AdapterProxy.
Available tag technology used with getTechList and AdapterProxy.
Available tag technology used with getTechList and AdapterProxy.
Available tag technology used with getTechList and AdapterProxy.
Available tag technology used with getTechList and AdapterProxy.
Available tag technology used with getTechList and AdapterProxy.
Available tag technology used with getTechList and AdapterProxy.
Available tag technology used with getTechList and AdapterProxy.
Available tag technology used with getTechList and AdapterProxy.
Available tag technology used with getTechList and AdapterProxy.
Available tag technology used with getTechList and AdapterProxy.
Available tag technology used with getTechList and AdapterProxy.
Used with ndefRecord records. Indicates the type field contains an absolute-URI BNF construct defined by RFC 3986.
Used with ndefRecord records. Indicates the record is empty.
Used with ndefRecord records. Indicates the type field contains an external type name.
Used with ndefRecord records.
Indicates the type field contains a media-type BNF construct, defined by RFC 2046.
Used with ndefRecord records.
Indicates the payload is an intermediate or final chunk of a chunked NDEF Record.
Used with ndefRecord records.
Indicates the payload type is unknown.
Used with ndefRecord records.
Indicates the type field contains a well-known RTD type name.
Too many retries have occurred.
Too many retries have occurred.
Connection to the tag has been lost.
Connection to the tag has been lost.
The tag has responded with an error.
The tag has responded with an error.
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
.
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.
Creates and returns an instance of Modules.Nfc.NdefMessage.
Properties to set on a new object, including any defined by Modules.Nfc.NdefMessage except those marked not-creation or read-only.
Creates and returns an instance of Modules.Nfc.NdefRecordApplication.
Properties to set on a new object, including any defined by Modules.Nfc.NdefRecordApplication except those marked not-creation or read-only.
Creates and returns an instance of Modules.Nfc.NdefRecordEmpty.
Properties to set on a new object, including any defined by Modules.Nfc.NdefRecordEmpty except those marked not-creation or read-only.
Creates and returns an instance of Modules.Nfc.NdefRecordExternal.
Properties to set on a new object, including any defined by Modules.Nfc.NdefRecordExternal except those marked not-creation or read-only.
Creates and returns an instance of Modules.Nfc.NdefRecordMedia.
Properties to set on a new object, including any defined by Modules.Nfc.NdefRecordMedia except those marked not-creation or read-only.
Creates and returns an instance of Modules.Nfc.NdefRecordSmartPoster.
Properties to set on a new object, including any defined by Modules.Nfc.NdefRecordSmartPoster except those marked not-creation or read-only.
Creates and returns an instance of Modules.Nfc.NdefRecordText.
Properties to set on a new object, including any defined by Modules.Nfc.NdefRecordText except those marked not-creation or read-only.
Creates and returns an instance of Modules.Nfc.NdefRecordUnknown.
Properties to set on a new object, including any defined by Modules.Nfc.NdefRecordUnknown except those marked not-creation or read-only.
Creates and returns an instance of Modules.Nfc.NdefRecordUri.
Properties to set on a new object, including any defined by Modules.Nfc.NdefRecordUri except those marked not-creation or read-only.
Creates and returns an instance of Modules.Nfc.NfcAdapter.
Properties to set on a new object, including any defined by Modules.Nfc.NfcAdapter except those marked not-creation or read-only.
Creates and returns an instance of Modules.Nfc.NfcForegroundDispatchFilter.
Properties to set on a new object, including any defined by Modules.Nfc.NfcForegroundDispatchFilter except those marked not-creation or read-only.
Creates and returns an instance of Modules.Nfc.TagTechnologyIsoDep.
Properties to set on a new object, including any defined by Modules.Nfc.TagTechnologyIsoDep except those marked not-creation or read-only.
Creates and returns an instance of Modules.Nfc.TagTechnologyMifareClassic.
Properties to set on a new object, including any defined by Modules.Nfc.TagTechnologyMifareClassic except those marked not-creation or read-only.
Creates and returns an instance of Modules.Nfc.TagTechnologyMifareUltralight.
Properties to set on a new object, including any defined by Modules.Nfc.TagTechnologyMifareUltralight except those marked not-creation or read-only.
Creates and returns an instance of Modules.Nfc.TagTechnologyNdef.
Properties to set on a new object, including any defined by Modules.Nfc.TagTechnologyNdef except those marked not-creation or read-only.
Creates and returns an instance of Modules.Nfc.TagTechnologyNdefFormatable.
Properties to set on a new object, including any defined by Modules.Nfc.TagTechnologyNdefFormatable except those marked not-creation or read-only.
Creates and returns an instance of Modules.Nfc.TagTechnologyNfcA.
Properties to set on a new object, including any defined by Modules.Nfc.TagTechnologyNfcA except those marked not-creation or read-only.
Creates and returns an instance of Modules.Nfc.TagTechnologyNfcB.
Properties to set on a new object, including any defined by Modules.Nfc.TagTechnologyNfcB except those marked not-creation or read-only.
Creates and returns an instance of Modules.Nfc.TagTechnologyNfcF.
Properties to set on a new object, including any defined by Modules.Nfc.TagTechnologyNfcF except those marked not-creation or read-only.
Creates and returns an instance of Modules.Nfc.TagTechnologyNfcV.
Properties to set on a new object, including any defined by Modules.Nfc.TagTechnologyNfcV except those marked not-creation or read-only.
Sets the value of the lifecycleContainer property.
New value for the property.