Modules.Nfc.NfcAdapter
> 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

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;
}
  • 1.0.0
  • 1.0.0
Defined By

Properties

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
Indicates if the proxy will bubble an event to its parent. ...

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

  • 3.0.0
  • 3.0.0
  • 3.0.0
Modules.Nfc.NfcAdapter
: Boolean
Session will automatically invalidate after the first NDEF tag is read successfully when this is set to true, and th...

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.

  • 2.0.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
Modules.Nfc.NfcAdapter
onBeamPushUris : Callback

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

  • 1.2.0
Modules.Nfc.NfcAdapter
onNdefDiscovered : Callback<NdefDiscovered>

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

  • 1.0.0
  • 2.0.0
Modules.Nfc.NfcAdapter
onNdefInvalidated : Callback<NdefInvalidated>

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

  • 2.0.1
Modules.Nfc.NfcAdapter
onPushComplete : Callback

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.

  • 1.0.0
Modules.Nfc.NfcAdapter
onPushMessage : Callback

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.

  • 1.0.0
Modules.Nfc.NfcAdapter
onTagDiscovered : Callback<NdefDiscovered>

Callback function to execute when a tag is discovered.

Callback function to execute when a tag is discovered.

See also: ACTION_TAG_DISCOVERED

  • 1.0.0
Modules.Nfc.NfcAdapter
: Callback<NdefDiscovered>
Callback function to execute when a tag is discovered and activities are registered for the specific technologies on ...

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

  • 1.0.0
Defined By

Methods

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

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

Parameters

  • name : String

    Name of the event.

  • callback : Callback<Object>

    Callback function to invoke when the event is fired.

Returns

  • void
Applies the properties to the proxy. ...

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.

  • 3.0.0
  • 3.0.0
  • 3.0.0

Parameters

  • props : Dictionary

    A dictionary of properties to apply.

Returns

  • void
Modules.Nfc.NfcAdapter
( )
Disable foreground dispatch to the current activity. ...

Disable foreground dispatch to the current activity.

  • 1.0.0

Returns

  • void
Modules.Nfc.NfcAdapter
( )
Disable NDEF message push over P2P. ...

Disable NDEF message push over P2P.

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

  • 1.0.0

Returns

  • void
Modules.Nfc.NfcAdapter
( dispatchFilter )
Enable foreground dispatch to the current activity. ...

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

  • 1.0.0

Parameters

Returns

  • void
Modules.Nfc.NfcAdapter
( message )
Enable NDEF message push over P2P. ...

Enable NDEF message push over P2P.

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

  • 1.0.0

Parameters

Returns

  • void
Fires a synthesized event to any registered listeners. ...

Fires a synthesized event to any registered listeners.

Parameters

  • 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

  • void
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 bubbleParent property. ...

Gets the value of the bubbleParent property.

  • 3.0.0
  • 3.0.0
  • 3.0.0

Returns

  • Boolean
Modules.Nfc.NfcAdapter
( ) : Boolean
Gets the value of the invalidateAfterFirstRead property. ...

Gets the value of the invalidateAfterFirstRead property.

  • 2.0.0

Returns

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

Gets the value of the lifecycleContainer property.

  • 3.6.0

Returns

Modules.Nfc.NfcAdapter
( ) : Callback
Gets the value of the onBeamPushUris property. ...

Gets the value of the onBeamPushUris property.

  • 1.2.0

Returns

  • Callback
Modules.Nfc.NfcAdapter
( ) : Callback<NdefDiscovered>
Gets the value of the onNdefDiscovered property. ...

Gets the value of the onNdefDiscovered property.

  • 1.0.0
  • 2.0.0

Returns

Modules.Nfc.NfcAdapter
( ) : Callback<NdefInvalidated>
Gets the value of the onNdefInvalidated property. ...

Gets the value of the onNdefInvalidated property.

  • 2.0.1

Returns

Modules.Nfc.NfcAdapter
( ) : Callback
Gets the value of the onPushComplete property. ...

Gets the value of the onPushComplete property.

  • 1.0.0

Returns

  • Callback
Modules.Nfc.NfcAdapter
( ) : Callback
Gets the value of the onPushMessage property. ...

Gets the value of the onPushMessage property.

  • 1.0.0

Returns

  • Callback
