public class

Nfc

extends Object
java.lang.Object
   ↳ mobisocial.nfc.Nfc

Class Overview

This class acts as an abstraction layer for Android's Nfc stack. The goals of this project are to:

  • Provide compatibility for Nfc across platforms.
  • Degrade gracefully on platforms and devices lacking Nfc support.
  • Extend the capabilities of Nfc by following Connection Handover Requests.
  • Simplify the Nfc experience for developers.

The Nfc class must be run from a foregrounded activity. It requires a few lifecycle events be triggered during an activity's runtime:


 class MyActivity extends Activity {
 
   public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     mNfc = new Nfc(this);
     mNfc.onCreate(this);
   }
 
   public void onResume() {
     super.onResume();
     mNfc.onResume(this);
     // your activity's onResume code
   }

  public void onPause() {
    super.onPause();
    mNfc.onPause(this);
    // your activity's onPause code
  }
  
  public void onNewIntent(Intent intent) {
    if (mNfc.onNewIntent(this, intent)) {
      return;
    }
    // your activity's onNewIntent code
  }
 }
 

Your application must hold the android.permission.NFC permission to use this class. However, this class will degrade gracefully on devices lacking Nfc capabilities.

The Nfc interface can be in one of three modes: MODE_WRITE, for writing to a passive NFC tag, and MODE_EXCHANGE, in which the interface can read data from passive tags and exchange data with another active Nfc device, or MODE_PASSTHROUGH which disables this interface.

Summary

Nested Classes
interface Nfc.OnTagWriteListener Interface definition for a callback called after an attempt to write an Nfc tag. 
Constants
String ACTION_HANDLE_NDEF The action of an ordered broadcast intent for applications to handle a received NDEF messages.
String ACTION_SET_NDEF A broadcasted intent used to set an NDEF message for use in a Connection Handover, for devices that do not have an active NFC radio.
int MODE_EXCHANGE Nfc interface mode for reading data from a passive tag or exchanging information with another active device.
int MODE_PASSTHROUGH Nfc interface mode in which Nfc interaction is disabled for this class.
int MODE_WRITE Nfc interface mode for writing data to a passive tag.
Public Constructors
Nfc(Activity activity, IntentFilter[] intentFilters, String[][] techLists)
Nfc(Activity activity)
Public Methods
synchronized void addNdefHandler(Integer priority, NdefHandler handler)
void addNdefHandler(NdefHandler handler)
Sets a callback to call when an Nfc tag is written.
synchronized void clearNdefHandlers()
void clearSharing()
Removes any message from being shared with an interested reader.
void disable()
Puts the interface in mode MODE_PASSTHROUGH.
void disableConnectionHandover()
Disallows connection handover requests.
void enableConnectionHandover()
Enables support for connection handover requests.
void enableExchangeMode()
Puts the interface in mode MODE_EXCHANGE, the default mode of operation for this Nfc interface.
void enableTagWriteMode(NdefMessage ndef)
Puts the interface in mode MODE_WRITE.
ConnectionHandoverManager getConnectionHandoverManager()
boolean isConnectionHandoverEnabled()
Returns true if connection handovers are currently supported.
boolean isNativeNfcAvailable()
Returns true if this device has a native NFC implementation.
void onCreate(Activity activity)
boolean onNewIntent(Activity activity, Intent intent)
Call this method in your activity's onNewIntent(Intent) method body.
void onPause(Activity activity)
Call this method in your Activity's onPause() method body.
void onResume(Activity activity)
Call this method in your Activity's onResume() method body.
void setOnTagWriteListener(Nfc.OnTagWriteListener listener)
Sets a callback to call when an Nfc tag is written.
void share(Uri uri)
void share(NdefMessage ndefMessage)
Makes an ndef message available to any interested reader.
void share(URL url)
void share(String mimeType, byte[] data)
[Expand]
Inherited Methods
From class java.lang.Object

Constants

protected static final String ACTION_HANDLE_NDEF

The action of an ordered broadcast intent for applications to handle a received NDEF messages. Such intents are broadcast from connection handover services. This library sets the result code to Activity.RESULT_CANCELED, indicating the foreground application has consumed the intent.

Constant Value: "mobisocial.intent.action.HANDLE_NDEF"

protected static final String ACTION_SET_NDEF

A broadcasted intent used to set an NDEF message for use in a Connection Handover, for devices that do not have an active NFC radio.

Constant Value: "mobisocial.intent.action.SET_NDEF"

public static final int MODE_EXCHANGE

Nfc interface mode for reading data from a passive tag or exchanging information with another active device. See addNdefHandler(NdefHandler) and share(NdefMessage) for handling the actual data.

Constant Value: 1 (0x00000001)

public static final int MODE_PASSTHROUGH

Nfc interface mode in which Nfc interaction is disabled for this class.

Constant Value: 0 (0x00000000)

public static final int MODE_WRITE

Nfc interface mode for writing data to a passive tag.

Constant Value: 2 (0x00000002)

Public Constructors

public Nfc (Activity activity, IntentFilter[] intentFilters, String[][] techLists)

public Nfc (Activity activity)

Public Methods

public synchronized void addNdefHandler (Integer priority, NdefHandler handler)

public void addNdefHandler (NdefHandler handler)

Sets a callback to call when an Nfc tag is written.

public synchronized void clearNdefHandlers ()

public void clearSharing ()

Removes any message from being shared with an interested reader.

public void disable ()

Puts the interface in mode MODE_PASSTHROUGH.

public void disableConnectionHandover ()

Disallows connection handover requests.

public void enableConnectionHandover ()

Enables support for connection handover requests.

public void enableExchangeMode ()

Puts the interface in mode MODE_EXCHANGE, the default mode of operation for this Nfc interface.

public void enableTagWriteMode (NdefMessage ndef)

Puts the interface in mode MODE_WRITE.

Parameters
ndef The NdefMessage to write to a discovered tag.
Throws
NullPointerException if ndef is null.

public ConnectionHandoverManager getConnectionHandoverManager ()

public boolean isConnectionHandoverEnabled ()

Returns true if connection handovers are currently supported.

public boolean isNativeNfcAvailable ()

Returns true if this device has a native NFC implementation.

public void onCreate (Activity activity)

public boolean onNewIntent (Activity activity, Intent intent)

Call this method in your activity's onNewIntent(Intent) method body.

Returns
  • true if this call consumed the intent.

public void onPause (Activity activity)

Call this method in your Activity's onPause() method body.

public void onResume (Activity activity)

Call this method in your Activity's onResume() method body.

public void setOnTagWriteListener (Nfc.OnTagWriteListener listener)

Sets a callback to call when an Nfc tag is written.

public void share (Uri uri)

public void share (NdefMessage ndefMessage)

Makes an ndef message available to any interested reader.

See Also

public void share (URL url)

public void share (String mimeType, byte[] data)