Android 2.3 NFC簡介

來源:互聯網
上載者:User

來自:http://blog.csdn.net/mutouyueliang/archive/2011/03/01/6215811.aspx

原創:木頭月亮

Android 2.3加入了NFC(近場通訊)的支援。官網developer.android.com的英文介紹如下:

Near Field Communications (NFC)

Android 2.3 includes an NFC stack and framework API that lets developers read NDEF tags that are discovered as a user touches an NFC-enabled device to tag elements embedded in stickers, smart posters, and even other devices.

The platform provides the underlying NFC services that work with the device hardware to discover tags when they come into range. On discovering a tag, the platform notifies applications by broadcasting an Intent, appending the tag's NDEF messages to the Intent as extras. Applications can create Intent filters to recognize and handle targeted tags and messages. For example, after receiving a tag by Intent, applications extract the NDEF messages, store them, alert the user, or handle them in other ways.


The NFC API is available in the android.nfc package. The key classes are:

  • NfcAdapter, which represents the NFC hardware on the device.
  • NdefMessage, which represents an NDEF data message, the standard format in which "records" carrying data are transmitted between devices and tags. Applications can receive these messages from ACTION_TAG_DISCOVERED Intents.
  • NdefRecord, delivered in an NdefMessage, which describes the type of data being shared and carries the data itself.

NFC communication relies on wireless technology in the device hardware, so support for the platform's NFC features on specific devices is determined by their manufacturers. To determine the NFC support on the current device, applications can call isEnabled() to query the NfcAdapter. The NFC API is always present, however, regardless of underlying hardware support.


To use the NFC API, applications must request permission from the user by declaring <uses-permission android:name="android.permission.NFC"> in their manifest files.

Additionally, developers can request filtering on Android Market, such that their applications are not discoverable to users whose devices do not support NFC. To request filtering, add <uses-feature android:name="android.hardware.nfc" android:required="true"> to the application's manifest.


NFC的應用情境有很多,但Android 2.3目前API只提供了電子標籤閱讀器的功能。相信隨著Android的版本升級,後續會增加很多應用情境和API。

功能是少一點,但從無到有的第一步總是不容易的。從架構上看,至少有以下幾部分的改動:

  • Android Framework層,為應用開發增加了相關API(即英文中的framework API)。
  • 協議層,增加NFC的協議棧(即英文中的NFC Stack)。
  • 硬體適配層,為硬體生產商提供適配介面,此層應該與NFC硬體的Linux驅動通訊(實現方式待驗證)。

工作流程是當支援NFC功能的手機或其他電子終端(後面簡稱手機)在使用者開啟NFC功能的時候,如果手機內建的NFC掃描器(相當於類NfcAdapter的功能)掃描到電子標籤後,就會向相關使用者程式發送ACTION_TAG_DISCOVERED的Intent,Itent的extras架構中會包含NDEF(NFC Data Exchange Format)訊息。如何處理此NDEF訊息,就是使用者程式的事情了。

NFC的API在android.nfc的包中提供,這個包主要提供三個大類,其中:NfcAdapter描述的就是手機中的NFC硬體,Android 2.3中可以暫時理解為電子標籤掃描器。電子標籤和掃描器中的訊息通過NdefMessage來表示,這個類很簡單,只是封裝了NdefRecord。每個NdefMessage中可以包含多個NdefRecord,通過類NdefMessage的方法getRecords()可以查詢到訊息的所有NdefRecord。NdefRecord才是資訊的真正載體,正確理解這個類是理解NFC技術的一個重點。NFC的技術規範http://www.nfc-forum.org/specs/spec_list/是理解這個類不可或缺的資料。

應用程式的編程思路是:

  1. 通過android.nfc.NfcAdapter.getDefaultAdapter()取得手機的objNfcAdapter
  2. 通過objNfcAdapter.isEnabled()查詢該手機是否支援NFC
  3. 如果手機支援NFC,就申請接收ACTION_TAG_DISCOVERED的Intent
  4. 如果接收到ACTION_TAG_DISCOVERED,就提取NdefMessage,並在此基礎上進而提取NdefRecord,整個是一個訊息解析過程

     

    來自:http://blog.csdn.net/mutouyueliang/archive/2011/03/01/6215811.aspx

    原創:木頭月亮

     

    相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.