Skip to content

Commit 58c9d47

Browse files
jhamAndroid (Google) Code Review
authored andcommitted
Merge "More documentation updates for NFC." into gingerbread
2 parents a3f53af + 28319c0 commit 58c9d47

File tree

3 files changed

+117
-47
lines changed

3 files changed

+117
-47
lines changed

core/java/android/nfc/NfcAdapter.java

Lines changed: 79 additions & 15 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;
@@ -43,30 +46,85 @@ public final class NfcAdapter {
4346

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

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

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

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

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

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

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
@@ -136,7 +136,6 @@ public final class Ndef extends BasicTagTechnology {
136136
* @param tag an MIFARE Classic compatible tag
137137
* @return MIFARE Classic object
138138
*/
139-
140139
public static Ndef get(Tag tag) {
141140
if (!tag.hasTech(TagTechnology.NDEF)) return null;
142141
try {

0 commit comments

Comments
 (0)