java.lang.Object | |
↳ | mobisocial.nfc.Nfc |
This class acts as an abstraction layer for Android's Nfc stack. The goals of this project are to:
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.
share(NdefMessage)
and similar, to share messages with other Nfc devices.
addNdefHandler(NdefHandler)
, for acting on received messages.
enableTagWriteMode(NdefMessage)
, to write to physical Nfc tags.
Nested Classes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
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 | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Sets a callback to call when an Nfc tag is written.
| |||||||||||
Removes any message from being shared with an interested reader.
| |||||||||||
Puts the interface in mode
MODE_PASSTHROUGH . | |||||||||||
Disallows connection handover requests.
| |||||||||||
Enables support for connection handover requests.
| |||||||||||
Puts the interface in mode
MODE_EXCHANGE ,
the default mode of operation for this Nfc interface. | |||||||||||
Puts the interface in mode
MODE_WRITE . | |||||||||||
Returns true if connection handovers are currently supported.
| |||||||||||
Returns true if this device has a native NFC implementation.
| |||||||||||
Call this method in your activity's onNewIntent(Intent) method body.
| |||||||||||
Call this method in your Activity's onPause() method body.
| |||||||||||
Call this method in your Activity's onResume() method body.
| |||||||||||
Sets a callback to call when an Nfc tag is written.
| |||||||||||
Makes an ndef message available to any interested reader.
| |||||||||||
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.lang.Object
|
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.
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.
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.
Nfc interface mode in which Nfc interaction is disabled for this class.
Nfc interface mode for writing data to a passive tag.
Sets a callback to call when an Nfc tag is written.
Removes any message from being shared with an interested reader.
Disallows connection handover requests.
Enables support for connection handover requests.
Puts the interface in mode MODE_EXCHANGE
,
the default mode of operation for this Nfc interface.
Puts the interface in mode MODE_WRITE
.
ndef | The NdefMessage to write to a discovered tag. |
---|
NullPointerException | if ndef is null. |
---|
Returns true if connection handovers are currently supported.
Returns true if this device has a native NFC implementation.
Call this method in your activity's onNewIntent(Intent) method body.
Call this method in your Activity's onResume() method body.
Sets a callback to call when an Nfc tag is written.
Makes an ndef message available to any interested reader.