狠狠撸

狠狠撸Share 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

Recently uploaded (9)

Build_With_AI_2025 Gemini 2.0 New Function
Build_With_AI_2025  Gemini 2.0 New FunctionBuild_With_AI_2025  Gemini 2.0 New Function
Build_With_AI_2025 Gemini 2.0 New Function
kevinchiu59
?
原版西蒙菲莎大学毕业证录取书厂贵鲍文凭学位证办理
原版西蒙菲莎大学毕业证录取书厂贵鲍文凭学位证办理原版西蒙菲莎大学毕业证录取书厂贵鲍文凭学位证办理
原版西蒙菲莎大学毕业证录取书厂贵鲍文凭学位证办理
oybyk
?
原版麦吉尔大学毕业证假文凭惭肠骋颈濒濒文凭学位证办理
原版麦吉尔大学毕业证假文凭惭肠骋颈濒濒文凭学位证办理原版麦吉尔大学毕业证假文凭惭肠骋颈濒濒文凭学位证办理
原版麦吉尔大学毕业证假文凭惭肠骋颈濒濒文凭学位证办理
oybyk
?
原版蒙特利尔大学毕业证文凭证书鲍诲别惭文凭学位证办理
原版蒙特利尔大学毕业证文凭证书鲍诲别惭文凭学位证办理原版蒙特利尔大学毕业证文凭证书鲍诲别惭文凭学位证办理
原版蒙特利尔大学毕业证文凭证书鲍诲别惭文凭学位证办理
oybyk
?
Cantonmade 2025 Hotel Supplier Catalog: Technical Specs for Engineers & Integ...
Cantonmade 2025 Hotel Supplier Catalog: Technical Specs for Engineers & Integ...Cantonmade 2025 Hotel Supplier Catalog: Technical Specs for Engineers & Integ...
Cantonmade 2025 Hotel Supplier Catalog: Technical Specs for Engineers & Integ...
RayChan91
?
原版康卡迪亚大学硕士学位证成绩单颁辞苍肠辞谤诲颈补文凭学位证办理
原版康卡迪亚大学硕士学位证成绩单颁辞苍肠辞谤诲颈补文凭学位证办理原版康卡迪亚大学硕士学位证成绩单颁辞苍肠辞谤诲颈补文凭学位证办理
原版康卡迪亚大学硕士学位证成绩单颁辞苍肠辞谤诲颈补文凭学位证办理
oybyk
?
原版维多利亚大学硕士毕业证成绩单鲍痴颈肠文凭学位证办理
原版维多利亚大学硕士毕业证成绩单鲍痴颈肠文凭学位证办理原版维多利亚大学硕士毕业证成绩单鲍痴颈肠文凭学位证办理
原版维多利亚大学硕士毕业证成绩单鲍痴颈肠文凭学位证办理
oybyk
?
原版莫纳什大学本科毕业证成绩单惭辞苍补蝉丑文凭学位证办理
原版莫纳什大学本科毕业证成绩单惭辞苍补蝉丑文凭学位证办理原版莫纳什大学本科毕业证成绩单惭辞苍补蝉丑文凭学位证办理
原版莫纳什大学本科毕业证成绩单惭辞苍补蝉丑文凭学位证办理
oybyk
?
原版昆士兰大学毕业证成绩单鲍蚕文凭学位证办理
原版昆士兰大学毕业证成绩单鲍蚕文凭学位证办理原版昆士兰大学毕业证成绩单鲍蚕文凭学位证办理
原版昆士兰大学毕业证成绩单鲍蚕文凭学位证办理
oybyk
?
Build_With_AI_2025 Gemini 2.0 New Function
Build_With_AI_2025  Gemini 2.0 New FunctionBuild_With_AI_2025  Gemini 2.0 New Function
Build_With_AI_2025 Gemini 2.0 New Function
kevinchiu59
?
原版西蒙菲莎大学毕业证录取书厂贵鲍文凭学位证办理
原版西蒙菲莎大学毕业证录取书厂贵鲍文凭学位证办理原版西蒙菲莎大学毕业证录取书厂贵鲍文凭学位证办理
原版西蒙菲莎大学毕业证录取书厂贵鲍文凭学位证办理
oybyk
?
原版麦吉尔大学毕业证假文凭惭肠骋颈濒濒文凭学位证办理
原版麦吉尔大学毕业证假文凭惭肠骋颈濒濒文凭学位证办理原版麦吉尔大学毕业证假文凭惭肠骋颈濒濒文凭学位证办理
原版麦吉尔大学毕业证假文凭惭肠骋颈濒濒文凭学位证办理
oybyk
?
原版蒙特利尔大学毕业证文凭证书鲍诲别惭文凭学位证办理
原版蒙特利尔大学毕业证文凭证书鲍诲别惭文凭学位证办理原版蒙特利尔大学毕业证文凭证书鲍诲别惭文凭学位证办理
原版蒙特利尔大学毕业证文凭证书鲍诲别惭文凭学位证办理
oybyk
?
Cantonmade 2025 Hotel Supplier Catalog: Technical Specs for Engineers & Integ...
Cantonmade 2025 Hotel Supplier Catalog: Technical Specs for Engineers & Integ...Cantonmade 2025 Hotel Supplier Catalog: Technical Specs for Engineers & Integ...
Cantonmade 2025 Hotel Supplier Catalog: Technical Specs for Engineers & Integ...
RayChan91
?
原版康卡迪亚大学硕士学位证成绩单颁辞苍肠辞谤诲颈补文凭学位证办理
原版康卡迪亚大学硕士学位证成绩单颁辞苍肠辞谤诲颈补文凭学位证办理原版康卡迪亚大学硕士学位证成绩单颁辞苍肠辞谤诲颈补文凭学位证办理
原版康卡迪亚大学硕士学位证成绩单颁辞苍肠辞谤诲颈补文凭学位证办理
oybyk
?
原版维多利亚大学硕士毕业证成绩单鲍痴颈肠文凭学位证办理
原版维多利亚大学硕士毕业证成绩单鲍痴颈肠文凭学位证办理原版维多利亚大学硕士毕业证成绩单鲍痴颈肠文凭学位证办理
原版维多利亚大学硕士毕业证成绩单鲍痴颈肠文凭学位证办理
oybyk
?
原版莫纳什大学本科毕业证成绩单惭辞苍补蝉丑文凭学位证办理
原版莫纳什大学本科毕业证成绩单惭辞苍补蝉丑文凭学位证办理原版莫纳什大学本科毕业证成绩单惭辞苍补蝉丑文凭学位证办理
原版莫纳什大学本科毕业证成绩单惭辞苍补蝉丑文凭学位证办理
oybyk
?
原版昆士兰大学毕业证成绩单鲍蚕文凭学位证办理
原版昆士兰大学毕业证成绩单鲍蚕文凭学位证办理原版昆士兰大学毕业证成绩单鲍蚕文凭学位证办理
原版昆士兰大学毕业证成绩单鲍蚕文凭学位证办理
oybyk
?

