Most visited

Recently visited

Added in API level 10

Tag

public final class Tag
extends Object implements Parcelable

java.lang.Object
   ↳ android.nfc.Tag


代表已发现的NFC标签。

Tag是一个不可变的对象,表示发现时NFC标签的状态。 它可以用作TagTechnology类的句柄来执行高级操作,或者通过getId()和它通过getTechList()包含的一组技术直接查询其ID。 传递给这个类并返回的数组不会被克隆,因此请小心不要修改它们。

每当标签被发现时(即使它是相同的物理标签),都会创建一个新的标签对象。 如果标签被移除并返回到范围内,则只有最近的标签对象才能成功用于创建TagTechnology

Tag Dispatch

When a tag is discovered, a Tag object is created and passed to a single activity via the EXTRA_TAG extra in an Intent via startActivity(Intent). A four stage dispatch is used to select the most appropriate activity to handle the tag. The Android OS executes each stage in order, and completes dispatch as soon as a single matching activity is found. If there are multiple matching activities found at any one stage then the Android activity chooser dialog is shown to allow the user to select the activity to receive the tag.

标签调度机制被设计为给标签分配正确的活动的概率高,而不会向用户显示活动选择器对话框。 这对于NFC交互非常重要,因为它们非常短暂 - 如果用户必须移动Android设备才能选择应用程序,那么连接可能会中断。

1. Foreground activity dispatch

A foreground activity that has called NfcAdapter.enableForegroundDispatch() is given priority. See the documentation on NfcAdapter.enableForegroundDispatch() for its usage.

2. NDEF data dispatch

If the tag contains NDEF data the system inspects the first NdefRecord in the first NdefMessage. If the record is a URI, SmartPoster, or MIME data startActivity(Intent) is called with ACTION_NDEF_DISCOVERED. For URI and SmartPoster records the URI is put into the intent's data field. For MIME records the MIME type is put in the intent's type field. This allows activities to register to be launched only when data they know how to handle is present on a tag. This is the preferred method of handling data on a tag since NDEF data can be stored on many types of tags and doesn't depend on a specific tag technology. See ACTION_NDEF_DISCOVERED for more detail. If the tag does not contain NDEF data, or if no activity is registered for ACTION_NDEF_DISCOVERED with a matching data URI or MIME type then dispatch moves to stage 3.

3. Tag Technology dispatch

startActivity(Intent) is called with ACTION_TECH_DISCOVERED to dispatch the tag to an activity that can handle the technologies present on the tag. Technologies are defined as sub-classes of TagTechnology, see the package android.nfc.tech. The Android OS looks for an activity that can handle one or more technologies in the tag. See ACTION_TECH_DISCOVERED for more detail.

4. Fall-back dispatch

If no activity has been matched then startActivity(Intent) is called with ACTION_TAG_DISCOVERED. This is intended as a fall-back mechanism. See ACTION_TAG_DISCOVERED.

NFC Tag Background

An NFC tag is a passive NFC device, powered by the NFC field of this Android device while it is in range. Tag's can come in many forms, such as stickers, cards, key fobs, or even embedded in a more sophisticated device.

标签可以具有广泛的功能。 简单标签只提供读/写语义,并包含一些一次性可编程区域以实现只读。 更复杂的标签提供数学运算和每个扇区的访问控制和认证。 最复杂的标签包含允许与标签上执行的代码进行复杂交互的操作环境。 使用TagTechnology类可以访问NFC标签中可用的各种功能。

Summary

Inherited constants

From interface android.os.Parcelable

Fields

public static final Creator<Tag> CREATOR

Public methods

int describeContents()

描述此Parcelable实例的封送表示中包含的特殊对象的种类。

byte[] getId()

获取标签标识符(如果有的话)。

String[] getTechList()

获取此标记中可用的技术,作为完全限定的类名称。

String toString()

用于调试的标签的人类可读描述。

void writeToParcel(Parcel dest, int flags)

将此对象平铺到一个包裹中。

Inherited methods

From class java.lang.Object
From interface android.os.Parcelable

Fields

CREATOR

Added in API level 10
Creator<Tag> CREATOR

Public methods

describeContents

Added in API level 10
int describeContents ()

描述此Parcelable实例的封送表示中包含的特殊对象的种类。 例如,如果对象将在writeToParcel(Parcel, int)的输出中包含writeToParcel(Parcel, int) ,则此方法的返回值必须包含CONTENTS_FILE_DESCRIPTOR位。

Returns
int a bitmask indicating the set of special object types marshaled by this Parcelable object instance.

getId

Added in API level 10
byte[] getId ()

获取标签标识符(如果有的话)。

标签标识是低级序列号,用于防冲突和识别。

大多数标签都有一个稳定的唯一标识符(UID),但是一些标签会在每次发现它们(RID)时生成一个随机标识符,而且有些标签根本没有标识符(字节数组将为零)。

ID的大小和格式特定于标签使用的RF技术。

该功能检索在发现时确定的ID,并且不执行任何进一步的RF通信或块。

Returns
byte[] ID as byte array, never null

getTechList

Added in API level 10
String[] getTechList ()

获取此标记中可用的技术,作为完全限定的类名称。

一种技术是TagTechnology接口的实现,可以通过使用此标记调用实现上的静态get(Tag)方法来实例化。 TagTechnology对象可以用于在标签上执行高级技术特定操作。

Android定义了必须由所有Android NFC设备正确列举的强制性技术,以及一组可选的专有技术。 有关更多详细信息,请参见TagTechnology

返回数组的排序是未定义的,不应该依赖。

Returns
String[] an array of fully-qualified TagTechnology class-names.

toString

Added in API level 10
String toString ()

用于调试的标签的人类可读描述。

Returns
String a string representation of the object.

writeToParcel

Added in API level 10
void writeToParcel (Parcel dest, 
                int flags)

将此对象平铺到一个包裹中。

Parameters
dest Parcel: The Parcel in which the object should be written.
flags int: Additional flags about how the object should be written. May be 0 or PARCELABLE_WRITE_RETURN_VALUE.

Hooray!