SlideShare a Scribd company logo
Android NFC 讀寫模式開發
Android Application Development of NFC Reader-Writer Mode
Chun-Kai Wang (王雋凱)
IDSL - Dept. of IM - NTUST
NFC Reader/Writer Mode
▪ Description from Android Developers:
▪ “Reader/writer mode, allowing the NFC device to read
and/or write passive NFC tags and stickers.”
2
NFC Tags
▪ NFC Tag 用於 NFC 通訊中小資料的互動,可以儲存如 URL、手機號碼
或其他文字資訊。NFC Forum 定義了四種不同的 Tag 類型:
▪ 除了 NFC Forum 定義的 Tag 類型外,其他廠商也提供了其他自訂的私
有 Tag 類型。其中,用的最廣的是 NXP 的 MIFARE Classic Tag。
3
Types of NFC Tag
NFC Forum Platform
NXP Specific
Platform
Type 1 Tag Type 2 Tag Type 3 Tag Type 4 Tag
Type MIFARE
Classic Tag
Compatible
Products
Broadcom
Topaz
NXP Mifare
Ultralight, NXP
Mifare
Ultralight C,
NXP NTAG203
Sony FeliCa NXP DESFire /
NXP SmartMX-
JCOP
NXP MIFARE
Classic 1k / NXP
MIFARE Classic
4k / NXP
MIFARE Classic
Mini
Memory Size 96 Bytes 48 Bytes / 144
Bytes
1, 4, 9 KB 4 KB /32 KB 768 Bytes /
3584 Bytes /
192 Bytes
Unit Price Low Low High Medium I High Low
Data Access Read/Write or
Read-Only
Read/Write or
Read-Only
Read/Write or
Read-Only
Read/Write or
Read-Only
Read/Write or
Read-only
4
NDEF
▪ NFC Data Exchange Format
▪ NDEF 是 NFC Forum 所定義的
NFC 資料交換格式通用標準。
▪ NFC tag 中的 NDEF 資料是封
裝在一個 NDEF message 裡面。
▪ NDEF message 內部可以有一
個或多個 record。
▪ NDEF 的功能:
▪ 從 NFC Tag 讀取 NDEF 資料。
▪ 從一個 NFC 裝置傳送 NDEF 資
料到另一個 NFC 裝置。
5
How Android handles NFC Tag
▪ NDEF Record 的第一位元組包含:
1. 3-bit TNF (Type Name Format):決定 Type 欄位的格式。
2. Variable length type:TNF 欄位的值決定此欄位的資料格式。
3. Variable length ID:Record 的 ID 編號,通常不需要設定。
4. Variable length payload:用來儲存實際的資料,NDEF message
的資料可以分開儲存在多個 record 中。
6
How Android handles NFC Tag (Cont.)
▪ 當 Android 裝置偵測到 NFC tag 時,開始分析從 NFC tag 中取得的資料。
▪ 利用 Tag dispatch system 透過 TNF 與 Type 兩個欄位嘗試去配對 NDEF
message 符合 MIME type 或URI:
▪ 如果配對成功,系統會封裝這些
資訊至 ACTION_NDEF_DISCOVERED
型態的 intent 物件。
▪ 如果配對不成功或是 NFC tag 不包含
NDEF data 造成無法配對,系統會再
倒給 ACTION_TECH_DISCOVERED
進行配對。
▪ 如果沒有配對成功再退給
ACTION_TAG_DISCOVERED。
7
Supported TNFs
Type Name Format (TNF) Mapping
TNF_ABSOLUTE_URI Type 欄位資料是 URI 資料
TNF_EMPTY Android 系統會以 ACTION_TECH_DISCOVERED 的方式處理
TNF_EXTERNAL_TYPE
Type 欄位資料格式為:URN 類型的 URI。 URN被編碼放入
NDEF type欄位,符合一個字段格式:
<domain name>:<service name>
Android 系統轉譯成:
vnd.android.nfc://ext/<domain name>:<service name>。
TNF_MIME_MEDIA Type 欄位是描述 MIME 的型態
TNF_UNCHANGED
第一個 Record 為無效的
Android 系統會以 ACTION_TECH_DISCOVERED 的方式處理
TNF_UNKNOWN Android 系統會以 ACTION_TECH_DISCOVERED 的方式處理
TNF_WELL_KNOWN 設定在 type field 中的 MIME type 或 URI,是取決於 Record
Type Definition (RTD)
8
Supported RTDs (for TNF_WELL_KNOWN)
Record Type Definition (RTD) Mapping
RTD_ALTERNATIVE_CARRIER Android 系統會以 ACTION_TECH_DISCOVERED 的方式處理
RTD_HANDOVER_CARRIER Android 系統會以 ACTION_TECH_DISCOVERED 的方式處理
RTD_HANDOVER_REQUEST Android 系統會以 ACTION_TECH_DISCOVERED 的方式處理
RTD_HANDOVER_SELECT Android 系統會以 ACTION_TECH_DISCOVERED 的方式處理
RTD_SMART_POSTER Payload 欄位是 URI
RTD_TEXT MIME 型態是 text/plain.
RTD_URI Payload 欄位是 URI
9
Reading NDEF-formatted Tag
▪ 讀取 NFC Tag 的操作主要包含以下步驟:
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
// 1. 定義 Tag 物件
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
// 2. 取得 NDEF Message
// your code in here...
// 3. 解析 NDEF Message
// your code in here...
// 4. 真實資料展示和進一步操作
// your code in here...
}
10
Getting NDEF Message from Tag
▪ Android 裝置掃描到的 NFC Tag 資料會以兩種方式儲存在 Intent :
▪ EXTRA_TAG:表示是一個 Tag 型態的物件。
▪ EXTRA_NDEF_MESSAGES:表示是 NDEF 型態的資料。
▪ 以下程式碼先檢查接收到的 Intent 是否為 ACTION_NDEF_DISCOVERED,
然後取出 EXTRA_NDEF_MESSAGES 形式的資料。
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent().getAction())) {
Parcelable[] rawMsgs =
intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
if (rawMsgs != null) {
msgs = new NdefMessage[rawMsgs.length];
for (int i = 0; i < rawMsgs.length; i++) {
msgs[i] = (NdefMessage) rawMsgs[i];
}
}
}
//process the msgs array
11
Parsing NDEF Message
▪ NDEF Message 是由一系列的 Records 所組成。
▪ Records 可以是 MIME-type media、URIs 或 RTDs (Record Type
Definitions) 類型。
12
Sample Project - NFCDemo
▪ 關於 NFC Tag 讀取資料解析的詳
細操作方法,可以參考 Android
官方的 Sample Project。
▪ Eclipse
→ File
→ New
→ Other
→ Android
→ Android Sample Project
→ Android 4.x
→ NFCDemo
13
Writing NDEF-formatted Tag
▪ 寫入 NFC Tag 的操作主要包含以下步驟:
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
// 1. 定義 Tag 物件
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
// 2. 建立 NDEF Record
// your code in here...
// 3. 建立 NDEF Message (包含一或多個 NDEF Record)
// your code in here...
// 4. 將 NDEF Message 寫入 Tag
// your code in here...
}
14
Creating Common Types of NDEF Records
▪ 建立 TNF_ABSOLUTE_URI 型態的 Record:
▪ 建立 TNF_MIME_MEDIA 型態的 Record:
▪ 使用 createMime() 靜態建構函式:
▪ 使用 NdefRecord 建構函式:
15
Creating Common Types of NDEF Records
▪ 建立 TNF_WELL_KNOWN 且 RTD_TEXT 型態的 Record:
16
Creating Common Types of NDEF Records
▪ 建立 TNF_WELL_KNOWN 且 RTD_URI 型態的 Record:
▪ 使用 createUri(String) 靜態建構函式:
▪ 使用 createUri(Uri) 靜態建構函式:
▪ 使用 NdefRecord 建構函式:
17
Creating Common Types of NDEF Records
▪ 建立 TNF_EXTERNAL_TYPE 型態的 Record:
▪ 使用 createExternal() 靜態建構函式:
▪ 使用 NdefRecord 建構函式:
18
Creating NDEF Message
▪ 準備好 NDEF Records 之後,就可以建立 NDEF Message 將 Records 包
裝起來:
▪ 如果要在 NDEF Message 中放入多個 Records,可參考以下程式碼:
NdefMessage msg = new NdefMessage(
new NdefRecord[] { ndefRecord });
NdefRecord[] ndefRecord1 = new NdefRecord(...);
NdefRecord[] ndefRecord2 = new NdefRecord(...);
NdefRecord[] ndefRecord3 = new NdefRecord(...);
NdefMessage msg = new NdefMessage(
new NdefRecord[] { ndefRecord1, ndefRecord2, ndefRecord3 });
19
Writing to an NDEF Tag
▪ 以下是將 NDEF Message 寫入 NDEF Tag 的簡單範例:
Ndef ndef = Ndef.get(tag);
if (ndef.isWritable()
&& ndef.getMaxSize() > ndefMessage.toByteArray().length) {
ndef.connect();
ndef.writeNdefMessage(ndefMessage);
ndef.close();
}
20
Useful Third-Party Libraries
▪ NDEF Tools for Android
▪ NDEF object representation library (no more byte arrays!)
▪ Simple conversion to and from Android SDK low-level equivalent
▪ NFC utility library. Abstract activities for:
▪ Detecting and reading messages
▪ Writing to tags
▪ Beaming (pushing) to other devices
▪ https://code.google.com/p/ndef-tools-for-android/
21
Thank You!

