java.lang.Object | |
↳ | android.nfc.NfcAdapter |
Represents the local NFC adapter.
Use the helper getDefaultAdapter(Context)
to get the default NFC
adapter for this Android device.
Nested Classes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
NfcAdapter.CreateNdefMessageCallback | A callback to be invoked when another NFC device capable of NDEF push (Android Beam) is within range. | ||||||||||
NfcAdapter.OnNdefPushCompleteCallback | A callback to be invoked when the system successfully delivers your NdefMessage
to another device. |
Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
String | ACTION_NDEF_DISCOVERED | Intent to start an activity when a tag with NDEF payload is discovered. | |||||||||
String | ACTION_TAG_DISCOVERED | Intent to start an activity when a tag is discovered. | |||||||||
String | ACTION_TECH_DISCOVERED | Intent to start an activity when a tag is discovered and activities are registered for the specific technologies on the tag. | |||||||||
String | EXTRA_ID | Optional extra containing a byte array containing the ID of the discovered tag for
the ACTION_NDEF_DISCOVERED , ACTION_TECH_DISCOVERED , and
ACTION_TAG_DISCOVERED intents. |
|||||||||
String | EXTRA_NDEF_MESSAGES | Optional extra containing an array of NdefMessage present on the discovered tag for
the ACTION_NDEF_DISCOVERED , ACTION_TECH_DISCOVERED , and
ACTION_TAG_DISCOVERED intents. |
|||||||||
String | EXTRA_TAG | Mandatory extra containing the Tag that was discovered for the
ACTION_NDEF_DISCOVERED , ACTION_TECH_DISCOVERED , and
ACTION_TAG_DISCOVERED intents. |
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Disable foreground dispatch to the given activity.
| |||||||||||
This method is deprecated.
use
setNdefPushMessage(NdefMessage, Activity, Activity...) instead
| |||||||||||
Enable foreground dispatch to the given Activity.
| |||||||||||
This method is deprecated.
use
setNdefPushMessage(NdefMessage, Activity, Activity...) instead
| |||||||||||
This method is deprecated.
use
getDefaultAdapter(Context)
| |||||||||||
Helper to get the default NFC Adapter.
| |||||||||||
Return true if this NFC Adapter has any features enabled.
| |||||||||||
Set the
NdefMessage to push over NFC during the specified activities. | |||||||||||
Set the callback to create a
NdefMessage to push over NFC. | |||||||||||
Set the callback on a successful NDEF push over NFC.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.lang.Object
|
Intent to start an activity when a tag with NDEF payload is discovered.
The system inspects the first NdefRecord
in the first NdefMessage
and
looks for a URI, SmartPoster, or MIME record. If a URI or SmartPoster record is found the
intent will contain the URI in its data field. If a MIME record is found the intent will
contain the MIME type in its type field. This allows activities to register
IntentFilter
s targeting specific content on tags. Activities should register the
most specific intent filters possible to avoid the activity chooser dialog, which can
disrupt the interaction with the tag as the user interacts with the screen.
If the tag has an NDEF payload this intent is started before
ACTION_TECH_DISCOVERED
. If any activities respond to this intent neither
ACTION_TECH_DISCOVERED
or ACTION_TAG_DISCOVERED
will be started.
Intent to start an activity when a tag is discovered.
This intent will not be started when a tag is discovered if any activities respond to
ACTION_NDEF_DISCOVERED
or ACTION_TECH_DISCOVERED
for the current tag.
Intent to start an activity when a tag is discovered and activities are registered for the specific technologies on the tag.
To receive this intent an activity must include an intent filter
for this action and specify the desired tech types in a
manifest meta-data
entry. Here is an example manfiest entry:
<activity android:name=".nfc.TechFilter" android:label="NFC/TechFilter"> <!-- Add a technology filter --> <intent-filter> <action android:name="android.nfc.action.TECH_DISCOVERED" /> </intent-filter> <meta-data android:name="android.nfc.action.TECH_DISCOVERED" android:resource="@xml/filter_nfc" /> </activity>
The meta-data XML file should contain one or more tech-list
entries
each consisting or one or more tech
entries. The tech
entries refer
to the qualified class name implementing the technology, for example "android.nfc.tech.NfcA".
A tag matches if any of the
tech-list
sets is a subset of Tag.getTechList()
. Each
of the tech-list
s is considered independently and the
activity is considered a match is any single tech-list
matches the tag that was
discovered. This provides AND and OR semantics for filtering desired techs. Here is an
example that will match any tag using NfcF
or any tag using NfcA
,
MifareClassic
, and Ndef
:
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> <!-- capture anything using NfcF --> <tech-list> <tech>android.nfc.tech.NfcF</tech> </tech-list> <!-- OR --> <!-- capture all MIFARE Classics with NDEF payloads --> <tech-list> <tech>android.nfc.tech.NfcA</tech> <tech>android.nfc.tech.MifareClassic</tech> <tech>android.nfc.tech.Ndef</tech> </tech-list> </resources>
This intent is started after ACTION_NDEF_DISCOVERED
and before
ACTION_TAG_DISCOVERED
. If any activities respond to ACTION_NDEF_DISCOVERED
this intent will not be started. If any activities respond to this intent
ACTION_TAG_DISCOVERED
will not be started.
Optional extra containing a byte array containing the ID of the discovered tag for
the ACTION_NDEF_DISCOVERED
, ACTION_TECH_DISCOVERED
, and
ACTION_TAG_DISCOVERED
intents.
Optional extra containing an array of NdefMessage
present on the discovered tag for
the ACTION_NDEF_DISCOVERED
, ACTION_TECH_DISCOVERED
, and
ACTION_TAG_DISCOVERED
intents.
Mandatory extra containing the Tag
that was discovered for the
ACTION_NDEF_DISCOVERED
, ACTION_TECH_DISCOVERED
, and
ACTION_TAG_DISCOVERED
intents.
Disable foreground dispatch to the given activity.
After calling enableForegroundDispatch(Activity, PendingIntent, IntentFilter[], String[][])
, an activity
must call this method before its onPause()
callback
completes.
This method must be called from the main thread.
Requires the NFC
permission.
activity | the Activity to disable dispatch to |
---|
IllegalStateException | if the Activity has already been paused |
---|
This method is deprecated.
use setNdefPushMessage(NdefMessage, Activity, Activity...)
instead
Disable NDEF message push over P2P.
After calling enableForegroundNdefPush(Activity, NdefMessage)
, an activity
must call this method before its onPause()
callback
completes.
Strongly recommend to use the new setNdefPushMessage(NdefMessage, Activity, Activity...)
instead: it automatically hooks into your activity life-cycle,
so you do not need to call enable/disable in your onResume/onPause.
This method must be called from the main thread.
Requires the NFC
permission.
activity | the Foreground activity |
---|
IllegalStateException | if the Activity has already been paused |
---|
Enable foreground dispatch to the given Activity.
This will give give priority to the foreground activity when
dispatching a discovered Tag
to an application.
If any IntentFilters are provided to this method they are used to match dispatch Intents
for both the ACTION_NDEF_DISCOVERED
and
ACTION_TAG_DISCOVERED
. Since ACTION_TECH_DISCOVERED
relies on meta data outside of the IntentFilter matching for that dispatch Intent is handled
by passing in the tech lists separately. Each first level entry in the tech list represents
an array of technologies that must all be present to match. If any of the first level sets
match then the dispatch is routed through the given PendingIntent. In other words, the second
level is ANDed together and the first level entries are ORed together.
If you pass null
for both the filters
and techLists
parameters
that acts a wild card and will cause the foreground activity to receive all tags via the
ACTION_TAG_DISCOVERED
intent.
This method must be called from the main thread, and only when the activity is in the
foreground (resumed). Also, activities must call disableForegroundDispatch(Activity)
before
the completion of their onPause()
callback to disable foreground dispatch
after it has been enabled.
Requires the NFC
permission.
activity | the Activity to dispatch to |
---|---|
intent | the PendingIntent to start for the dispatch |
filters | the IntentFilters to override dispatching for, or null to always dispatch |
techLists | the tech lists used to perform matching for dispatching of the
ACTION_TECH_DISCOVERED intent |
IllegalStateException | if the Activity is not currently in the foreground |
---|
This method is deprecated.
use setNdefPushMessage(NdefMessage, Activity, Activity...)
instead
Enable NDEF message push over NFC while this Activity is in the foreground.
You must explicitly call this method every time the activity is
resumed, and you must call disableForegroundNdefPush(Activity)
before
your activity completes onPause()
.
Strongly recommend to use the new setNdefPushMessage(NdefMessage, Activity, Activity...)
instead: it automatically hooks into your activity life-cycle,
so you do not need to call enable/disable in your onResume/onPause.
For NDEF push to function properly the other NFC device must support either NFC Forum's SNEP (Simple Ndef Exchange Protocol), or Android's "com.android.npp" (Ndef Push Protocol). This was optional on Gingerbread level Android NFC devices, but SNEP is mandatory on Ice-Cream-Sandwich and beyond.
This method must be called from the main thread.
Requires the NFC
permission.
activity | foreground activity |
---|---|
message | a NDEF Message to push over NFC |
IllegalStateException | if the activity is not currently in the foreground |
---|
This method is deprecated.
use getDefaultAdapter(Context)
Get a handle to the default NFC Adapter on this Android device.
Most Android devices will only have one NFC Adapter (NFC Controller).
Helper to get the default NFC Adapter.
Most Android devices will only have one NFC Adapter (NFC Controller).
This helper is the equivalent of:
NfcManager manager = (NfcManager) context.getSystemService(Context.NFC_SERVICE);
NfcAdapter adapter = manager.getDefaultAdapter();
context | the calling application's context |
---|
Return true if this NFC Adapter has any features enabled.
Application may use this as a helper to suggest that the user should turn on NFC in Settings.
If this method returns false, the NFC hardware is guaranteed not to generate or respond to any NFC transactions.
Set the NdefMessage
to push over NFC during the specified activities.
This method may be called at any time, but the NDEF message is only made available for NDEF push when one of the specified activities is in resumed (foreground) state.
Only one NDEF message can be pushed by the currently resumed activity.
If both setNdefPushMessage(NdefMessage, Activity, Activity...)
and
setNdefPushMessageCallback(NfcAdapter.CreateNdefMessageCallback, Activity, Activity...)
are set then
the callback will take priority.
Pass a null NDEF message to disable foreground NDEF push in the specified activities.
At least one activity must be specified, and usually only one is necessary.
Requires the NFC
permission.
message | NDEF message to push over NFC, or null to disable |
---|---|
activity | an activity in which NDEF push should be enabled to share the provided NDEF message |
activities | optional additional activities that should also enable NDEF push with the provided NDEF message |
Set the callback to create a NdefMessage
to push over NFC.
This method may be called at any time, but this callback is only made if one of the specified activities is in resumed (foreground) state.
Only one NDEF message can be pushed by the currently resumed activity.
If both setNdefPushMessage(NdefMessage, Activity, Activity...)
and
setNdefPushMessageCallback(NfcAdapter.CreateNdefMessageCallback, Activity, Activity...)
are set then
the callback will take priority.
Pass a null callback to disable the callback in the specified activities.
At least one activity must be specified, and usually only one is necessary.
Requires the NFC
permission.
callback | callback, or null to disable |
---|---|
activity | an activity in which NDEF push should be enabled to share an NDEF message that's retrieved from the provided callback |
activities | optional additional activities that should also enable NDEF push using the provided callback |
Set the callback on a successful NDEF push over NFC.
This method may be called at any time, but NDEF push and this callback can only occur when one of the specified activities is in resumed (foreground) state.
One or more activities must be specified.
Requires the NFC
permission.
callback | callback, or null to disable |
---|---|
activity | an activity to enable the callback (at least one is required) |
activities | zero or more additional activities to enable to callback |