2019-04-23 NFC基礎(chǔ)

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中執(zhí)行基本的NFC任務(wù)。它解釋了如何以NDEF消息的形式發(fā)送和接收NFC數(shù)據(jù),并描述了支持這些功能的Android框架API。有關(guān)更高級(jí)的主題,包括使用非NDEF數(shù)據(jù)的討論,請(qǐng)參閱高級(jí)NFC。
There are two major use cases when working with NDEF data and Android:
在Android上使用NDEF數(shù)據(jù)時(shí)有兩個(gè)主要的用例:

  1. Reading NDEF data from an NFC tag
    1.從NFC標(biāo)簽讀取NDEF數(shù)據(jù)
    2.Beaming NDEF messages from one device to another with Android Beam?
    2.使用Android Beam從一個(gè)設(shè)備發(fā)送NDEF消息到另一個(gè)設(shè)備。
    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標(biāo)簽讀取ndef數(shù)據(jù)由標(biāo)簽調(diào)度系統(tǒng)處理,該系統(tǒng)分析發(fā)現(xiàn)的NFC標(biāo)簽,對(duì)數(shù)據(jù)進(jìn)行適當(dāng)分類,并啟動(dòng)對(duì)分類數(shù)據(jù)感興趣的應(yīng)用程序。想要處理掃描的NFC標(biāo)簽的應(yīng)用程序可以聲明意向過濾器并請(qǐng)求處理數(shù)據(jù)。
    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?功能允許設(shè)備通過物理方式將ndef消息推送到其他設(shè)備上。與其他無線技術(shù)(如藍(lán)牙)相比,這種交互提供了一種更簡單的數(shù)據(jù)發(fā)送方式,因?yàn)槭褂肗FC,不需要手動(dòng)設(shè)備發(fā)現(xiàn)或配對(duì)。當(dāng)兩個(gè)設(shè)備進(jìn)入(連接)范圍時(shí),連接自動(dòng)啟動(dòng)。Android Beam通過一組NFC API可用,因此任何應(yīng)用程序都可以在設(shè)備之間傳輸信息。例如,聯(lián)系人、瀏覽器和YouTube應(yīng)用程序使用Android Beam與其他設(shè)備共享聯(lián)系人、網(wǎng)頁和視頻。

The tag dispatch system
標(biāo)簽調(diào)度系統(tǒng)
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驅(qū)動(dòng)的設(shè)備通常在屏幕解鎖時(shí)尋找NFC標(biāo)簽,除非在設(shè)備的設(shè)置菜單中禁用NFC。當(dāng)Android驅(qū)動(dòng)的設(shè)備發(fā)現(xiàn)NFC標(biāo)簽時(shí),所需的行為是讓最合適的Activity處理意圖,而不是詢問用戶要使用什么應(yīng)用程序。因?yàn)樵O(shè)備在很短的范圍內(nèi)掃描NFC標(biāo)簽,所以讓用戶手動(dòng)選擇一個(gè)活動(dòng)可能會(huì)迫使他們移動(dòng)設(shè)備離開標(biāo)簽并且導(dǎo)致連接斷開。你應(yīng)該開發(fā)你的Activity來處理它所關(guān)心的NFC標(biāo)簽,以防止Activity選擇器出現(xiàn)。
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:
為了幫助您實(shí)現(xiàn)這一目標(biāo),Android提供了一個(gè)特殊的標(biāo)簽調(diào)度系統(tǒng),該系統(tǒng)分析掃描的NFC標(biāo)簽,解析它們,并嘗試定位對(duì)掃描數(shù)據(jù)感興趣的應(yīng)用程序。它是通過以下方式實(shí)現(xiàn)的:
1.Parsing the NFC tag and figuring out the MIME type or a URI that identifies the data payload in the tag.
1.分析NFC標(biāo)簽并找出MIME類型或標(biāo)識(shí)標(biāo)記中數(shù)據(jù)有效負(fù)載的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和有效負(fù)載封裝到意圖中。前兩個(gè)步驟在如何將NFC標(biāo)記映射到MIME類型和URI中進(jìn)行了描述。
3.Starts an activity based on the intent. This is described in How NFC Tags are Dispatched to Applications.
3.基于上述意圖啟動(dòng)Activity。這在如何將NFC標(biāo)記分派到應(yīng)用程序中進(jìn)行了描述。

