Android如何實現(xiàn)NFC讀取卡片信息
效果圖

因為朋友需要個讀取NFC卡片數(shù)據(jù)的功能,所以最近看了一下Android 系統(tǒng)下NFC 讀取卡片信息的操作。
NFC(近距離無線通信 ) 是一組近距離無線技術,通常只有在距離不超過 4 厘米時才能啟動連接.借助 NFC,您可以在 NFC 標簽與 Android 設備之間或者兩臺 Android 設備之間共享小型負載。
支持NFC的Android設備同時支持以下三種主要操作模式
- 讀取器/寫入器模式:支持 NFC 設備讀取和/或?qū)懭氡粍?NFC 標簽和貼紙。
- 點對點模式:支持 NFC 設備與其他 NFC 對等設備交換數(shù)據(jù);- Android Beam 使用的就是此操作模式。
- 卡模擬模式:支持 NFC 設備本身充當 NFC 卡。然后,可以通過外部 NFC 讀取器(例如 NFC 銷售終端)訪問模擬 NFC 卡。
NFC讀取卡片數(shù)據(jù)流程
- Android 設備通常會在屏幕解鎖后查找 NFC 標簽(停用NFC除外)
- 卡片接近啟動標簽調(diào)度系統(tǒng)
- 數(shù)據(jù)通過Intent攜帶數(shù)據(jù)啟動Activity
標簽調(diào)度系統(tǒng)定義了三種 Intent,按優(yōu)先級從高到低列出如下:
1. ACTION_NDEF_DISCOVERED:如果掃描到包含 NDEF 負載的標簽,并且可識別其類型,則使用此 Intent 啟動 Activity。這是優(yōu)先級最高的 Intent,標簽調(diào)度系統(tǒng)會盡可能嘗試使用此 Intent 啟動 Activity,在行不通時才會嘗試使用其他 Intent。
2. ACTION_TECH_DISCOVERED :如果沒有登記要處理 ACTION_NDEF_DISCOVERED Intent 的 Activity,則標簽調(diào)度系統(tǒng)會嘗試使用此 Intent 來啟動應用。此外,如果掃描到的標簽包含無法映射到 MIME 類型或 URI 的 NDEF 數(shù)據(jù),或者該標簽不包含 NDEF 數(shù)據(jù),但它使用了已知的標簽技術,那么也會直接啟動此 Intent(無需先啟動 ACTION_NDEF_DISCOVERED)。
3. ACTION_TAG_DISCOVERED:如果沒有處理 ACTION_NDEF_DISCOVERED 或者 ACTION_TECH_DISCOVERED Intent 的 Activity,則使用此 Intent 啟動 Activity。
- 啟動Activity 處理Intent攜帶的數(shù)據(jù)
實現(xiàn)讀取北京地鐵卡數(shù)據(jù)功能
1. 配置NFC權限
<!-- API 級別 9 僅通過 所以最低是10版本-->
<uses-sdk android:minSdkVersion="10" />
<!-- NFC 權限 -->
<uses-permission android:name="android.permission.NFC" />
<!-- 以便您的應用僅在那些具備 NFC 硬件的設備的 Google Play 中顯示:-->
<uses-feature
android:name="android.hardware.nfc"
android:required="true" />2. 配置NFC拉起頁面的過濾器選項
<!--NFC啟動的頁面 -->
<activity android:name=".NFCActivity">
<!-- 配置過濾啟動類型-->
<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 是過濾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. 啟動頁面代碼
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 拉起頁面
*/
class NFCActivity : AppCompatActivity(), NfcAdapter.CreateNdefMessageCallback {
//支持的標簽類型
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, "該機型不支持NFC", Toast.LENGTH_LONG).show()
finish()
}
// Register callback *設置一個回調(diào),使用Android Beam(TM)動態(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())
)
)
}
}這里是簡單的利用NFC讀取卡片數(shù)據(jù)的操作,具體的數(shù)據(jù)處理只是簡單的處理了北京公交卡的數(shù)據(jù),具體項目業(yè)務上需要讀取什么卡數(shù)據(jù)需要項目中具體去處理。
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Android實現(xiàn)GridView中ImageView動態(tài)變換的方法
這篇文章主要介紹了Android實現(xiàn)GridView中ImageView動態(tài)變換的方法,以實例形式較為詳細的分析了GridView中ImageView動態(tài)變換的頁面布局及功能實現(xiàn)相關技巧,需要的朋友可以參考下2015-10-10
Android Retrofit2網(wǎng)路編程實現(xiàn)方法詳解
這篇文章主要介紹了Android Retrofit2網(wǎng)路編程實現(xiàn)方法詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-12-12
android自動安裝apk代碼實例(不使用apk安裝器安裝)
這篇文章主要介紹了android自動安裝apk代碼實例,代碼簡單,大家參考使用吧2013-11-11
Android 進度條按鈕ProgressButton的實現(xiàn)代碼
這篇文章主要介紹了Android 進度條按鈕實現(xiàn)(ProgressButton)代碼,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值 ,需要的朋友可以參考下2018-10-10
Android中ViewFlipper和AdapterViewFlipper使用的方法實例
ViewFlipper和AdapterViewFlipper是Android自帶的一個多頁面管理控件,下面這篇文章主要給大家介紹了關于Android中ViewFlipper和AdapterViewFlipper使用的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-05-05
Android App中制作仿MIUI的Tab切換效果的實例分享
這篇文章主要介紹了Android App中制作仿MIUI的Tab切換效果的實例分享,實現(xiàn)具有跟隨手指滾動而滾動功能的ViewPagerIndicator,需要的朋友可以參考下2016-04-04

