This document describes the basic NFC tasks you perform in Android. It explains how to send and receive NFC data in the form of NDEF messages and describes the Android framework APIs that support these features. For more advanced topics, including a discussion of working with non-NDEF data, see Advanced NFC.
该文档描述了在Android中执行基本的NFC任务。它解释了如何以NDEF消息的形式发送和接收NFC数据,并描述了支持这些功能的Android框架API。有关更高级的主题,包括使用非NDEF数据的讨论,请参阅高级NFC。
There are two major use cases when working with NDEF data and Android:
在Android上使用NDEF数据时有两个主要的用例:
- Reading NDEF data from an NFC tag
1.从NFC标签读取NDEF数据
2.Beaming NDEF messages from one device to another with Android Beam™
2.使用Android Beam从一个设备发送NDEF消息到另一个设备。
Reading NDEF data from an NFC tag is handled with the tag dispatch system, which analyzes discovered NFC tags, appropriately categorizes the data, and starts an application that is interested in the categorized data. An application that wants to handle the scanned NFC tag can declare an intent filter and request to handle the data.
从NFC标签读取ndef数据由标签调度系统处理,该系统分析发现的NFC标签,对数据进行适当分类,并启动对分类数据感兴趣的应用程序。想要处理扫描的NFC标签的应用程序可以声明意向过滤器并请求处理数据。
The Android Beam™ feature allows a device to push an NDEF message onto another device by physically tapping the devices together. This interaction provides an easier way to send data than other wireless technologies like Bluetooth, because with NFC, no manual device discovery or pairing is required. The connection is automatically started when two devices come into range. Android Beam is available through a set of NFC APIs, so any application can transmit information between devices. For example, the Contacts, Browser, and YouTube applications use Android Beam to share contacts, web pages, and videos with other devices.
Android Beam™功能允许设备通过物理方式将ndef消息推送到其他设备上。与其他无线技术(如蓝牙)相比,这种交互提供了一种更简单的数据发送方式,因为使用NFC,不需要手动设备发现或配对。当两个设备进入(连接)范围时,连接自动启动。Android Beam通过一组NFC API可用,因此任何应用程序都可以在设备之间传输信息。例如,联系人、浏览器和YouTube应用程序使用Android Beam与其他设备共享联系人、网页和视频。
The tag dispatch system
标签调度系统
Android-powered devices are usually looking for NFC tags when the screen is unlocked, unless NFC is disabled in the device's Settings menu. When an Android-powered device discovers an NFC tag, the desired behavior is to have the most appropriate activity handle the intent without asking the user what application to use. Because devices scan NFC tags at a very short range, it is likely that making users manually select an activity would force them to move the device away from the tag and break the connection. You should develop your activity to only handle the NFC tags that your activity cares about to prevent the Activity Chooser from appearing.
Android驱动的设备通常在屏幕解锁时寻找NFC标签,除非在设备的设置菜单中禁用NFC。当Android驱动的设备发现NFC标签时,所需的行为是让最合适的Activity处理意图,而不是询问用户要使用什么应用程序。因为设备在很短的范围内扫描NFC标签,所以让用户手动选择一个活动可能会迫使他们移动设备离开标签并且导致连接断开。你应该开发你的Activity来处理它所关心的NFC标签,以防止Activity选择器出现。
To help you with this goal, Android provides a special tag dispatch system that analyzes scanned NFC tags, parses them, and tries to locate applications that are interested in the scanned data. It does this by:
为了帮助您实现这一目标,Android提供了一个特殊的标签调度系统,该系统分析扫描的NFC标签,解析它们,并尝试定位对扫描数据感兴趣的应用程序。它是通过以下方式实现的:
1.Parsing the NFC tag and figuring out the MIME type or a URI that identifies the data payload in the tag.
1.分析NFC标签并找出MIME类型或标识标记中数据有效负载的URI。
2.Encapsulating the MIME type or URI and the payload into an intent. These first two steps are described in How NFC tags are mapped to MIME types and URIs.
将MIME 类型或URI和有效负载封装到意图中。前两个步骤在如何将NFC标记映射到MIME类型和URI中进行了描述。
3.Starts an activity based on the intent. This is described in How NFC Tags are Dispatched to Applications.
3.基于上述意图启动Activity。这在如何将NFC标记分派到应用程序中进行了描述。
How NFC tags are mapped to MIME types and URIs
如何将NFC标记映射到MIME类型和URI中
Before you begin writing your NFC applications, it is important to understand the different types of NFC tags, how the tag dispatch system parses NFC tags, and the special work that the tag dispatch system does when it detects an NDEF message. NFC tags come in a wide array of technologies and can also have data written to them in many different ways. Android has the most support for the NDEF standard, which is defined by the NFC Forum.
在开始编写您的NFC应用之前,去了解NFC标签的不同类型,标签调度系统如何解析标签,并且当标签调度系统检测到NDEF消息时所作的特殊工作是非常重要的,NFC标签有各种各样的技术,并且可以以多种不同的方式写入数据。Android对由NFC论坛定义的ndef标准的支持最多。
NDEF data is encapsulated inside a message ([NdefMessage](https://developer.android.google.cn/reference/android/nfc/NdefMessage.html)
) that contains one or more records ([NdefRecord](https://developer.android.google.cn/reference/android/nfc/NdefRecord.html)
). Each NDEF record must be well-formed according to the specification of the type of record that you want to create. Android also supports other types of tags that do not contain NDEF data, which you can work with by using the classes in the [android.nfc.tech](https://developer.android.google.cn/reference/android/nfc/tech/package-summary.html)
package. To learn more about these technologies, see the Advanced NFC topic. Working with these other types of tags involves writing your own protocol stack to communicate with the tags, so we recommend using NDEF when possible for ease of development and maximum support for Android-powered devices.
NDEF数据被封装在消息(NdefMessage)中,它(NdefMessage)包含一条或者多条记录(NdefRecord)。每个ndef记录都必须根据要创建的记录类型的规范进行良好的格式设置。Android还支持不包含ndef数据的其他类型的标签,您可以使用android.nfc.tech包中的类来使用这些标签。
要了解有关这些技术的更多信息,请参阅高级NFC主题。使用这些其他类型的标签需要编写自己的协议栈来与标签通信,因此我们建议尽可能使用ndef,以便于开发和最大限度地支持Android驱动的设备。
Now that you have some background in NFC tags, the following sections describe in more detail how Android handles NDEF formatted tags. When an Android-powered device scans an NFC tag containing NDEF formatted data, it parses the message and tries to figure out the data's MIME type or identifying URI. To do this, the system reads the first [NdefRecord](https://developer.android.google.cn/reference/android/nfc/NdefRecord.html)
inside the [NdefMessage](https://developer.android.google.cn/reference/android/nfc/NdefMessage.html)
to determine how to interpret the entire NDEF message (an NDEF message can have multiple NDEF records). In a well-formed NDEF message, the first [NdefRecord](https://developer.android.google.cn/reference/android/nfc/NdefRecord.html)
contains the following fields:
既然您对NFC标签有了一些背景,下面的部分将更详细地描述Android如何处理NDEF格式的标签。当Android驱动的设备扫描包含NDEF 格式数据的NFC标签时,它会解析消息并试图找出数据的MIME 类型或标识URI。为此,系统读取NdefRecord中的第一条NdefRecord ,以确定如何解释整个NDEF消息(NDEF消息可以有多个NDEF记录)。在格式良好的NDEF消息中,第一个NdefRecord 包含以下字段:
3-bit TNF (Type Name Format):3元TNF(类型 名称 格式)
Indicates how to interpret the variable length type field. Valid values are described in Table 1.
指示如何解释可变长度类型字段。有效值见表1。
Variable length type:可变长度类型
Describes the type of the record. If using [TNF_WELL_KNOWN](https://developer.android.google.cn/reference/android/nfc/NdefRecord.html#TNF_WELL_KNOWN)
, use this field to specify the Record Type Definition (RTD). Valid RTD values are described in Table 2.
描述记录的类型。如果使用TNF_WELL_KNOWN,使用该字段去指定记录类型定义。有效的RTD值参见表2。
Variable length ID:可变长度ID
A unique identifier for the record. This field is not used often, but if you need to uniquely identify a tag, you can create an ID for it.
记录的唯一标识符。此字段不常用,但如果需要唯一标识标记,可以为其创建ID
Variable length payload:可变长度有效载荷
The actual data payload that you want to read or write. An NDEF message can contain multiple NDEF records, so don't assume the full payload is in the first NDEF record of the NDEF message.
要读取或写入的实际数据负载。一个NDEF消息可以包含多个NDEF记录,因此不要假定完整的有效载荷在NDEF消息的第一个NDEF记录中。