当前位置: 代码网 > it编程>App开发>Android > Android如何实现NFC读取卡片信息

Android如何实现NFC读取卡片信息

2024年05月19日 Android 我要评论
效果图因为朋友需要个读取nfc卡片数据的功能,所以最近看了一下android 系统下nfc 读取卡片信息的操作。nfc(近距离无线通信 ) 是一组近距离无线技术,通常只有在距离不超过 4 厘米时才能启

效果图

因为朋友需要个读取nfc卡片数据的功能,所以最近看了一下android 系统下nfc 读取卡片信息的操作。

nfc(近距离无线通信 ) 是一组近距离无线技术,通常只有在距离不超过 4 厘米时才能启动连接.借助 nfc,您可以在 nfc 标签与 android 设备之间或者两台 android 设备之间共享小型负载。

支持nfc的android设备同时支持以下三种主要操作模式

  • 读取器/写入器模式:支持 nfc 设备读取和/或写入被动 nfc 标签和贴纸。
  • 点对点模式:支持 nfc 设备与其他 nfc 对等设备交换数据;- android beam 使用的就是此操作模式。
  • 卡模拟模式:支持 nfc 设备本身充当 nfc 卡。然后,可以通过外部 nfc 读取器(例如 nfc 销售终端)访问模拟 nfc 卡。

nfc读取卡片数据流程

  • android 设备通常会在屏幕解锁后查找 nfc 标签(停用nfc除外)
  • 卡片接近启动标签调度系统
  • 数据通过intent携带数据启动activity

标签调度系统定义了三种 intent,按优先级从高到低列出如下:    

1.  action_ndef_discovered:如果扫描到包含 ndef 负载的标签,并且可识别其类型,则使用此 intent 启动 activity。这是优先级最高的 intent,标签调度系统会尽可能尝试使用此 intent 启动 activity,在行不通时才会尝试使用其他 intent。    

2.  action_tech_discovered :如果没有登记要处理 action_ndef_discovered intent 的 activity,则标签调度系统会尝试使用此 intent 来启动应用。此外,如果扫描到的标签包含无法映射到 mime 类型或 uri 的 ndef 数据,或者该标签不包含 ndef 数据,但它使用了已知的标签技术,那么也会直接启动此 intent(无需先启动 action_ndef_discovered)。    

3.  action_tag_discovered:如果没有处理 action_ndef_discovered 或者 action_tech_discovered intent 的 activity,则使用此 intent 启动 activity。

  • 启动activity 处理intent携带的数据

实现读取北京地铁卡数据功能

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  *设置一个回调,使用android beam(tm)动态生成要发送的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携带的数据
     */
    private fun processintent(intent: intent) {
        // 处理北京公交卡的数据
        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, "获取北京地铁卡数据:" + 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读取卡片数据的操作,具体的数据处理只是简单的处理了北京公交卡的数据,具体项目业务上需要读取什么卡数据需要项目中具体去处理。

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com