2013年7月31日 星期三

android 開發使用 NFC TAG

在Android系統下,使用NFC必須先在Android系統開啟NFC認證(註冊)才可以在Android系統下使用,

而 NFC message 以 NDEF NFC Data Exchange Format (NDEF)格式為主,

但是 遇到非 NDEF NFC Data Exchange Format (NDEF)格式 也是有其他的方法可以使用。

而使用 NFC 有一些注意到的事項,

Android 2.3.3 (API Level 10)才有支援完整的NFC功能,

Android 4.x 支持 Android Application Record (AAR),

Android 4.0 支持 Beam,

在Android developers官網裡分別有二篇文章說明NFC的使用:<NFC Basics>與<Advanced NFC>。

根據 NFC Basics 學習在Android上,

如何傳送與接收NDEF data message,

來自NFC tag 、 Beaming NDEF Message由一個device到另一個,

操作NFC APIs、Android Beam™,

NDFE data 從 NFC tag 中取出,

交由tag dispatch system進行處理,包括:分析發現的NFC tags、將資料進行分類,並且根據資料啟動應用程式。

一個應用程式想要處理被掃瞄到的NFC tag,可以宣告intent filter與請求要處理的data。

NDEF record的定義:

由四個資訊組合:
1). 3-bit TNF (Type Name Format):
      說明如何解釋Type欄位 (Variable length type)的格式。透過下表說明the tag dispatch system如何透過TNF與type field
      來識別該NDEF message是MIME type或URI。可以被配對到的NDEF message,系統會觸發ACTION_NDEF_DISCOVERED,
      如果無法配對,系統會再倒給ACTION_TECH_DISCOVERED進行配對,如果沒有再退給ACTION_TAG_DISCOVERED。

2). Variable length type:
      描述該record的類型。如果是TNF_WELL_KNOWN,使用該欄位需要參考Record Type Definition (RTD)。

3). Variable length ID:
      該record的unique identifier。該欄位經常不使用,但如果需要唯一識別該tag,可以建立一個ID給他。

4). Variable length payload:
      實際想要讀或寫的資料內容(data payload)。一個NDEF message可包括多個NDEF records,所以不要以為所有的資料
     均寫在NDEF message的第一個record。

以下的註冊就是教你如何在Android系統下使用NFC,

註冊filter ACTION_NDEF_DISCOVERED:
            為了filter ACTION_NDEF_DISCOVERED intents,宣告intent filter要處理的資料類型。
以下例子使用處理MIME Type為text/plain:
<intent-filter>
    <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
    <category android:name="android.intent.category.DEFAULT"/>
    <data android:mimeType="text/plain" />
</intent-filter>
以下例子使用處理URI類型:http://developer.android.com/index.html;
註冊處理為http的scheme,developer.android.com的host,以及固定第一個字段為:index.html。
<intent-filter>
    <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
    <category android:name="android.intent.category.DEFAULT"/>
   <data android:scheme="http"
              android:host="developer.android.com"
              android:pathPrefix="/index.html" />
</intent-filter>


註冊filter ACTION_TECH_DISCOVERED:
          如果要註冊filter ACTION_TECH_DISCOVERED,需要在專案中增加一個新的XML檔案,定義該應用程式支持的 tech-list sets。
可以透過呼叫getTechList()來確認偵測到的NFC tag是否與定義的tech-list sets有所匹配。該檔案建議放罝於<project-root>/res/xml目錄下。
舉例來說:
如果一個tag被偵測到,它支持三種標準:MifareClassic、NdefFormatable與NfcA,在定義的tech-list sets就需要加入這些標準,
可能是一個、二個或三個標準都支持,以確保可以處理該intent。以下列出定義常見的tech-list集合:
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <tech-list>
        <tech>android.nfc.tech.IsoDep</tech>
        <tech>android.nfc.tech.NfcA</tech>
        <tech>android.nfc.tech.NfcB</tech>
        <tech>android.nfc.tech.NfcF</tech>
        <tech>android.nfc.tech.NfcV</tech>
        <tech>android.nfc.tech.Ndef</tech>
        <tech>android.nfc.tech.NdefFormatable</tech>
        <tech>android.nfc.tech.MifareClassic</tech>
        <tech>android.nfc.tech.MifareUltralight</tech>
    </tech-list>
</resources>

另外,也可以定義多組tech-list sets。每一個tech-list set是獨立的,如果任何一組tech-list set被匹配到,activity將會被啟動,
該activity也可以透過getTechList()來確認偵測到的NFC tag是否與定義的tech-list sets有所匹配。它提供了 AND 與OR的定義匹配技術。
透過下列範例,說明支持NdefA與NdefB:
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <tech-list>
        <tech>android.nfc.tech.NfcA</tech>
        <tech>android.nfc.tech.Ndef</tech>
    </tech-list>
</resources>

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <tech-list>
        <tech>android.nfc.tech.NfcB</tech>
        <tech>android.nfc.tech.Ndef</tech>
    </tech-list>
</resources>

定義好了resource,可以在AppManifest.xml中指定activity透過<meta-data />定義要使用那一個resource。如下:
<activity>
...
<intent-filter>
    <action android:name="android.nfc.action.TECH_DISCOVERED"/>
</intent-filter>

<meta-data android:name="android.nfc.action.TECH_DISCOVERED"
    android:resource="@xml/nfc_tech_filter" />
...
</activity>
更多詳細的資料可以參考<Working with Supported Tag Technologies>。


註冊ACTION_TAG_DISCOVERED:
<intent-filter>
    <action android:name="android.nfc.action.TAG_DISCOVERED"/>
</intent-filter>

沒有留言:

張貼留言