google nfc 基础文档翻译 (上)

此文章是google 官方对NFC的简介。


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 supportthese features. For more advanced topics, including a discussion of working with non-NDEF data,see Advanced NFC.

 本文档介绍了基本的NFC任务在Android上。它说明了如何发送和接收的NDEF消息的形式数据,并介绍支持这些功能的框架API。对于更高级内容,包括非NDEF数据的讨论,请参阅高级NFC

There are two major uses cases when working with NDEF data and Android:

  • Reading NDEF data from an NFC tag
  • Beaming NDEF messages from one device to another with AndroidBeam™

在Android使用NDEF数据,有两个主要的用途:
       ● 阅读NDEF数据从一个NFC标签
       ● 传送NDEF信息从一个设备到另一个支持Android Beam的设备

Reading NDEF data from an NFC tag is handled with the tag dispatchsystem, which analyzes discovered NFC tags, appropriately categorizes the data, and startsan 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 on to another device by physically tapping the devices together. This interaction provides an easier wayto send data than other wireless technologies like Bluetooth, because with NFC, no manual devicediscovery 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.

        Andr​​oid 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 behavioris to have the most appropriate activity handle the intent without asking the user what applicationto 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.

To help you with this goal, Android provides a special tag dispatch system that analyzes scannedNFC tags, parses them, and tries to locate applications that are interested in the scanned data. Itdoes this by:

  1. Parsing the NFC tag and figuring out the MIME type or a URI that identifies the data payload in the tag.
  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.
  3. Starts an activity based on the intent. This is described in How NFC Tags are Dispatched to Applications.

Android设备通常是在屏幕解锁的时候扫描NFC标签,除非NFC功能在设置菜单中被禁用。当Android 设备发现了一个NFC标签,启动最适当的activity处理intent,而不需要询问用户使用什么应用程序处理。因为设备扫描NFC标签在很短的范围内,让用户手动选择的活动很有可能将迫使他们从标签上移动设备,进而断开连接。应该开发activity处理NFC标签,activity以防止activity选择出现。
        为实现这个目标,Android提供了一个特殊的标签调度系统来分析扫描NFC标签并解析它们,并试图在扫描数据中找到对应的的应用程序。这是通过:
        1. 解析NFC标签和搞清楚的MIME类型或在标签中用于标识一个数据URI。
        2. 封装MIME类型或URI和有效载荷到Intent。前两个步骤在下面的”NFC标签是如何映射到MIME类型和URI“中有介绍。
        3. 基于Intent启动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 awide 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标签,调度系统如何解析NFC标签,和当标签调度系统检测到一个NDEF消息时的专项的工作。NFC标签来在各种各样的技术组织,也可以有许多不同方式的数据写入。Android使用的是最受支持的NDEF标准,它是由NFC论坛定义的。


NDEF data is encapsulated inside a message (NdefMessage) that contains oneor more records (NdefRecord). Each NDEF record must be well-formed according tothe specification of the type of record that you want to create. Androidalso 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 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 whenpossible for ease of development and maximum support for Android-powered devices.

Note:To download complete NDEF specifications, go to the NFC Forum Specification Download site and seeCreating common types of NDEF records for examples of how toconstruct NDEF records.


        NDEF数据被封装内部消息(NdefMessage),它包含一个或多个的记录(NdefRecord)。每个NDEF记录必须形成规范的记录。Android还支持其他的标签不包含NDEF数据,可以通过使用在android.nfc.tech包中的类。要了解有关这些技术的更多信息,请参阅高级NFC篇。工作涉及到编写自己的协议栈与其他类型的标签进行通信,因此我们建议在可能的情况下使用NDEF为易于开发和对Android的设备的最大支持。
         ★注意: 要下载完整的的NDEF规范,请到NFC论坛,参考创建通用NDEF记录 ,学习如何构建NDEF记录。


