Skip to content

Commit 630ac8d

Browse files
Nick PellyAndroid (Google) Code Review
authored andcommitted
Merge "NFC documentation overhaul." into gingerbread
2 parents 8444023 + 74fe6c6 commit 630ac8d

File tree

13 files changed

+857
-364
lines changed

13 files changed

+857
-364
lines changed

core/java/android/nfc/NfcAdapter.java

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@
3333
import android.util.Log;
3434

3535
/**
36-
* Represents the device's local NFC adapter.
36+
* Represents the local NFC adapter.
3737
* <p>
3838
* Use the helper {@link #getDefaultAdapter(Context)} to get the default NFC
3939
* adapter for this Android device.
40+
* <p>
4041
*/
4142
public final class NfcAdapter {
4243
private static final String TAG = "NFC";
@@ -359,14 +360,13 @@ public void attemptDeadServiceRecovery(Exception e) {
359360

360361
/**
361362
* Return true if this NFC Adapter has any features enabled.
362-
* <p>
363-
* If this method returns false, then applications should request the user
364-
* turn on NFC tag discovery in Settings.
365-
* <p>
366-
* If this method returns false, the NFC hardware is guaranteed not to
367-
* perform or respond to any NFC communication.
368363
*
369-
* @return true if this NFC Adapter is enabled to discover new tags
364+
* <p>Application may use this as a helper to suggest that the user
365+
* should turn on NFC in Settings.
366+
* <p>If this method returns false, the NFC hardware is guaranteed not to
367+
* generate or respond to any NFC transactions.
368+
*
369+
* @return true if this NFC Adapter has any features enabled
370370
*/
371371
public boolean isEnabled() {
372372
try {
@@ -414,17 +414,24 @@ public boolean disable() {
414414
}
415415

416416
/**
417-
* Enables foreground dispatching to the given Activity. This will force all NFC Intents that
418-
* match the given filters to be delivered to the activity bypassing the standard dispatch
419-
* mechanism. If no IntentFilters are given all the PendingIntent will be invoked for every
420-
* dispatch Intent.
417+
* Enable foreground dispatch to the given Activity.
418+
*
419+
* <p>This will give give priority to the foreground activity when
420+
* dispatching a discovered {@link Tag} to an application.
421+
*
422+
* <p>Activities must call {@link #disableForegroundDispatch} in
423+
* their {@link Activity#onPause} callback.
424+
*
425+
* <p>a null set of intent filters will cause the forground activity
426+
* to receive all tags.
421427
*
422-
* This method must be called from the main thread.
428+
* <p>This method must be called from the main thread, and
429+
* only when the activity is in the foreground (resumed). *
423430
*
424431
* @param activity the Activity to dispatch to
425432
* @param intent the PendingIntent to start for the dispatch
426433
* @param filters the IntentFilters to override dispatching for, or null to always dispatch
427-
* @throws IllegalStateException
434+
* @throws IllegalStateException if the Activity is not currently in the foreground
428435
*/
429436
public void enableForegroundDispatch(Activity activity, PendingIntent intent,
430437
IntentFilter[] filters, String[][] techLists) {
@@ -450,13 +457,16 @@ public void enableForegroundDispatch(Activity activity, PendingIntent intent,
450457
}
451458

452459
/**
453-
* Disables foreground activity dispatching setup with
454-
* {@link #enableForegroundDispatch}.
460+
* Disable foreground dispatch to the given activity.
455461
*
456-
* <p>This must be called before the Activity returns from
457-
* it's <code>onPause()</code> or this method will throw an IllegalStateException.
462+
* <p>After calling {@link #enableForegroundDispatch}, an activity
463+
* must call this method before its {@link Activity#onPause} callback
464+
* completes.
458465
*
459466
* <p>This method must be called from the main thread.
467+
*
468+
* @param activity the Activity to disable dispatch to
469+
* @throws IllegalStateException if the Activity has already been paused
460470
*/
461471
public void disableForegroundDispatch(Activity activity) {
462472
ActivityThread.currentActivityThread().unregisterOnActivityPausedListener(activity,
@@ -484,13 +494,22 @@ void disableForegroundDispatchInternal(Activity activity, boolean force) {
484494
}
485495

486496
/**
487-
* Enable NDEF message push over P2P while this Activity is in the foreground. For this to
488-
* function properly the other NFC device being scanned must support the "com.android.npp"
489-
* NDEF push protocol.
497+
* Enable NDEF message push over P2P while this Activity is in the foreground.
498+
*
499+
* <p>For this to function properly the other NFC device being scanned must
500+
* support the "com.android.npp" NDEF push protocol. Support for this
501+
* protocol is currently optional for Android NFC devices.
502+
*
503+
* <p>This method must be called from the main thread.
490504
*
491505
* <p><em>NOTE</em> While foreground NDEF push is active standard tag dispatch is disabled.
492506
* Only the foreground activity may receive tag discovered dispatches via
493507
* {@link #enableForegroundDispatch}.
508+
*
509+
* @param activity the foreground Activity
510+
* @param msg a NDEF Message to push over P2P
511+
* @throws IllegalStateException if the Activity is not currently in the foreground
512+
* @throws OperationNotSupportedException if this Android device does not support NDEF push
494513
*/
495514
public void enableForegroundNdefPush(Activity activity, NdefMessage msg) {
496515
if (activity == null || msg == null) {
@@ -510,13 +529,17 @@ public void enableForegroundNdefPush(Activity activity, NdefMessage msg) {
510529
}
511530

512531
/**
513-
* Disables foreground NDEF push setup with
514-
* {@link #enableForegroundNdefPush}.
532+
* Disable NDEF message push over P2P.
515533
*
516-
* <p>This must be called before the Activity returns from
517-
* it's <code>onPause()</code> or this method will throw an IllegalStateException.
534+
* <p>After calling {@link #enableForegroundNdefPush}, an activity
535+
* must call this method before its {@link Activity#onPause} callback
536+
* completes.
518537
*
519538
* <p>This method must be called from the main thread.
539+
*
540+
* @param activity the Foreground activity
541+
* @throws IllegalStateException if the Activity has already been paused
542+
* @throws OperationNotSupportedException if this Android device does not support NDEF push
520543
*/
521544
public void disableForegroundNdefPush(Activity activity) {
522545
ActivityThread.currentActivityThread().unregisterOnActivityPausedListener(activity,

core/java/android/nfc/NfcSecureElement.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ public final class NfcSecureElement {
3535
private static final String TAG = "NfcSecureElement";
3636

3737
private INfcSecureElement mService;
38-
39-
38+
39+
4040
/**
4141
* @hide
4242
*/
@@ -68,7 +68,7 @@ public int openSecureElementConnection(String seType) throws IOException {
6868

6969

7070
public byte [] exchangeAPDU(int handle,byte [] data) throws IOException {
71-
71+
7272

7373
// Perform exchange APDU
7474
try {
@@ -85,7 +85,7 @@ public int openSecureElementConnection(String seType) throws IOException {
8585
}
8686

8787
public void closeSecureElementConnection(int handle) throws IOException {
88-
88+
8989
try {
9090
int status = mService.closeSecureElementConnection(handle);
9191
// Handle potential errors
@@ -96,14 +96,14 @@ public void closeSecureElementConnection(int handle) throws IOException {
9696
Log.e(TAG, "RemoteException in closeSecureElement(): ", e);
9797
}
9898
}
99-
100-
99+
100+
101101
/**
102102
* Returns target type. constants.
103-
*
103+
*
104104
* @return Secure Element technology type. The possible values are defined in
105105
* {@link TagTechnology}
106-
*
106+
*
107107
*/
108108
public int[] getSecureElementTechList(int handle) throws IOException {
109109
try {
@@ -113,16 +113,16 @@ public int[] getSecureElementTechList(int handle) throws IOException {
113113
return null;
114114
}
115115
}
116-
116+
117117
/**
118118
* Returns Secure Element UID.
119-
*
119+
*
120120
* @return Secure Element UID.
121121
*/
122122
public byte[] getSecureElementUid(int handle) throws IOException {
123-
123+
124124
byte[] uid = null;
125-
try {
125+
try {
126126
uid = mService.getSecureElementUid(handle);
127127
// Handle potential errors
128128
if (uid == null) {

core/java/android/nfc/Tag.java

Lines changed: 86 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package android.nfc;
1818

19+
import android.content.Context;
1920
import android.nfc.tech.IsoDep;
2021
import android.nfc.tech.MifareClassic;
2122
import android.nfc.tech.MifareUltralight;
@@ -33,27 +34,69 @@
3334
import java.util.Arrays;
3435

3536
/**
36-
* Represents a (generic) discovered tag.
37+
* Represents an NFC tag that has been discovered.
3738
* <p>
38-
* A tag is a passive NFC element, such as NFC Forum Tag's, MIFARE class Tags,
39-
* Sony FeliCa Tags, etc.
39+
* {@link Tag} is an immutable object that represents the state of a NFC tag at
40+
* 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.
44+
* <p>
45+
* A new tag object is created every time a tag is discovered (comes into range), even
46+
* if it is the same physical tag. If a tag is removed and then returned into range, then
47+
* only the most recent tag object can be successfully used to create a {@link TagTechnology}.
48+
*
49+
* <h3>Tag Dispatch</h3>
50+
* 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.
57+
* <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
60+
* its usage.
61+
* <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.
66+
* 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.
70+
* <h4>3. Tag Technology dispatch</h4>
71+
* {@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.
73+
* 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.
76+
* <h4>4. Fall-back dispatch</h4>
77+
* If no application has been matched, then {@link Context#startActivity} is called with
78+
* {@link NfcAdapter#ACTION_TAG_DISCOVERED}. This is intended as a fall-back mechanism.
79+
* See {@link NfcAdapter#ACTION_TAG_DISCOVERED}.
80+
*
4081
* <p>
41-
* Tag's have a type and usually have a UID.
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+
*
87+
* <h3>NFC Tag Background</h3>
88+
* 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
90+
* even embedded in a more sophisticated device.
4291
* <p>
43-
* {@link Tag} objects are passed to applications via the {@link NfcAdapter#EXTRA_TAG} extra
44-
* in {@link NfcAdapter#ACTION_TAG_DISCOVERED} intents. A {@link Tag} object is immutable
45-
* and represents the state of the tag at the time of discovery. It can be
46-
* directly queried for its UID and Type, or used to create a {@link TagTechnology} using the
47-
* static <code>get()</code> methods on the varios tech classes.
92+
* Tags can have a wide range of capabilities. Simple tags just offer read/write semantics,
93+
* and contain some one time
94+
* programmable areas to make read-only. More complex tags offer math operations
95+
* 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
98+
* range of capabilities available in NFC tags.
4899
* <p>
49-
* A {@link Tag} can be used to create a {@link TagTechnology} only while the tag is in
50-
* range. If it is removed and then returned to range, then the most recent
51-
* {@link Tag} object (in {@link NfcAdapter#ACTION_TAG_DISCOVERED}) should be used to create a
52-
* {@link TagTechnology}.
53-
* <p>This is an immutable data class. All properties are set at Tag discovery
54-
* time and calls on this class will retrieve those read-only properties, and
55-
* not cause any further RF activity or block. Note however that arrays passed to and
56-
* returned by this class are *not* cloned, so be careful not to modify them.
57100
*/
58101
public final class Tag implements Parcelable {
59102
/*package*/ final byte[] mId;
@@ -149,21 +192,35 @@ public int getServiceHandle() {
149192

150193
/**
151194
* Get the Tag Identifier (if it has one).
152-
* <p>Tag ID is usually a serial number for the tag.
153-
*
154-
* @return ID, or null if it does not exist
195+
* <p>The tag identifier is a low level serial number, used for anti-collision
196+
* and identification.
197+
* <p> Most tags have a stable unique identifier
198+
* (UID), but some tags will generate a random ID every time they are discovered
199+
* (RID), and there are some tags with no ID at all (the byte array will be zero-sized).
200+
* <p> The size and format of an ID is specific to the RF technology used by the tag.
201+
* <p> This function retrieves the ID as determined at discovery time, and does not
202+
* perform any further RF communication or block.
203+
* @return ID as byte array, never null
155204
*/
156205
public byte[] getId() {
157206
return mId;
158207
}
159208

160209
/**
161-
* Returns technologies present in the tag that this implementation understands,
162-
* or a zero length array if there are no supported technologies on this tag.
163-
*
164-
* The elements of the list are the names of the classes implementing the technology.
165-
*
210+
* Get the technologies available in this tag, as fully qualified class names.
211+
* <p>
212+
* A technology is an implementation of the {@link TagTechnology} interface,
213+
* and can be instantiated by calling the static <code>get(Tag)</code>
214+
* method on the implementation with this Tag. The {@link TagTechnology}
215+
* object can then be used to perform advanced, technology-specific operations on a tag.
216+
* <p>
217+
* Android defines a mandatory set of technologies that must be correctly
218+
* enumerated by all Android NFC devices, and an optional
219+
* set of proprietary technologies.
220+
* See {@link TagTechnology} for more details.
221+
* <p>
166222
* The ordering of the returned array is undefined and should not be relied upon.
223+
* @return an array of fully-qualified {@link TagTechnology} class-names.
167224
*/
168225
public String[] getTechList() {
169226
return mTechStringList;
@@ -176,7 +233,7 @@ public boolean hasTech(int techType) {
176233
}
177234
return false;
178235
}
179-
236+
180237
/** @hide */
181238
public Bundle getTechExtras(int tech) {
182239
int pos = -1;
@@ -198,6 +255,9 @@ public INfcTag getTagService() {
198255
return mTagService;
199256
}
200257

258+
/**
259+
* Human-readable description of the tag, for debugging.
260+
*/
201261
@Override
202262
public String toString() {
203263
StringBuilder sb = new StringBuilder("TAG ")

0 commit comments

Comments
 (0)