How NFC tags are mapped to MIME types and URIs
如何將NFC標(biāo)記映射到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應(yīng)用之前,去了解NFC標(biāo)簽的不同類型,標(biāo)簽調(diào)度系統(tǒng)如何解析標(biāo)簽,并且當(dāng)標(biāo)簽調(diào)度系統(tǒng)檢測到NDEF消息時(shí)所作的特殊工作是非常重要的,NFC標(biāo)簽有各種各樣的技術(shù),并且可以以多種不同的方式寫入數(shù)據(jù)。Android對(duì)由NFC論壇定義的ndef標(biāo)準(zhǔn)的支持最多。
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數(shù)據(jù)被封裝在消息(NdefMessage)中,它(NdefMessage)包含一條或者多條記錄(NdefRecord)。每個(gè)ndef記錄都必須根據(jù)要?jiǎng)?chuàng)建的記錄類型的規(guī)范進(jìn)行良好的格式設(shè)置。Android還支持不包含ndef數(shù)據(jù)的其他類型的標(biāo)簽,您可以使用android.nfc.tech包中的類來使用這些標(biāo)簽。
要了解有關(guān)這些技術(shù)的更多信息,請(qǐng)參閱高級(jí)NFC主題。使用這些其他類型的標(biāo)簽需要編寫自己的協(xié)議棧來與標(biāo)簽通信,因此我們建議盡可能使用ndef,以便于開發(fā)和最大限度地支持Android驅(qū)動(dòng)的設(shè)備。
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:
既然您對(duì)NFC標(biāo)簽有了一些背景,下面的部分將更詳細(xì)地描述Android如何處理NDEF格式的標(biāo)簽。當(dāng)Android驅(qū)動(dòng)的設(shè)備掃描包含NDEF 格式數(shù)據(jù)的NFC標(biāo)簽時(shí),它會(huì)解析消息并試圖找出數(shù)據(jù)的MIME 類型或標(biāo)識(shí)URI。為此,系統(tǒng)讀取NdefRecord中的第一條NdefRecord ,以確定如何解釋整個(gè)NDEF消息(NDEF消息可以有多個(gè)NDEF記錄)。在格式良好的NDEF消息中,第一個(gè)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.
記錄的唯一標(biāo)識(shí)符。此字段不常用,但如果需要唯一標(biāo)識(shí)標(biāo)記,可以為其創(chuàng)建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.
要讀取或?qū)懭氲膶?shí)際數(shù)據(jù)負(fù)載。一個(gè)NDEF消息可以包含多個(gè)NDEF記錄,因此不要假定完整的有效載荷在NDEF消息的第一個(gè)NDEF記錄中。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,854評(píng)論 0 10
  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom閱讀 3,203評(píng)論 0 3
  • Correctness AdapterViewChildren Summary: AdapterViews can...
    MarcusMa閱讀 9,053評(píng)論 0 6
  • 這幾周一直像小陀螺一樣,不停地忙碌著,似乎終于過上了我自己說的那種充實(shí)的生活??墒亲约翰]有想象中的快樂,反而總是...
    滄海蜉蚴閱讀 323評(píng)論 0 1
  • 月到十五圓又圓,正月十五陰雨天。 家家戶戶吃湯圓,歡歡喜喜互拜年。 清晨即起率全家,吉祥如意慶團(tuán)圓。 閑來無事網(wǎng)聊...
    彼得瓦爾閱讀 462評(píng)論 1 6

友情鏈接更多精彩內(nèi)容