Now that you have some background in NFC tags, the following sections describe in more detail howAndroid handles NDEF formatted tags. When an Android-powered device scans an NFC tag containing NDEFformatted data, it parses the message and tries to figure out the data's MIME type or identifyingURI. To do this, the system reads the first NdefRecord inside the NdefMessage to determine how to interpret the entire NDEF message (an NDEF message canhave multiple NDEF records). In a well-formed NDEF message, the first NdefRecordcontains the following fields:

3-bit TNF (Type Name Format)
Indicates how to interpret the variable length type field. Valid values are described indescribed in Table 1.
Variable length type
Describes the type of the record. If using TNF_WELL_KNOWN, usethis field to specify the Record Type Definition (RTD). Valid RTD values are described in Table 2.
Variable length ID
A unique identifier for the record. This field is not used often, butif you need to uniquely identify a tag, you can create an ID for it.
Variable length payload
The actual data payload that you want to read or write. An NDEFmessage can contain multiple NDEF records, so don't assume the full payload is in the first NDEFrecord of the NDEF message.

The tag dispatch system uses the TNF and type fields to try to map a MIME type or URI to theNDEF message. If successful, it encapsulates that information inside of a ACTION_NDEF_DISCOVERED intent along with the actual payload. However, thereare cases when the tag dispatch system cannot determine the type of data based on the first NDEFrecord. This happens when the NDEF data cannot be mapped to a MIME type or URI, or when the NFC tag does not contain NDEF data to begin with. In such cases, a Tag object that has information about the tag's technologies and the payload are encapsulated inside of a ACTION_TECH_DISCOVERED intent instead.

Table 1. describes how the tag dispatch system maps TNF and typefields to MIME types or URIs. It also describes which TNFs cannot be mapped to a MIME type or URI.In these cases, the tag dispatch system falls back toACTION_TECH_DISCOVERED.

For example, if the tag dispatch system encounters a record of type TNF_ABSOLUTE_URI, it maps the variable length type field of that recordinto a URI. The tag dispatch system encapsulates that URI in the data field of an ACTION_NDEF_DISCOVERED intent along with other information about the tag,such as the payload. On the other hand, if it encounters a record of type TNF_UNKNOWN, it creates an intent that encapsulates the tag's technologiesinstead.


        现在有了NFC标签的一些背景知识,下面的章节更详细的描述了Android是如何处理NDEF格式的标签。当Android手机扫 ​​描一个包含NDEF数据格式的NFC标签,它会解析消息,并试图找出数据的MIME类型或标识URI。要做到这一点,系统读取NdefMessage里的第一个NdefRecord,以确定如何解析整个的的NDEF消息(NDEF消息可以有多个NDEF记录)。在一个良好的NDEF消息,第一NdefRecord 包含以下字段:

3-bit TNF (Type Name Format)
        指示如何解析variable length type区域。有效值像在表1中描述的。
Variable length type
         描述记录类型。如果使用TNF_WELL_KNOWN,使用此字段指定记录类型定义(RTD)。有效的RTD值描述在表2中。
Variable length ID
         记录的唯一标识符。此字段不经常使用,但如果需要唯一识别的标签,你可以为其创建一个ID。
Variable length payload
         这个是要读取或写入的实际数据负载。NDEF消息可以包含多个NDEF记录,所以不要以为全部负载就是在第一NDEF创纪录的NDEF消息。
         标签调度系统使用TNF和类型字段试图去映射的MIME类型或URI的NDEF消息。如果成功的话,它封装了内部信息的实际有效载荷的ACTION_NDEF_DISCOVERED intent。然而,也会发生标签调度系统不能基于第一NDEF记录的数据确定类型。发生这种情况时,NDEF数据不能被映射到一个MIME类型或URI,或当NFC标签不包含NDEF数据做为开始。在这种情况下,标签的对象,该对象具有标记的技术和有效载荷的信息,封装一个成ACTION_TECH_DISCOVERED intent。



How NFC Tags are Dispatched to Applications

