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
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" ]
]
});
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);
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;
}
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
.
Indicates if the proxy will bubble an event to its parent.
Some proxies (most commonly views) have a relationship to other proxies, often established by the add() method. For example, for a button added to a window, a click event on the button would bubble up to the window. Other common parents are table sections to their rows, table views to their sections, and scrollable views to their views. Set this property to false to disable the bubbling to the proxy's parent.
Default: true
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.
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.
Callback function used to dynamically generate one or more Uris to send using Android Beam.
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
Callback function to execute when a tag with NDEF payload is discovered.
Callback function to execute when a tag with NDEF payload is discovered.
See also: ACTION_NDEF_DISCOVERED
Callback function to execute when a session was invalidated (either errored by the system or cancelled by the user).
Callback function to execute when a session was invalidated (either errored by the system or cancelled by the user).
Callback function to execute on successful Android Beam operation.
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 in the Android Developer Reference.
Callback function used to dynamically generated NDEF messsages to send using Android Beam.
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 in the Android Developer Reference.
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
Adds the specified callback as an event listener for the named event.
Name of the event.
Callback function to invoke when the event is fired.
Applies the properties to the proxy.
Properties are supplied as a dictionary. Each key-value pair in the object is applied to the proxy such that myproxy[key] = value.
A dictionary of properties to apply.
Disable foreground dispatch to the current activity.
Disable NDEF message push over P2P.
This method was deprecated in API level 14. Use setNdefPushMessage
instead.
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
A filter specifying intent, intent filters and technology lists used to match dispatch intents.
Enable NDEF message push over P2P.
This method was deprecated in API level 14. Use setNdefPushMessage
instead.
An NDEF message to push over NFC.
Fires a synthesized event to any registered listeners.
Name of the event.
A dictionary of keys and values to add to the Titanium.Event object sent to the listeners.
Return true if this NFC Adapter has any features enabled.
Return true if the NDEF Push (Android Beam) feature is enabled.
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.
The Android intent received by your application
Removes the specified callback as an event listener for the named event.
Multiple listeners can be registered for the same event, so the
callback
parameter is used to determine which listener to remove.
When adding a listener, you must save a reference to the callback function in order to remove the listener later:
var listener = function() { Ti.API.info("Event listener called."); }
window.addEventListener('click', listener);
To remove the listener, pass in a reference to the callback function:
window.removeEventListener('click', listener);
Name of the event.
Callback function to remove. Must be the same function passed to addEventListener
.
Set one or more Uris to send using Android Beam.
See also setBeamPushUris
An array of Uri(s) to push over Android Beam.
Sets the value of the bubbleParent property.
New value for the property.
Sets the value of the invalidateAfterFirstRead property.
New value for the property.
Sets the value of the lifecycleContainer property.
New value for the property.
Set a static Modules.Nfc.NdefMessage to send using Android Beam.
See also: setNdefPushMessage
An NDEF message to push over NFC, or null to disable.
Sets the value of the onBeamPushUris property.
New value for the property.
Sets the value of the onNdefDiscovered property.
New value for the property.
Sets the value of the onNdefInvalidated property.
New value for the property.
Sets the value of the onPushComplete property.
New value for the property.
Sets the value of the onPushMessage property.
New value for the property.
Sets the value of the onTagDiscovered property.
New value for the property.
Sets the value of the onTechDiscovered property.
New value for the property.