More Related Content

PPTX
Android Application Development of NFC Reader-Writer Mode
PDF
NFC近場通訊-twMVC#9
PPTX
Nfc tutorial
PPTX
Rfid簡報
PPT
Nfc近距離無線通訊(公開版)
PPTX
第7組低頭族報告
PPT
NFC (near field communication) introduction
PPTX
Scan life 二維條碼
Android Application Development of NFC Reader-Writer Mode
NFC近場通訊-twMVC#9
Nfc tutorial
Rfid簡報
Nfc近距離無線通訊(公開版)
第7組低頭族報告
NFC (near field communication) introduction
Scan life 二維條碼

Viewers also liked (7)

PPTX
Introduction to MongoDB
PPTX
NFC-based User Authentication Mechanisms for Personalized IPTV Services
PDF
Android NFC Application Development Environment Setup
PPTX
Android Application Development of NFC Peer-to-Peer Mode
PPTX
友增吾簡-互動式名片系統
PDF
Mobility Aware Distributed Service Composition Framework in SOA based MANET A...
PDF
Introduction to CodeIgniter
Introduction to MongoDB
NFC-based User Authentication Mechanisms for Personalized IPTV Services
Android NFC Application Development Environment Setup
Android Application Development of NFC Peer-to-Peer Mode
友增吾簡-互動式名片系統
Mobility Aware Distributed Service Composition Framework in SOA based MANET A...
Introduction to CodeIgniter
Ad

