最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2023年11月22日 16:58:43   作者:金戈鐡馬  
這篇文章主要介紹了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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

最新評論

老河口市| 开远市| 漾濞| 都昌县| 潍坊市| 台州市| 玉龙| 大悟县| 江西省| 定州市| 香港| 师宗县| 三门县| 乐昌市| 库尔勒市| 皋兰县| 延川县| 河源市| 安泽县| 黎川县| 雅江县| 尼勒克县| 农安县| 株洲市| 西吉县| 东至县| 嘉鱼县| 乌兰县| 新沂市| 绥芬河市| 佛冈县| 若羌县| 曲麻莱县| 辽中县| 阿荣旗| 舒兰市| 海城市| 锡林浩特市| 阳高县| 廊坊市| 阿合奇县|