Skip to content

Commit 28319c0

Browse files
committed
More documentation updates for NFC.
Change-Id: Ie134d4296ee12333e6647ce76126f9767ba1ba34
1 parent 584afa8 commit 28319c0

File tree

3 files changed

+115
-45
lines changed

3 files changed

+115
-45
lines changed

core/java/android/nfc/NfcAdapter.java

Lines changed: 77 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@
2626
import android.content.IntentFilter;
2727
import android.content.pm.IPackageManager;
2828
import android.content.pm.PackageManager;
29+
import android.nfc.tech.MifareClassic;
30+
import android.nfc.tech.Ndef;
31+
import android.nfc.tech.NfcA;
32+
import android.nfc.tech.NfcF;
2933
import android.os.IBinder;
30-
import android.os.Parcel;
3134
import android.os.RemoteException;
3235
import android.os.ServiceManager;
3336
import android.util.Log;
@@ -44,30 +47,85 @@ public final class NfcAdapter {
4447

4548
/**
4649
* Intent to start an activity when a tag with NDEF payload is discovered.
47-
* If the tag has and NDEF payload this intent is started before
48-
* {@link #ACTION_TECH_DISCOVERED}.
4950
*
50-
* If any activities respond to this intent neither
51+
* <p>The system inspects the first {@link NdefRecord} in the first {@link NdefMessage} and
52+
* looks for a URI, SmartPoster, or MIME record. If a URI or SmartPoster record is found the
53+
* intent will contain the URI in its data field. If a MIME record is found the intent will
54+
* contain the MIME type in its type field. This allows activities to register
55+
* {@link IntentFilter}s targeting specific content on tags. Activities should register the
56+
* most specific intent filters possible to avoid the activity chooser dialog, which can
57+
* disrupt the interaction with the tag as the user interacts with the screen.
58+
*
59+
* <p>If the tag has an NDEF payload this intent is started before
60+
* {@link #ACTION_TECH_DISCOVERED}. If any activities respond to this intent neither
5161
* {@link #ACTION_TECH_DISCOVERED} or {@link #ACTION_TAG_DISCOVERED} will be started.
5262
*/
5363
@SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
5464
public static final String ACTION_NDEF_DISCOVERED = "android.nfc.action.NDEF_DISCOVERED";
5565

5666
/**
57-
* Intent to started when a tag is discovered. The data URI is formated as
58-
* {@code vnd.android.nfc://tag/} with the path having a directory entry for each technology
59-
* in the {@link Tag#getTechList()} is sorted ascending order.
67+
* Intent to start an activity when a tag is discovered and activities are registered for the
68+
* specific technologies on the tag.
69+
*
70+
* <p>To receive this intent an activity must include an intent filter
71+
* for this action and specify the desired tech types in a
72+
* manifest <code>meta-data</code> entry. Here is an example manfiest entry:
73+
* <pre>
74+
* &lt;activity android:name=".nfc.TechFilter" android:label="NFC/TechFilter"&gt;
75+
* &lt;!-- Add a technology filter --&gt;
76+
* &lt;intent-filter&gt;
77+
* &lt;action android:name="android.nfc.action.TECH_DISCOVERED" /&gt;
78+
* &lt;/intent-filter&gt;
79+
*
80+
* &lt;meta-data android:name="android.nfc.action.TECH_DISCOVERED"
81+
* android:resource="@xml/filter_nfc"
82+
* /&gt;
83+
* &lt;/activity&gt;
84+
* </pre>
85+
*
86+
* <p>The meta-data XML file should contain one or more <code>tech-list</code> entries
87+
* each consisting or one or more <code>tech</code> entries. The <code>tech</code> entries refer
88+
* to the qualified class name implementing the technology, for example "android.nfc.tech.NfcA".
6089
*
61-
* This intent is started after {@link #ACTION_NDEF_DISCOVERED} and before
62-
* {@link #ACTION_TAG_DISCOVERED}
90+
* <p>A tag matches if any of the
91+
* <code>tech-list</code> sets is a subset of {@link Tag#getTechList() Tag.getTechList()}. Each
92+
* of the <code>tech-list</code>s is considered independently and the
93+
* activity is considered a match is any single <code>tech-list</code> matches the tag that was
94+
* discovered. This provides AND and OR semantics for filtering desired techs. Here is an
95+
* example that will match any tag using {@link NfcF} or any tag using {@link NfcA},
96+
* {@link MifareClassic}, and {@link Ndef}:
6397
*
64-
* If any activities respond to this intent {@link #ACTION_TAG_DISCOVERED} will not be started.
98+
* <pre>
99+
* &lt;resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"&gt;
100+
* &lt;!-- capture anything using NfcF --&gt;
101+
* &lt;tech-list&gt;
102+
* &lt;tech&gt;android.nfc.tech.NfcF&lt;/tech&gt;
103+
* &lt;/tech-list&gt;
104+
*
105+
* &lt;!-- OR --&gt;
106+
*
107+
* &lt;!-- capture all MIFARE Classics with NDEF payloads --&gt;
108+
* &lt;tech-list&gt;
109+
* &lt;tech&gt;android.nfc.tech.NfcA&lt;/tech&gt;
110+
* &lt;tech&gt;android.nfc.tech.MifareClassic&lt;/tech&gt;
111+
* &lt;tech&gt;android.nfc.tech.Ndef&lt;/tech&gt;
112+
* &lt;/tech-list&gt;
113+
* &lt;/resources&gt;
114+
* </pre>
115+
*
116+
* <p>This intent is started after {@link #ACTION_NDEF_DISCOVERED} and before
117+
* {@link #ACTION_TAG_DISCOVERED}. If any activities respond to {@link #ACTION_NDEF_DISCOVERED}
118+
* this intent will not be started. If any activities respond to this intent
119+
* {@link #ACTION_TAG_DISCOVERED} will not be started.
65120
*/
66121
@SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
67122
public static final String ACTION_TECH_DISCOVERED = "android.nfc.action.TECH_DISCOVERED";
68123

69124
/**
70125
* Intent to start an activity when a tag is discovered.
126+
*
127+
* <p>This intent will not be started when a tag is discovered if any activities respond to
128+
* {@link #ACTION_NDEF_DISCOVERED} or {@link #ACTION_TECH_DISCOVERED} for the current tag.
71129
*/
72130
@SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
73131
public static final String ACTION_TAG_DISCOVERED = "android.nfc.action.TAG_DISCOVERED";
@@ -79,17 +137,23 @@ public final class NfcAdapter {
79137
public static final String ACTION_TAG_LEFT_FIELD = "android.nfc.action.TAG_LOST";
80138

81139
/**
82-
* Mandatory Tag extra for the ACTION_TAG intents.
140+
* Mandatory extra containing the {@link Tag} that was discovered for the
141+
* {@link #ACTION_NDEF_DISCOVERED}, {@link #ACTION_TECH_DISCOVERED}, and
142+
* {@link #ACTION_TAG_DISCOVERED} intents.
83143
*/
84144
public static final String EXTRA_TAG = "android.nfc.extra.TAG";
85145

86146
/**
87-
* Optional NdefMessage[] extra for the ACTION_TAG intents.
147+
* Optional extra containing an array of {@link NdefMessage} present on the discovered tag for
148+
* the {@link #ACTION_NDEF_DISCOVERED}, {@link #ACTION_TECH_DISCOVERED}, and
149+
* {@link #ACTION_TAG_DISCOVERED} intents.
88150
*/
89151
public static final String EXTRA_NDEF_MESSAGES = "android.nfc.extra.NDEF_MESSAGES";
90152

91153
/**
92-
* Optional byte[] extra for the tag identifier.
154+
* Optional extra containing a byte array containing the ID of the discovered tag for
155+
* the {@link #ACTION_NDEF_DISCOVERED}, {@link #ACTION_TECH_DISCOVERED}, and
156+
* {@link #ACTION_TAG_DISCOVERED} intents.
93157
*/
94158
public static final String EXTRA_ID = "android.nfc.extra.ID";
95159

core/java/android/nfc/Tag.java

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -38,63 +38,70 @@
3838
* <p>
3939
* {@link Tag} is an immutable object that represents the state of a NFC tag at
4040
* the time of discovery. It can be used as a handle to {@link TagTechnology} classes
41-
* to perform advanced operations, or directly queried for its ID ({@link #getId} and the
42-
* set of technologies it contains ({@link #getTechList}). Arrays passed to and
43-
* returned by this class are *not* cloned, so be careful not to modify them.
41+
* to perform advanced operations, or directly queried for its ID via {@link #getId} and the
42+
* set of technologies it contains via {@link #getTechList}. Arrays passed to and
43+
* returned by this class are <em>not</em> cloned, so be careful not to modify them.
4444
* <p>
4545
* A new tag object is created every time a tag is discovered (comes into range), even
4646
* if it is the same physical tag. If a tag is removed and then returned into range, then
4747
* only the most recent tag object can be successfully used to create a {@link TagTechnology}.
4848
*
4949
* <h3>Tag Dispatch</h3>
5050
* When a tag is discovered, a {@link Tag} object is created and passed to a
51-
* single application via the {@link NfcAdapter#EXTRA_TAG} extra in a
52-
* {@link Context#startActivity} {@link android.content.Intent}. A four stage dispatch is used to select the
53-
* most appropriate application to handle the tag. The Android OS executes each stage in order,
54-
* and completes dispatch as soon as a single matching application is found. If there are multiple
55-
* matching applications found at any one stage then the Android Activity Chooser dialog is shown
56-
* to allow the user to select the application.
51+
* single activity via the {@link NfcAdapter#EXTRA_TAG} extra in an
52+
* {@link android.content.Intent} via {@link Context#startActivity}. A four stage dispatch is used
53+
* to select the
54+
* most appropriate activity to handle the tag. The Android OS executes each stage in order,
55+
* and completes dispatch as soon as a single matching activity is found. If there are multiple
56+
* matching activities found at any one stage then the Android activity chooser dialog is shown
57+
* to allow the user to select the activity to receive the tag.
58+
*
59+
* <p>The Tag dispatch mechanism was designed to give a high probability of dispatching
60+
* a tag to the correct activity without showing the user an activity chooser dialog.
61+
* This is important for NFC interactions because they are very transient -- if a user has to
62+
* move the Android device to choose an application then the connection will likely be broken.
63+
*
5764
* <h4>1. Foreground activity dispatch</h4>
58-
* A foreground activity that has called {@link NfcAdapter#enableForegroundDispatch} is
59-
* given priority. See the documentation on {#link NfcAdapter#enableForegroundDispatch} for
65+
* A foreground activity that has called
66+
* {@link NfcAdapter#enableForegroundDispatch NfcAdapter.enableForegroundDispatch()} is
67+
* given priority. See the documentation on
68+
* {@link NfcAdapter#enableForegroundDispatch NfcAdapter.enableForegroundDispatch()} for
6069
* its usage.
6170
* <h4>2. NDEF data dispatch</h4>
62-
* If the tag contains NDEF data, then {@link Context#startActivity} is called with
63-
* {@link NfcAdapter#ACTION_NDEF_DISCOVERED} and a data URI determined from the
64-
* first NDEF Record in the first NDEF Message in the Tag. This allows NDEF tags to be given
65-
* priority dispatch to applications that can handle the content.
71+
* If the tag contains NDEF data the system inspects the first {@link NdefRecord} in the first
72+
* {@link NdefMessage}. If the record is a URI, SmartPoster, or MIME data
73+
* {@link Context#startActivity} is called with {@link NfcAdapter#ACTION_NDEF_DISCOVERED}. For URI
74+
* and SmartPoster records the URI is put into the intent's data field. For MIME records the MIME
75+
* type is put in the intent's type field. This allows activities to register to be launched only
76+
* when data they know how to handle is present on a tag. This is the preferred method of handling
77+
* data on a tag since NDEF data can be stored on many types of tags and doesn't depend on a
78+
* specific tag technology.
6679
* See {@link NfcAdapter#ACTION_NDEF_DISCOVERED} for more detail. If the tag does not contain
67-
* NDEF data, or if no application is registered
68-
* for {@link NfcAdapter#ACTION_NDEF_DISCOVERED} with a matching data URI then dispatch moves
69-
* to stage 3.
80+
* NDEF data, or if no activity is registered
81+
* for {@link NfcAdapter#ACTION_NDEF_DISCOVERED} with a matching data URI or MIME type then dispatch
82+
* moves to stage 3.
7083
* <h4>3. Tag Technology dispatch</h4>
7184
* {@link Context#startActivity} is called with {@link NfcAdapter#ACTION_TECH_DISCOVERED} to
72-
* dispatch the tag to an application that can handle the technologies present on the tag.
85+
* dispatch the tag to an activity that can handle the technologies present on the tag.
7386
* Technologies are defined as sub-classes of {@link TagTechnology}, see the package
74-
* {@link android.nfc.tech}. The Android OS looks for an application that can handle one or
75-
* more technologies in the tag. See {@link NfcAdapter#ACTION_TECH_DISCOVERED for more detail.
87+
* {@link android.nfc.tech}. The Android OS looks for an activity that can handle one or
88+
* more technologies in the tag. See {@link NfcAdapter#ACTION_TECH_DISCOVERED} for more detail.
7689
* <h4>4. Fall-back dispatch</h4>
77-
* If no application has been matched, then {@link Context#startActivity} is called with
90+
* If no activity has been matched then {@link Context#startActivity} is called with
7891
* {@link NfcAdapter#ACTION_TAG_DISCOVERED}. This is intended as a fall-back mechanism.
7992
* See {@link NfcAdapter#ACTION_TAG_DISCOVERED}.
8093
*
81-
* <p>
82-
* <i>The Tag dispatch mechanism was designed to give a high probability of dispatching
83-
* a tag to the correct application without showing the user an Application Chooser dialog.
84-
* This is important for NFC interactions because they are very transient - if a user has to
85-
* move the Android device to choose an application then the connection is broken.</i>
86-
*
8794
* <h3>NFC Tag Background</h3>
8895
* An NFC tag is a passive NFC device, powered by the NFC field of this Android device while
89-
* it is in range. Tag's can come in many forms, such as stickers, cards, key fob, or
96+
* it is in range. Tag's can come in many forms, such as stickers, cards, key fobs, or
9097
* even embedded in a more sophisticated device.
9198
* <p>
9299
* Tags can have a wide range of capabilities. Simple tags just offer read/write semantics,
93100
* and contain some one time
94101
* programmable areas to make read-only. More complex tags offer math operations
95102
* and per-sector access control and authentication. The most sophisticated tags
96-
* contain operating environments such as Javacard, allowing complex interactions with the
97-
* applets executing on the tag. Use {@link TagTechnology} classes to access a broad
103+
* contain operating environments allowing complex interactions with the
104+
* code executing on the tag. Use {@link TagTechnology} classes to access a broad
98105
* range of capabilities available in NFC tags.
99106
* <p>
100107
*/

core/java/android/nfc/tech/Ndef.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ public final class Ndef extends BasicTagTechnology {
137137
* @param tag an MIFARE Classic compatible tag
138138
* @return MIFARE Classic object
139139
*/
140-
141140
public static Ndef get(Tag tag) {
142141
if (!tag.hasTech(TagTechnology.NDEF)) return null;
143142
try {

0 commit comments

Comments
 (0)