SlideShare a Scribd company logo
1 of 22
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

What's hot

Study of 5G FAPI Specification
Study of 5G FAPI SpecificationStudy of 5G FAPI Specification
Study of 5G FAPI SpecificationImamNurBaniYusuf
 
Nfc Overview
Nfc OverviewNfc Overview
Nfc Overviewmehdibs
 
Packet sniffer repot
Packet sniffer repotPacket sniffer repot
Packet sniffer repotKunal Thakur
 
Generic framing protocol
Generic framing protocolGeneric framing protocol
Generic framing protocolMapYourTech
 
Wifi direct technology a technical report
Wifi direct technology   a technical reportWifi direct technology   a technical report
Wifi direct technology a technical reportAngelos Alevizopoulos
 
Synchronization Architecture for 3G and 4G Networks
Synchronization Architecture for 3G and 4G NetworksSynchronization Architecture for 3G and 4G Networks
Synchronization Architecture for 3G and 4G NetworksSymmetricomSYMM
 
Mac protocols
Mac protocolsMac protocols
Mac protocolsjuno susi
 
Hacking Gsm - Secret Keys Revealed
Hacking Gsm - Secret Keys RevealedHacking Gsm - Secret Keys Revealed
Hacking Gsm - Secret Keys Revealedshlominar
 
Near Field Communication (NFC Architecture and Operating Modes)
Near Field Communication (NFC Architecture and Operating Modes)Near Field Communication (NFC Architecture and Operating Modes)
Near Field Communication (NFC Architecture and Operating Modes)Deepak Kl
 
NFC: Shaping the Future of the Connected Customer Experience
NFC: Shaping the Future of the Connected Customer ExperienceNFC: Shaping the Future of the Connected Customer Experience
NFC: Shaping the Future of the Connected Customer ExperienceNFC Forum
 
Wireless sensors networks protocols
Wireless sensors networks protocolsWireless sensors networks protocols
Wireless sensors networks protocolsRushin Shah
 
Arp (address resolution protocol)
Arp (address resolution protocol)Arp (address resolution protocol)
Arp (address resolution protocol)tigerbt
 
Lecture 9 electronic_mail_representation_and_transfer
Lecture 9 electronic_mail_representation_and_transferLecture 9 electronic_mail_representation_and_transfer
Lecture 9 electronic_mail_representation_and_transferSerious_SamSoul
 
Architecture and Development of NFC Applications
Architecture and Development of NFC ApplicationsArchitecture and Development of NFC Applications
Architecture and Development of NFC ApplicationsThomas de Lazzari
 

What's hot (20)

Near field communication
Near field communicationNear field communication
Near field communication
 
Study of 5G FAPI Specification
Study of 5G FAPI SpecificationStudy of 5G FAPI Specification
Study of 5G FAPI Specification
 
Nfc Overview
Nfc OverviewNfc Overview
Nfc Overview
 
Packet sniffer repot
Packet sniffer repotPacket sniffer repot
Packet sniffer repot
 
Generic framing protocol
Generic framing protocolGeneric framing protocol
Generic framing protocol
 
Wifi direct technology a technical report
Wifi direct technology   a technical reportWifi direct technology   a technical report
Wifi direct technology a technical report
 
Aircrack
AircrackAircrack
Aircrack
 
Synchronization Architecture for 3G and 4G Networks
Synchronization Architecture for 3G and 4G NetworksSynchronization Architecture for 3G and 4G Networks
Synchronization Architecture for 3G and 4G Networks
 
Mac protocols
Mac protocolsMac protocols
Mac protocols
 
Hacking Gsm - Secret Keys Revealed
Hacking Gsm - Secret Keys RevealedHacking Gsm - Secret Keys Revealed
Hacking Gsm - Secret Keys Revealed
 
POP3 Post Office Protocol
POP3 Post Office ProtocolPOP3 Post Office Protocol
POP3 Post Office Protocol
 
Near Field Communication (NFC Architecture and Operating Modes)
Near Field Communication (NFC Architecture and Operating Modes)Near Field Communication (NFC Architecture and Operating Modes)
Near Field Communication (NFC Architecture and Operating Modes)
 
