Android如何實(shí)現(xiàn)NFC讀取卡片信息
效果圖
因?yàn)榕笥研枰獋€(gè)讀取NFC卡片數(shù)據(jù)的功能,所以最近看了一下Android 系統(tǒng)下NFC 讀取卡片信息的操作。
NFC(近距離無(wú)線通信 ) 是一組近距離無(wú)線技術(shù),通常只有在距離不超過(guò) 4 厘米時(shí)才能啟動(dòng)連接.借助 NFC,您可以在 NFC 標(biāo)簽與 Android 設(shè)備之間或者兩臺(tái) Android 設(shè)備之間共享小型負(fù)載。
支持NFC的Android設(shè)備同時(shí)支持以下三種主要操作模式
- 讀取器/寫(xiě)入器模式:支持 NFC 設(shè)備讀取和/或?qū)懭氡粍?dòng) NFC 標(biāo)簽和貼紙。
- 點(diǎn)對(duì)點(diǎn)模式:支持 NFC 設(shè)備與其他 NFC 對(duì)等設(shè)備交換數(shù)據(jù);- Android Beam 使用的就是此操作模式。
- 卡模擬模式:支持 NFC 設(shè)備本身充當(dāng) NFC 卡。然后,可以通過(guò)外部 NFC 讀取器(例如 NFC 銷(xiāo)售終端)訪問(wèn)模擬 NFC 卡。
NFC讀取卡片數(shù)據(jù)流程
- Android 設(shè)備通常會(huì)在屏幕解鎖后查找 NFC 標(biāo)簽(停用NFC除外)
- 卡片接近啟動(dòng)標(biāo)簽調(diào)度系統(tǒng)
- 數(shù)據(jù)通過(guò)Intent攜帶數(shù)據(jù)啟動(dòng)Activity
標(biāo)簽調(diào)度系統(tǒng)定義了三種 Intent,按優(yōu)先級(jí)從高到低列出如下:
1. ACTION_NDEF_DISCOVERED:如果掃描到包含 NDEF 負(fù)載的標(biāo)簽,并且可識(shí)別其類(lèi)型,則使用此 Intent 啟動(dòng) Activity。這是優(yōu)先級(jí)最高的 Intent,標(biāo)簽調(diào)度系統(tǒng)會(huì)盡可能?chē)L試使用此 Intent 啟動(dòng) Activity,在行不通時(shí)才會(huì)嘗試使用其他 Intent。
2. ACTION_TECH_DISCOVERED :如果沒(méi)有登記要處理 ACTION_NDEF_DISCOVERED Intent 的 Activity,則標(biāo)簽調(diào)度系統(tǒng)會(huì)嘗試使用此 Intent 來(lái)啟動(dòng)應(yīng)用。此外,如果掃描到的標(biāo)簽包含無(wú)法映射到 MIME 類(lèi)型或 URI 的 NDEF 數(shù)據(jù),或者該標(biāo)簽不包含 NDEF 數(shù)據(jù),但它使用了已知的標(biāo)簽技術(shù),那么也會(huì)直接啟動(dòng)此 Intent(無(wú)需先啟動(dòng) ACTION_NDEF_DISCOVERED)。
3. ACTION_TAG_DISCOVERED:如果沒(méi)有處理 ACTION_NDEF_DISCOVERED 或者 ACTION_TECH_DISCOVERED Intent 的 Activity,則使用此 Intent 啟動(dòng) Activity。
- 啟動(dòng)Activity 處理Intent攜帶的數(shù)據(jù)
實(shí)現(xiàn)讀取北京地鐵卡數(shù)據(jù)功能
1. 配置NFC權(quán)限
<!-- API 級(jí)別 9 僅通過(guò) 所以最低是10版本--> <uses-sdk android:minSdkVersion="10" /> <!-- NFC 權(quán)限 --> <uses-permission android:name="android.permission.NFC" /> <!-- 以便您的應(yīng)用僅在那些具備 NFC 硬件的設(shè)備的 Google Play 中顯示:--> <uses-feature android:name="android.hardware.nfc" android:required="true" />
2. 配置NFC拉起頁(yè)面的過(guò)濾器選項(xiàng)
<!--NFC啟動(dòng)的頁(yè)面 --> <activity android:name=".NFCActivity"> <!-- 配置過(guò)濾啟動(dòng)類(lèi)型--> <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" /> <!-- <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>--> <!-- <intent-filter>--> <!-- <action android:name="android.nfc.action.NDEF_DISCOVERED"/>--> <!-- <category android:name="android.intent.category.DEFAULT"/>--> <!-- <data android:mimeType="text/plain" />--> <!-- </intent-filter>--> </activity>
注意 nfc_tech_filter.xml 是過(guò)濾NFC 卡片類(lèi)型
<?xml version="1.0" encoding="utf-8"?> <resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> <!-- 可以處理所有Android支持的NFC類(lèi)型 --> <tech-list> <tech>android.nfc.tech.IsoDep</tech> </tech-list> <tech-list> <tech>android.nfc.tech.NfcA</tech> </tech-list> <tech-list> <tech>android.nfc.tech.NfcB</tech> </tech-list> <tech-list> <tech>android.nfc.tech.NfcF</tech> </tech-list> <tech-list> <tech>android.nfc.tech.NfcV</tech> </tech-list> <tech-list> <tech>android.nfc.tech.Ndef</tech> </tech-list> <tech-list> <tech>android.nfc.tech.NdefFormatable</tech> </tech-list> <tech-list> <tech>android.nfc.tech.MifareUltralight</tech> </tech-list> <tech-list> <tech>android.nfc.tech.MifareClassic</tech> </tech-list> </resources>
4. 啟動(dòng)頁(yè)面代碼
import android.content.Intent import android.nfc.NdefMessage import android.nfc.NdefRecord.createMime import android.nfc.NfcAdapter import android.nfc.NfcEvent import android.nfc.Tag import android.os.Bundle import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import androidx.databinding.DataBindingUtil import com.wkq.nfc.databinding.ActivityMainBinding /** * NFC 拉起頁(yè)面 */ class NFCActivity : AppCompatActivity(), NfcAdapter.CreateNdefMessageCallback { //支持的標(biāo)簽類(lèi)型 private var nfcAdapter: NfcAdapter? = null private var binding: ActivityMainBinding? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main) nfcAdapter = NfcAdapter.getDefaultAdapter(this) if (nfcAdapter==null){ Toast.makeText(this, "該機(jī)型不支持NFC", Toast.LENGTH_LONG).show() finish() } // Register callback *設(shè)置一個(gè)回調(diào),使用Android Beam(TM)動(dòng)態(tài)生成要發(fā)送的NDEF消息。 nfcAdapter?.setNdefPushMessageCallback(this, this) } override fun onResume() { super.onResume() // Check to see that the Activity started due to an Android Beam if (NfcAdapter.ACTION_TECH_DISCOVERED == intent.action) { processIntent(intent) } } override fun onPause() { super.onPause() nfcAdapter!!.disableReaderMode(this) } override fun onNewIntent(intent: Intent?) { super.onNewIntent(intent) setIntent(intent) } /** * 處理Intent攜帶的數(shù)據(jù) */ private fun processIntent(intent: Intent) { // 處理北京公交卡的數(shù)據(jù) var tag = intent.extras if (tag==null)return var content = NFCUtil.bytesToHex((tag!!.get("android.nfc.extra.TAG") as Tag).id) binding?.tvContent!!.text = content Toast.makeText(this, "獲取北京地鐵卡數(shù)據(jù):" + content, Toast.LENGTH_LONG).show() } override fun createNdefMessage(event: NfcEvent?): NdefMessage { val text = "Beam me up, Android!\n\n" + "Beam Time: " + System.currentTimeMillis() return NdefMessage( arrayOf( createMime("application/vnd.com.example.android.beam", text.toByteArray()) ) ) } }
這里是簡(jiǎn)單的利用NFC讀取卡片數(shù)據(jù)的操作,具體的數(shù)據(jù)處理只是簡(jiǎn)單的處理了北京公交卡的數(shù)據(jù),具體項(xiàng)目業(yè)務(wù)上需要讀取什么卡數(shù)據(jù)需要項(xiàng)目中具體去處理。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Kotlin協(xié)程flowOn與線程切換超詳細(xì)示例介紹
這篇文章主要介紹了Kotlin協(xié)程flowOn與線程切換,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-09-09Android快速實(shí)現(xiàn)一個(gè)財(cái)務(wù)APP程序詳解
這篇文章主要介紹了Android實(shí)現(xiàn)的財(cái)務(wù)APP程序,結(jié)合前后端共功能完善,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07Android 實(shí)現(xiàn)仿QQ拖拽氣泡效果的示例
這篇文章主要介紹了Android 實(shí)現(xiàn)仿QQ拖拽氣泡效果的示例,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2021-04-04Android計(jì)時(shí)與倒計(jì)時(shí)實(shí)現(xiàn)限時(shí)搶購(gòu)的5種方法
這篇文章主要為大家詳細(xì)介紹了Android計(jì)時(shí)與倒計(jì)時(shí)實(shí)現(xiàn)限時(shí)搶購(gòu)的5種方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02Android Camera2實(shí)現(xiàn)最簡(jiǎn)單的預(yù)覽框顯示
這篇文章主要為大家詳細(xì)介紹了Android Camera2實(shí)現(xiàn)最簡(jiǎn)單的預(yù)覽框顯示,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05