When the tag dispatch system is done creating an intent that encapsulates the NFC tag and itsidentifying information, it sends the intent to an interested application thatfilters for the intent. If more than one application can handle the intent, the Activity Chooseris presented so the user can select the Activity. The tag dispatch system defines three intents,which are listed in order of highest to lowest priority:

  1. ACTION_NDEF_DISCOVERED: This intent is used to start anActivity when a tag that contains an NDEF payload is scanned and is of a recognized type. This isthe highest priority intent, and the tag dispatch system tries to start an Activity with thisintent before any other intent, whenever possible.
  2. ACTION_TECH_DISCOVERED: If no activities register tohandle the ACTION_NDEF_DISCOVEREDintent, the tag dispatch system tries to start an application with this intent. Thisintent is also directly started (without starting ACTION_NDEF_DISCOVERED first) if the tag that is scannedcontains NDEF data that cannot be mapped to a MIME type or URI, or if the tag does not contain NDEFdata but is of a known tag technology.
  3. ACTION_TAG_DISCOVERED: This intent is started if no activities handle the ACTION_NDEF_DISCOVERED or ACTION_TECH_DISCOVERED intents.

The basic way the tag dispatch system works is as follows:

  1. Try to start an Activity with the intent that was created by the tag dispatch systemwhen parsing the NFC tag (eitherACTION_NDEF_DISCOVERED or ACTION_TECH_DISCOVERED).
  2. If no activities filter for that intent, try to start an Activity with the next lowest priority intent (either ACTION_TECH_DISCOVERED or ACTION_TAG_DISCOVERED) until an application filters for the intent or until the tag dispatch system tries all possible intents.
  3. If no applications filter for any of the intents, do nothing.

NFC标签分发到应用

        标签调度系统创建一个intent,intent封装了NFC标签和它的识别信息,调度系统发送给对应的应用程序,它是依据intent的过滤器。如果有一个以上的应用程序可以处理这个intent,activity选择器就会出现,使用户可以选择的activity。标签调度系统定义了三个意向,按照最高优先级到最低优先级的顺序:
       1. ACTION_NDEF_DISCOVERED:当包含NDEF有效载荷或是一个公认的类型的NFC tag被扫描到时,这个Intent用来启动一个activity。这是最高优先级的intent,标签调度系统尝试用这个intent启动一个activity,早于任何其他的intent。
        2. ACTION_TECH_DISCOVERED:如果没有activity处理ACTION_NDEF_DISCOVERED  intent,标签调度系统尝试使用这个intent启动应用程序。如果被扫描tag包含NDEF的数据不能被映射到一个MIME类型或URI,或者tag不包含NDEF数据,但是是一个已知的标签技术,在这两种情况下,此intent也可以直接启动(首先没有启动ACTION_NDEF_DISCOVERED)。
        3. ACTION_TAG_DISCOVERED:在没有activity处理ACTION_NDEF_DISCOVERED或ACTION_TECH_DISCOVERED的 intent时,此intent才会被启用。

标签调度系统的工作原理,基本方法如下:
        1.  用intent启动一个activity,此intent是由标签调度系统解析NFC的tag所创建的(ACTION_NDEF_DISCOVERED或ACTION_TECH_DISCOVERED)。
        2.  如果activity过滤器没有过滤那个intent,就要用下一个低优先级的的intent(ACTION_TECH_DISCOVERED或ACTION_TAG_DISCOVERED)启动activity,直到应用程序过滤器找到相应的intent,或者标签调度系统尝试所有可能的intent。
        3. 如果应用程序不针对任何的intent过滤,那就do nothing。


Figure 1. Tag Dispatch System

Whenever possible, work with NDEF messages and the ACTION_NDEF_DISCOVERED intent, because it is the most specific out ofthe three. This intent allows you to start your application at a more appropriate time than theother two intents, giving the user a better experience.

      只要有可能,都会用NDEF 消息和ACTION_NDEF_DISCOVERED 来工作,因为它是三个Intent中最突出使用的。ACTION_NDEF_DISCOVERED 可以比其他的两个intent能在更合适的时间启动应用,给了用户很好的体验。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值