NFC: Shaping the Future of the Connected Customer Experience
NFC: Shaping the Future of the Connected Customer ExperienceNFC: Shaping the Future of the Connected Customer Experience
NFC: Shaping the Future of the Connected Customer Experience
 
Wireless sensors networks protocols
Wireless sensors networks protocolsWireless sensors networks protocols
Wireless sensors networks protocols
 
Nfc
NfcNfc
Nfc
 
Arp (address resolution protocol)
Arp (address resolution protocol)Arp (address resolution protocol)
Arp (address resolution protocol)
 
Near Field Communiation
Near Field CommuniationNear Field Communiation
Near Field Communiation
 
X.25 protocol
X.25 protocolX.25 protocol
X.25 protocol
 
Lecture 9 electronic_mail_representation_and_transfer
Lecture 9 electronic_mail_representation_and_transferLecture 9 electronic_mail_representation_and_transfer
Lecture 9 electronic_mail_representation_and_transfer
 
Architecture and Development of NFC Applications
Architecture and Development of NFC ApplicationsArchitecture and Development of NFC Applications
Architecture and Development of NFC Applications
 

Viewers also liked

Mobility Aware Distributed Service Composition Framework in SOA based MANET A...
Mobility Aware Distributed Service Composition Framework in SOA based MANET A...Mobility Aware Distributed Service Composition Framework in SOA based MANET A...
Mobility Aware Distributed Service Composition Framework in SOA based MANET A...Chun-Kai Wang
 
Introduction to CodeIgniter
Introduction to CodeIgniterIntroduction to CodeIgniter
Introduction to CodeIgniterChun-Kai Wang
 
Android NFC Application Development Environment Setup
Android NFC Application Development Environment SetupAndroid NFC Application Development Environment Setup
Android NFC Application Development Environment SetupChun-Kai Wang
 
NFC-based User Authentication Mechanisms for Personalized IPTV Services
NFC-based User Authentication Mechanisms for Personalized IPTV ServicesNFC-based User Authentication Mechanisms for Personalized IPTV Services
NFC-based User Authentication Mechanisms for Personalized IPTV ServicesChun-Kai Wang
 
友增吾簡-互動式名片系統
友增吾簡-互動式名片系統友增吾簡-互動式名片系統
友增吾簡-互動式名片系統Chun-Kai Wang
 
Android Application Development of NFC Peer-to-Peer Mode
Android Application Development of NFC Peer-to-Peer ModeAndroid Application Development of NFC Peer-to-Peer Mode
Android Application Development of NFC Peer-to-Peer ModeChun-Kai Wang
 
Nfc reader writer_mode
Nfc reader writer_modeNfc reader writer_mode
Nfc reader writer_modeChun-Kai Wang
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDBChun-Kai Wang
 

Viewers also liked (8)

Mobility Aware Distributed Service Composition Framework in SOA based MANET A...
Mobility Aware Distributed Service Composition Framework in SOA based MANET A...Mobility Aware Distributed Service Composition Framework in SOA based MANET A...
Mobility Aware Distributed Service Composition Framework in SOA based MANET A...
 
Introduction to CodeIgniter
Introduction to CodeIgniterIntroduction to CodeIgniter
Introduction to CodeIgniter
 
Android NFC Application Development Environment Setup
Android NFC Application Development Environment SetupAndroid NFC Application Development Environment Setup
Android NFC Application Development Environment Setup
 
NFC-based User Authentication Mechanisms for Personalized IPTV Services
NFC-based User Authentication Mechanisms for Personalized IPTV ServicesNFC-based User Authentication Mechanisms for Personalized IPTV Services
NFC-based User Authentication Mechanisms for Personalized IPTV Services
 
友增吾簡-互動式名片系統
友增吾簡-互動式名片系統友增吾簡-互動式名片系統
友增吾簡-互動式名片系統
 
Android Application Development of NFC Peer-to-Peer Mode
Android Application Development of NFC Peer-to-Peer ModeAndroid Application Development of NFC Peer-to-Peer Mode
Android Application Development of NFC Peer-to-Peer Mode
 
Nfc reader writer_mode
Nfc reader writer_modeNfc reader writer_mode
Nfc reader writer_mode
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 

Android Application Development of 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