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í)支持以下三種主要操作模式
- 讀取器/寫入器模式:支持 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 銷售終端)訪問模擬 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í)別其類型,則使用此 Intent 啟動(dòng) Activity。這是優(yōu)先級(jí)最高的 Intent,標(biāo)簽調(diào)度系統(tǒng)會(huì)盡可能嘗試使用此 Intent 啟動(dòng) Activity,在行不通時(shí)才會(huì)嘗試使用其他 Intent。
2. ACTION_TECH_DISCOVERED :如果沒有登記要處理 ACTION_NDEF_DISCOVERED Intent 的 Activity,則標(biāo)簽調(diào)度系統(tǒng)會(huì)嘗試使用此 Intent 來(lái)啟動(dòng)應(yīng)用。此外,如果掃描到的標(biāo)簽包含無(wú)法映射到 MIME 類型或 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:如果沒有處理 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)類型-->
<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 卡片類型
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<!-- 可以處理所有Android支持的NFC類型 -->
<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)簽類型
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)文章
Android編程實(shí)現(xiàn)圖標(biāo)拖動(dòng)效果的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)圖標(biāo)拖動(dòng)效果的方法,涉及Android事件響應(yīng)及圖標(biāo)變換的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11
Android實(shí)現(xiàn)GridView中ImageView動(dòng)態(tài)變換的方法
這篇文章主要介紹了Android實(shí)現(xiàn)GridView中ImageView動(dòng)態(tài)變換的方法,以實(shí)例形式較為詳細(xì)的分析了GridView中ImageView動(dòng)態(tài)變換的頁(yè)面布局及功能實(shí)現(xiàn)相關(guān)技巧,需要的朋友可以參考下2015-10-10
Android懸浮窗屏蔽懸浮窗外部所有的點(diǎn)擊事件的實(shí)例代碼
這篇文章主要介紹了Android懸浮窗屏蔽懸浮窗外部所有的點(diǎn)擊事件實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03
Android Retrofit2網(wǎng)路編程實(shí)現(xiàn)方法詳解
這篇文章主要介紹了Android Retrofit2網(wǎng)路編程實(shí)現(xiàn)方法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12
android自動(dòng)安裝apk代碼實(shí)例(不使用apk安裝器安裝)
這篇文章主要介紹了android自動(dòng)安裝apk代碼實(shí)例,代碼簡(jiǎn)單,大家參考使用吧2013-11-11
Android 進(jìn)度條按鈕ProgressButton的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android 進(jìn)度條按鈕實(shí)現(xiàn)(ProgressButton)代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2018-10-10
Android中ViewFlipper和AdapterViewFlipper使用的方法實(shí)例
ViewFlipper和AdapterViewFlipper是Android自帶的一個(gè)多頁(yè)面管理控件,下面這篇文章主要給大家介紹了關(guān)于Android中ViewFlipper和AdapterViewFlipper使用的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05
Android App中制作仿MIUI的Tab切換效果的實(shí)例分享
這篇文章主要介紹了Android App中制作仿MIUI的Tab切換效果的實(shí)例分享,實(shí)現(xiàn)具有跟隨手指滾動(dòng)而滾動(dòng)功能的ViewPagerIndicator,需要的朋友可以參考下2016-04-04

