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記錄中。