# Modules.Nfc.NfcAdapter

Represents the local NFC adapter.

Availability
1.0.0
1.0.0

# Overview

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 (opens new window)

# 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;
}

# Properties

# apiName READONLY

Availability
3.2.0
3.2.0
apiName :String

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, Titanium.UI.Button returns Ti.UI.Button.


# bubbleParent

Availability
3.0.0
3.0.0
bubbleParent :Boolean

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


# invalidateAfterFirstRead

Availability
2.0.0
invalidateAfterFirstRead :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 INVALIDATION_ERROR_FIRST_NDEF_TAG_READ in this case.


# lifecycleContainer

Availability
3.6.0

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.


# onBeamPushUris

Availability
1.2.0
onBeamPushUris :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

Availability
1.0.0
2.0.0
onNdefDiscovered :Callback<NdefDiscovered>

Callback function to execute when a tag with NDEF payload is discovered.


# onNdefInvalidated

Availability
2.0.1
onNdefInvalidated :Callback<NdefInvalidated>

Callback function to execute when a session was invalidated (either errored by the system or cancelled by the user).


# onPushComplete

Availability
1.0.0
onPushComplete :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

Availability
1.0.0
onPushMessage :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

Availability
1.0.0
onTagDiscovered :Callback<NdefDiscovered>

Callback function to execute when a tag is discovered.


# onTechDiscovered

Availability
1.0.0
onTechDiscovered :Callback<NdefDiscovered>

Callback function to execute when a tag is discovered and activities are registered for the specific technologies on the tag.

# Methods

# addEventListener

Availability
1.0.0
1.0.0
addEventListener(name, callback) void

Adds the specified callback as an event listener for the named event.

Parameters

Name Type Description
name String

Name of the event.

callback Callback<Titanium.Event>

Callback function to invoke when the event is fired.

Returns

Type
void

# applyProperties

Availability
3.0.0
3.0.0
applyProperties(props) void

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.

Parameters

Name Type Description
props Dictionary

A dictionary of properties to apply.

Returns

Type
void

# disableForegroundDispatch

Availability
1.0.0
disableForegroundDispatch() void

Disable foreground dispatch to the current activity.

Returns

Type
void

# disableForegroundNdefPush

Availability
1.0.0
disableForegroundNdefPush() void

Disable NDEF message push over P2P.

This method was deprecated in API level 14. Use setNdefPushMessage instead.

Returns

Type
void

# enableForegroundDispatch

Availability
1.0.0
enableForegroundDispatch(dispatchFilter) void

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[][]))

Parameters

Name Type Description
dispatchFilter Modules.Nfc.NfcForegroundDispatchFilter

A filter specifying intent, intent filters and technology lists used to match dispatch intents.

Returns

Type
void

# enableForegroundNdefPush

Availability
1.0.0
enableForegroundNdefPush(message) void

Enable NDEF message push over P2P.

This method was deprecated in API level 14. Use setNdefPushMessage instead.

Parameters

Name Type Description
message Modules.Nfc.NdefMessage

An NDEF message to push over NFC.

Returns

Type
void

# fireEvent

Availability
1.0.0
1.0.0
fireEvent(name[, event]) void

Fires a synthesized event to any registered listeners.

Parameters

Name Type Description
name String

Name of the event.

event Dictionary

A dictionary of keys and values to add to the Titanium.Event object sent to the listeners.

Returns

Type
void

# isEnabled

Availability
1.0.0
isEnabled() Boolean

Return true if this NFC Adapter has any features enabled.

Returns

Type
Boolean

# isNdefPushEnabled

Availability
1.0.0
isNdefPushEnabled() Boolean

Return true if the NDEF Push (Android Beam) feature is enabled.

Returns

Type
Boolean

# onNewIntent

Availability
1.0.0
onNewIntent(intent) void

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 thenewintent` event in your application and call this method with the received intent.

Parameters

Name Type Description
intent Titanium.Android.Intent

The Android intent received by your application

Returns

Type
void

# removeEventListener

Availability
1.0.0
1.0.0
removeEventListener(name, callback) void

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);

Parameters

Name Type Description
name String

Name of the event.

callback Callback<Titanium.Event>

Callback function to remove. Must be the same function passed to addEventListener.

Returns

Type
void

# setBeamPushUris

Availability
1.2.0
setBeamPushUris(Uris) void

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))

Parameters

Name Type Description
Uris Array<String>

An array of Uri(s) to push over Android Beam.

Returns

Type
void

# setNdefPushMessage

Availability
1.0.0
setNdefPushMessage(message) void

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...))

Parameters

Name Type Description
message Modules.Nfc.NdefMessage

An NDEF message to push over NFC, or null to disable.

Returns

Type
void