Modules.Nfc.NfcAdapter
Represents the local NFC 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. The NFC Adapter must be created after the activity has been opened. You can use the window open event to know when the activity has been opened.
Use the Modules.Nfc.createNfcAdapter method to create an NFC adapter.
See also: NfcAdapter
Extends: Titanium.Proxy · Since: 1.0.0 · Platforms: android, iphone
Properties #
invalidateAfterFirstRead#
Type: Boolean
Session will automatically invalidate after the first NDEF tag is read
successfully when this is set to true, and the error callback will
return Modules.Nfc.INVALIDATION_ERROR_FIRST_NDEF_TAG_READ in this case.
onBeamPushUris#
Type: Callback
Callback function used to dynamically generate one or more Uris to send using Android Beam.
The callback function must return an array of Uri(s) to be pushed.
This method is only available on Android 4.0 (API 16) and above.
See also:
[setBeamPushUrisCallback](http://developer.android.com/reference/android/nfc/NfcAdapter.html#setBeamPushUrisCallback(android.nfc.NfcAdapter.CreateBeamUrisCallback, android.app.Activity))
onNdefDiscovered#
Type: Callback<NdefDiscovered>
Callback function to execute when a tag with NDEF payload is discovered.
See also:
ACTION_NDEF_DISCOVERED
onNdefInvalidated#
Type: Callback<NdefInvalidated>
Callback function to execute when a session was invalidated (either errored by the system or cancelled by the user).
onPushComplete#
Type: Callback
Callback function to execute on successful Android Beam operation.
This method is only available on Android 4.0 (API 14) and above.
See also:
[setOnNdefPushCompleteCallback](http://developer.android.com/reference/android/nfc/NfcAdapter.html#setOnNdefPushCompleteCallback(android.nfc.NfcAdapter.OnNdefPushCompleteCallback, android.app.Activity, android.app.Activity...))
in the Android Developer Reference.
onPushMessage#
Type: Callback
Callback function used to dynamically generated NDEF messsages to send using Android Beam.
The callback function must return an NDEF message to be used for the Android Beam operation.
This method is only available on Android 4.0 (API 14) and above.
See also:
[setNdefPushMessageCallback](http://developer.android.com/reference/android/nfc/NfcAdapter.html#setNdefPushMessageCallback(android.nfc.NfcAdapter.CreateNdefMessageCallback, android.app.Activity, android.app.Activity...))
in the Android Developer Reference.
onTagDiscovered#
Type: Callback<NdefDiscovered>
Callback function to execute when a tag is discovered.
See also:
ACTION_TAG_DISCOVERED
onTechDiscovered#
Type: Callback<NdefDiscovered>
Callback function to execute when a tag is discovered and activities are registered for the specific technologies on the tag.
See also:
ACTION_TECH_DISCOVERED
Methods #
disableForegroundDispatch #
Disable foreground dispatch to the current activity.
disableForegroundNdefPush #
Disable NDEF message push over P2P.
This method was deprecated in API level 14. Use setNdefPushMessage instead.
disableReader #
Disable reader mode in the current activity.
enableForegroundDispatch #
Enable foreground dispatch to the current activity.
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. See the Foreground Dispatch example
for an example of enabling and disabling foreground dispatch during these events.
See also:
[enableForegroundDispatch](http://developer.android.com/reference/android/nfc/NfcAdapter.html#enableForegroundDispatch(android.app.Activity, android.app.PendingIntent, android.content.IntentFilter[], java.lang.String[][]))
| Name | Type | Summary | Optional |
|---|---|---|---|
dispatchFilter | Modules.Nfc.NfcForegroundDispatchFilter | A filter specifying intent, intent filters and technology lists used to match dispatch intents. | No |
enableForegroundNdefPush #
Enable NDEF message push over P2P.
This method was deprecated in API level 14. Use setNdefPushMessage instead.
| Name | Type | Summary | Optional |
|---|---|---|---|
message | Modules.Nfc.NdefMessage | An NDEF message to push over NFC. | No |
enableReader #
Enable reader mode in the current activity.
In this mode the NFC controller will only act as an NFC tag reader/writer, thus disabling any peer-to-peer (Android Beam) and card-emulation modes of the NFC adapter on this device.
See also:
Android Developer - enableReaderMode
| Name | Type | Summary | Optional |
|---|---|---|---|
Callback | Callback | Tag data | No |
onNewIntent #
Processes a new intent received by an application.
The NFC tag dispatch system will dispatch an intent to your application when it discovers a tag
that matches your application's intent filters. Intents received by your application after it has
started MUST be passed to this method in order to be processed and parsed for processing by your
application. If the intent is recognized as an NFC action, this method will call your
onNdefDiscovered, onTagDiscovered, or onTechDiscovered' callback with the parsed information. You should add an event listener to the current activity for the newintent` event in your application
and call this method with the received intent.
| Name | Type | Summary | Optional |
|---|---|---|---|
intent | Titanium.Android.Intent | The Android intent received by your application | No |
setBeamPushUris #
Set one or more Uris to send using Android Beam.
See also
[setBeamPushUris](http://developer.android.com/reference/android/nfc/NfcAdapter.html#setBeamPushUris(android.net.Uri[], android.app.Activity))
| Name | Type | Summary | Optional |
|---|---|---|---|
Uris | Array<String> | An array of Uri(s) to push over Android Beam. | No |
setNdefPushMessage #
Set a static Modules.Nfc.NdefMessage to send using Android Beam.
See also:
[setNdefPushMessage](http://developer.android.com/reference/android/nfc/NfcAdapter.html#setNdefPushMessage(android.nfc.NdefMessage, android.app.Activity, android.app.Activity...))
| Name | Type | Summary | Optional |
|---|---|---|---|
message | Modules.Nfc.NdefMessage | An NDEF message to push over NFC, or null to disable. | No |
Examples #
Foreground Dispatch (Android)
This example uses foreground dispatch to receive NDEF messages only when the application is in the foreground.
// 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,
onTechDiscovered: handleDiscovery
});
// It's possible that the device does not support NFC. Check it here
// before we go any further.
if (!nfcAdapter.isEnabled()) {
alert('NFC is not enabled on this device');
return;
}
// All tag scans are received by the activity as a new intent. Each
// scan intent needs to be passed to the nfc adapter for processing.
var act = Ti.Android.currentActivity;
act.addEventListener('newintent', function(e) {
nfcAdapter.onNewIntent(e.intent);
});
// To enable NFC dispatching only while the application is in the foreground,
// the application must signal the module whenever the application state changes.
act.addEventListener('resume', function(e) {
nfcAdapter.enableForegroundDispatch(dispatchFilter);
});
act.addEventListener('pause', function(e) {
nfcAdapter.disableForegroundDispatch();
});
// This application is only interested in receiving NFC messages while
// in the foreground. So the dispatch filter must be defined to identify
// what types of NFC messages to receive.
dispatchFilter = nfc.createNfcForegroundDispatchFilter({
intentFilters: [
{ action: nfc.ACTION_NDEF_DISCOVERED, mimeType: 'text/plain' },
{ action: nfc.ACTION_NDEF_DISCOVERED, scheme: 'http', host: 'www.appcelerator.com' }
],
techLists: [
[ "android.nfc.tech.NfcF" ],
[ "android.nfc.tech.Ndef" ],
[ "android.nfc.tech.MifareClassic" ],
[ "android.nfc.tech.NfcA" ]
]
});Push Message (Android)
This example sets a default push message to send using Android Beam.
// Create the NFC adapter to be associated with this activity.
// There should only be ONE adapter created per activity.
nfcAdapter = nfc.createNfcAdapter({});
// It's possible that the device does not support NFC. Check it here
// before we go any further.
if (!nfcAdapter.isNdefPushEnabled()) {
alert('NFC is not enabled on this device');
return;
}
// Set the default Ndef message to send when tapped
var textRecord = nfc.createNdefRecordText({
text: "NDEF Push Sample"
});
var msg = nfc.createNdefMessage({
records: [ textRecord ]
});
nfcAdapter.setNdefPushMessage(msg);Push Message Callback (Android)
This example uses the push message callback to dynamically create the NDEF message as needed.
// Create the NFC adapter to be associated with this activity.
// There should only be ONE adapter created per activity.
nfcAdapter = nfc.createNfcAdapter({
onPushMessage: function() {
if (sendText) {
ndefRecord = nfc.createNdefRecordText({
text: "Hello"
});
} else {
ndefRecord = nfc.createNdefRecordUri({
uri: "http://www.appcelerator.com"
});
}
return nfc.createNdefMessage({
records: [
ndefRecord
]
});
}
});
// It's possible that the device does not support NFC. Check it here
// before we go any further.
if (!nfcAdapter.isEnabled()) {
alert('NFC is not enabled on this device');
return;
}