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.
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.
| |||||||||||
Disable NDEF message push over P2P.
| |||||||||||
Enable foreground dispatch to the given Activity.
| |||||||||||
Enable NDEF message push over P2P while this Activity is in the foreground.
| |||||||||||
This method is deprecated.
use
getDefaultAdapter(Context)
| |||||||||||
Helper to get the default NFC Adapter.
| |||||||||||
Return true if this NFC Adapter has any features enabled.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
![]() |
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 |
---|
Disable NDEF message push over P2P.
After calling enableForegroundNdefPush(Activity, NdefMessage)
, 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 Foreground activity |
---|
IllegalStateException | if the Activity has already been paused |
---|---|
if this Android device does not support NDEF push |
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 |
---|
Enable NDEF message push over P2P while this Activity is in the foreground.
For this to function properly the other NFC device being scanned must support the "com.android.npp" NDEF push protocol. Support for this protocol is currently optional for Android NFC devices.
This method must be called from the main thread.
NOTE: While foreground NDEF push is active standard tag dispatch is disabled.
Only the foreground activity may receive tag discovered dispatches via
enableForegroundDispatch(Activity, PendingIntent, IntentFilter[], String[][])
.
Requires the NFC
permission.
activity | the foreground Activity |
---|---|
msg | a NDEF Message to push over P2P |
IllegalStateException | if the Activity is not currently in the foreground |
---|---|
if this Android device does not support NDEF push |
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.