Modules.Nfc.NfcAdapter
( ) : Callback<NdefDiscovered>
Gets the value of the onTagDiscovered property. ...

Gets the value of the onTagDiscovered property.

  • 1.0.0

Returns

Modules.Nfc.NfcAdapter
( ) : Callback<NdefDiscovered>
Gets the value of the onTechDiscovered property. ...

Gets the value of the onTechDiscovered property.

  • 1.0.0

Returns

Modules.Nfc.NfcAdapter
( ) : Boolean
Return true if this NFC Adapter has any features enabled. ...

Return true if this NFC Adapter has any features enabled.

  • 1.0.0

Returns

  • Boolean
Modules.Nfc.NfcAdapter
( ) : Boolean
Return true if the NDEF Push (Android Beam) feature is enabled. ...

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

  • 1.0.0

Returns

  • Boolean
Modules.Nfc.NfcAdapter
( intent )
Processes a new intent received by an application. ...

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.

  • 1.0.0

Parameters

Returns

  • void
Removes the specified callback as an event listener for the named event. ...

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 : String

    Name of the event.

  • callback : Callback<Object>

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

Returns

  • void
Modules.Nfc.NfcAdapter
( Uris )
Set one or more Uris to send using Android Beam. ...

Set one or more Uris to send using Android Beam.

See also setBeamPushUris

  • 1.2.0

Parameters

  • Uris : Array<String>

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

Returns

  • void
Sets the value of the bubbleParent property. ...

Sets the value of the bubbleParent property.

  • 3.0.0
  • 3.0.0
  • 3.0.0

Parameters

  • bubbleParent : Boolean

    New value for the property.

Returns

  • void
Modules.Nfc.NfcAdapter
( invalidateAfterFirstRead )
Sets the value of the invalidateAfterFirstRead property. ...

Sets the value of the invalidateAfterFirstRead property.

  • 2.0.0

Parameters

  • invalidateAfterFirstRead : Boolean

    New value for the property.

Returns

  • void
Sets the value of the lifecycleContainer property. ...

Sets the value of the lifecycleContainer property.

  • 3.6.0

Parameters

Returns

  • void
Modules.Nfc.NfcAdapter
( message )
Set a static Modules.Nfc.NdefMessage to send using Android Beam. ...

Set a static Modules.Nfc.NdefMessage to send using Android Beam.

See also: setNdefPushMessage

  • 1.0.0

Parameters

Returns

  • void
Modules.Nfc.NfcAdapter
( onBeamPushUris )
Sets the value of the onBeamPushUris property. ...

Sets the value of the onBeamPushUris property.

  • 1.2.0

Parameters

  • onBeamPushUris : Callback

    New value for the property.

Returns

  • void
Modules.Nfc.NfcAdapter
( onNdefDiscovered )
Sets the value of the onNdefDiscovered property. ...

Sets the value of the onNdefDiscovered property.

  • 1.0.0
  • 2.0.0

Parameters

  • onNdefDiscovered : Callback<NdefDiscovered>

    New value for the property.

Returns

  • void
Modules.Nfc.NfcAdapter
( onNdefInvalidated )
Sets the value of the onNdefInvalidated property. ...

Sets the value of the onNdefInvalidated property.

  • 2.0.1

Parameters

Returns

  • void
Modules.Nfc.NfcAdapter
( onPushComplete )
Sets the value of the onPushComplete property. ...

Sets the value of the onPushComplete property.

  • 1.0.0

Parameters

  • onPushComplete : Callback

    New value for the property.

Returns

  • void
Modules.Nfc.NfcAdapter
( onPushMessage )
Sets the value of the onPushMessage property. ...

Sets the value of the onPushMessage property.

  • 1.0.0

Parameters

  • onPushMessage : Callback

    New value for the property.

Returns

  • void
Modules.Nfc.NfcAdapter
( onTagDiscovered )
Sets the value of the onTagDiscovered property. ...

Sets the value of the onTagDiscovered property.

  • 1.0.0

Parameters

  • onTagDiscovered : Callback<NdefDiscovered>

    New value for the property.

Returns

  • void
Modules.Nfc.NfcAdapter
( onTechDiscovered )
Sets the value of the onTechDiscovered property. ...

Sets the value of the onTechDiscovered property.

  • 1.0.0

Parameters

  • onTechDiscovered : Callback<NdefDiscovered>

    New value for the property.

Returns

  • void