Featured (20)

2024 Trend Updates: What Really Works In SEO & Content Marketing
2024 Trend Updates: What Really Works In SEO & Content Marketing2024 Trend Updates: What Really Works In SEO & Content Marketing
2024 Trend Updates: What Really Works In SEO & Content Marketing
Search Engine Journal
?
Storytelling For The Web: Integrate Storytelling in your Design Process
Storytelling For The Web: Integrate Storytelling in your Design ProcessStorytelling For The Web: Integrate Storytelling in your Design Process
Storytelling For The Web: Integrate Storytelling in your Design Process
Chiara Aliotta
?
Artificial Intelligence, Data and Competition – SCHREPEL – June 2024 OECD dis...
Artificial Intelligence, Data and Competition – SCHREPEL – June 2024 OECD dis...Artificial Intelligence, Data and Competition – SCHREPEL – June 2024 OECD dis...
Artificial Intelligence, Data and Competition – SCHREPEL – June 2024 OECD dis...
OECD Directorate for Financial and Enterprise Affairs
?
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
SocialHRCamp
?
2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
Marius Sescu
?
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
Expeed Software
?
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
Pixeldarts
?
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
?
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
?
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
Skeleton Technologies
?
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
?
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
contently
?
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
?
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
?
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
?
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
?
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
?
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero
?
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
?
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
?
2024 Trend Updates: What Really Works In SEO & Content Marketing
2024 Trend Updates: What Really Works In SEO & Content Marketing2024 Trend Updates: What Really Works In SEO & Content Marketing
2024 Trend Updates: What Really Works In SEO & Content Marketing
Search Engine Journal
?
Storytelling For The Web: Integrate Storytelling in your Design Process
Storytelling For The Web: Integrate Storytelling in your Design ProcessStorytelling For The Web: Integrate Storytelling in your Design Process
Storytelling For The Web: Integrate Storytelling in your Design Process
Chiara Aliotta
?
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
SocialHRCamp
?
2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
Marius Sescu
?
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
Expeed Software
?
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
Pixeldarts
?
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
?
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
?
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
?
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
contently
?
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
?
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
?
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
?
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
?
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero
?
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
?

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