Nfc reader writer_mode

  • 1. Android NFC 讀寫模式開發 Android Application Development of NFC Reader-Writer Mode Chun-Kai Wang (王雋凱) IDSL - Dept. of IM - NTUST
  • 2. NFC Reader/Writer Mode ▪ Description from Android Developers: ▪ “Reader/writer mode, allowing the NFC device to read and/or write passive NFC tags and stickers.” 2
  • 3. NFC Tags ▪ NFC Tag 用於 NFC 通訊中小資料的互動,可以儲存如 URL、手機號碼 或其他文字資訊。NFC Forum 定義了四種不同的 Tag 類型: ▪ 除了 NFC Forum 定義的 Tag 類型外,其他廠商也提供了其他自訂的私 有 Tag 類型。其中,用的最廣的是 NXP 的 MIFARE Classic Tag。 3
  • 4. Types of NFC Tag NFC Forum Platform NXP Specific Platform Type 1 Tag Type 2 Tag Type 3 Tag Type 4 Tag Type MIFARE Classic Tag Compatible Products Broadcom Topaz NXP Mifare Ultralight, NXP Mifare Ultralight C, NXP NTAG203 Sony FeliCa NXP DESFire / NXP SmartMX- JCOP NXP MIFARE Classic 1k / NXP MIFARE Classic 4k / NXP MIFARE Classic Mini Memory Size 96 Bytes 48 Bytes / 144 Bytes 1, 4, 9 KB 4 KB /32 KB 768 Bytes / 3584 Bytes / 192 Bytes Unit Price Low Low High Medium I High Low Data Access Read/Write or Read-Only Read/Write or Read-Only Read/Write or Read-Only Read/Write or Read-Only Read/Write or Read-only 4
  • 5. NDEF ▪ NFC Data Exchange Format ▪ NDEF 是 NFC Forum 所定義的 NFC 資料交換格式通用標準。 ▪ NFC tag 中的 NDEF 資料是封 裝在一個 NDEF message 裡面。 ▪ NDEF message 內部可以有一 個或多個 record。 ▪ NDEF 的功能: ▪ 從 NFC Tag 讀取 NDEF 資料。 ▪ 從一個 NFC 裝置傳送 NDEF 資 料到另一個 NFC 裝置。 5
  • 6. How Android handles NFC Tag ▪ NDEF Record 的第一位元組包含: 1. 3-bit TNF (Type Name Format):決定 Type 欄位的格式。 2. Variable length type:TNF 欄位的值決定此欄位的資料格式。 3. Variable length ID:Record 的 ID 編號,通常不需要設定。 4. Variable length payload:用來儲存實際的資料,NDEF message 的資料可以分開儲存在多個 record 中。 6
  • 7. How Android handles NFC Tag (Cont.) ▪ 當 Android 裝置偵測到 NFC tag 時,開始分析從 NFC tag 中取得的資料。 ▪ 利用 Tag dispatch system 透過 TNF 與 Type 兩個欄位嘗試去配對 NDEF message 符合 MIME type 或URI: ▪ 如果配對成功,系統會封裝這些 資訊至 ACTION_NDEF_DISCOVERED 型態的 intent 物件。 ▪ 如果配對不成功或是 NFC tag 不包含 NDEF data 造成無法配對,系統會再 倒給 ACTION_TECH_DISCOVERED 進行配對。 ▪ 如果沒有配對成功再退給 ACTION_TAG_DISCOVERED。 7
  • 8. Supported TNFs Type Name Format (TNF) Mapping TNF_ABSOLUTE_URI Type 欄位資料是 URI 資料 TNF_EMPTY Android 系統會以 ACTION_TECH_DISCOVERED 的方式處理 TNF_EXTERNAL_TYPE Type 欄位資料格式為:URN 類型的 URI。 URN被編碼放入 NDEF type欄位,符合一個字段格式: <domain name>:<service name> Android 系統轉譯成: vnd.android.nfc://ext/<domain name>:<service name>。 TNF_MIME_MEDIA Type 欄位是描述 MIME 的型態 TNF_UNCHANGED 第一個 Record 為無效的 Android 系統會以 ACTION_TECH_DISCOVERED 的方式處理 TNF_UNKNOWN Android 系統會以 ACTION_TECH_DISCOVERED 的方式處理 TNF_WELL_KNOWN 設定在 type field 中的 MIME type 或 URI,是取決於 Record Type Definition (RTD) 8
  • 9. Supported RTDs (for TNF_WELL_KNOWN) Record Type Definition (RTD) Mapping RTD_ALTERNATIVE_CARRIER Android 系統會以 ACTION_TECH_DISCOVERED 的方式處理 RTD_HANDOVER_CARRIER Android 系統會以 ACTION_TECH_DISCOVERED 的方式處理 RTD_HANDOVER_REQUEST Android 系統會以 ACTION_TECH_DISCOVERED 的方式處理 RTD_HANDOVER_SELECT Android 系統會以 ACTION_TECH_DISCOVERED 的方式處理 RTD_SMART_POSTER Payload 欄位是 URI RTD_TEXT MIME 型態是 text/plain. RTD_URI Payload 欄位是 URI 9
  • 10. Reading NDEF-formatted Tag ▪ 讀取 NFC Tag 的操作主要包含以下步驟: if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) { // 1. 定義 Tag 物件 Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); // 2. 取得 NDEF Message // your code in here... // 3. 解析 NDEF Message // your code in here... // 4. 真實資料展示和進一步操作 // your code in here... } 10
  • 11. Getting NDEF Message from Tag ▪ Android 裝置掃描到的 NFC Tag 資料會以兩種方式儲存在 Intent : ▪ EXTRA_TAG:表示是一個 Tag 型態的物件。 ▪ EXTRA_NDEF_MESSAGES:表示是 NDEF 型態的資料。 ▪ 以下程式碼先檢查接收到的 Intent 是否為 ACTION_NDEF_DISCOVERED, 然後取出 EXTRA_NDEF_MESSAGES 形式的資料。 if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent().getAction())) { Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES); if (rawMsgs != null) { msgs = new NdefMessage[rawMsgs.length]; for (int i = 0; i < rawMsgs.length; i++) { msgs[i] = (NdefMessage) rawMsgs[i]; } } } //process the msgs array 11
  • 12. Parsing NDEF Message ▪ NDEF Message 是由一系列的 Records 所組成。 ▪ Records 可以是 MIME-type media、URIs 或 RTDs (Record Type Definitions) 類型。 12
  • 13. Sample Project - NFCDemo ▪ 關於 NFC Tag 讀取資料解析的詳 細操作方法,可以參考 Android 官方的 Sample Project。 ▪ Eclipse → File → New → Other → Android → Android Sample Project → Android 4.x → NFCDemo 13
  • 14. Writing NDEF-formatted Tag ▪ 寫入 NFC Tag 的操作主要包含以下步驟: if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) { // 1. 定義 Tag 物件 Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); // 2. 建立 NDEF Record // your code in here... // 3. 建立 NDEF Message (包含一或多個 NDEF Record) // your code in here... // 4. 將 NDEF Message 寫入 Tag // your code in here... } 14
  • 15. Creating Common Types of NDEF Records ▪ 建立 TNF_ABSOLUTE_URI 型態的 Record: ▪ 建立 TNF_MIME_MEDIA 型態的 Record: ▪ 使用 createMime() 靜態建構函式: ▪ 使用 NdefRecord 建構函式: 15
  • 16. Creating Common Types of NDEF Records ▪ 建立 TNF_WELL_KNOWN 且 RTD_TEXT 型態的 Record: 16
  • 17. Creating Common Types of NDEF Records ▪ 建立 TNF_WELL_KNOWN 且 RTD_URI 型態的 Record: ▪ 使用 createUri(String) 靜態建構函式: ▪ 使用 createUri(Uri) 靜態建構函式: ▪ 使用 NdefRecord 建構函式: 17
  • 18. Creating Common Types of NDEF Records ▪ 建立 TNF_EXTERNAL_TYPE 型態的 Record: ▪ 使用 createExternal() 靜態建構函式: ▪ 使用 NdefRecord 建構函式: 18
  • 19. Creating NDEF Message ▪ 準備好 NDEF Records 之後,就可以建立 NDEF Message 將 Records 包 裝起來: ▪ 如果要在 NDEF Message 中放入多個 Records,可參考以下程式碼: NdefMessage msg = new NdefMessage( new NdefRecord[] { ndefRecord }); NdefRecord[] ndefRecord1 = new NdefRecord(...); NdefRecord[] ndefRecord2 = new NdefRecord(...); NdefRecord[] ndefRecord3 = new NdefRecord(...); NdefMessage msg = new NdefMessage( new NdefRecord[] { ndefRecord1, ndefRecord2, ndefRecord3 }); 19
  • 20. Writing to an NDEF Tag ▪ 以下是將 NDEF Message 寫入 NDEF Tag 的簡單範例: Ndef ndef = Ndef.get(tag); if (ndef.isWritable() && ndef.getMaxSize() > ndefMessage.toByteArray().length) { ndef.connect(); ndef.writeNdefMessage(ndefMessage); ndef.close(); } 20
  • 21. Useful Third-Party Libraries ▪ NDEF Tools for Android ▪ NDEF object representation library (no more byte arrays!) ▪ Simple conversion to and from Android SDK low-level equivalent ▪ NFC utility library. Abstract activities for: ▪ Detecting and reading messages ▪ Writing to tags ▪ Beaming (pushing) to other devices ▪ https://code.google.com/p/ndef-tools-for-android/ 21