Android读取NFC卡片数据

这里以nfc公交卡为例子

使用如下
  1. 请将需要获取nfc数据的 Activity 对应的清单文件里面设置成 栈顶模式,并在intent 标签内添加 action -- nfc
java 复制代码
android:launchMode="singleTop"
java 复制代码
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
  1. 在对应Activity内的各个生命周期内加入以下代码
onCreate 创建nfc

初始化NFC show 里面是说明当前设备是否支持NFC是否打开NFC

java 复制代码
NfcUtil.createNfc(this,object : NfcUtil.NFCCreateBack{
            override fun show(s: String) {
                Log.e("NFC_TAG", "createNfc: -${s}", )
            }
        })

该设备是否支持NFC

java 复制代码
NfcUtil.getNFC(this)

跳到手机系统NFC设置界面

java 复制代码
NfcUtil.jumpNFC()
onResume 开启nfc监听
java 复制代码
NfcUtil.resumeNfc()
onPause 关闭nfc监听
java 复制代码
NfcUtil.pauseNfc()
onNewIntent 获取监听到的数据
java 复制代码
//这里必须setIntent,set  NFC事件响应后的intent才能拿到数据
        setIntent(intent)
        NfcUtil.resolveIntent(intent!!,object: NfcUtil.NFCBack{
            override fun onSuc(s: NfcUtil.NFCBean) {
                tv1?.text = "名称: ${s.tagID_hex}"
                tv2?.text = "ID: ${s.tagID_dec}"
                tv3?.text = "内存大小: ${s.tagID_size}"
                tv4?.text = "名称: ${s.msg}"
            }

        })

NfcUtil

java 复制代码
/**
 * Nfc读取工具类
 * */
@SuppressLint("StaticFieldLeak")
object NfcUtil {

    private var activity: Activity? = null
    var mNfcAdapter: NfcAdapter? = null
    var pIntent: PendingIntent? = null
    lateinit var mIntentFilter: Array<IntentFilter>
    lateinit var mTechList: Array<Array<String>>

    //创建必要数据
    fun createNfc(a: Activity,c: NFCCreateBack){
        activity = a
        mNfcAdapter = NfcAdapter.getDefaultAdapter(activity)
        if (mNfcAdapter == null) {
            c.show("设备不支持NFC!")
        }else{
            if (!mNfcAdapter!!.isEnabled) {
                c.show("请在系统设置中先启用NFC功能!")
            }
            pIntent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
                PendingIntent.getActivity(activity, 0, Intent(activity,activity?.javaClass).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), PendingIntent.FLAG_MUTABLE )
            } else {
                PendingIntent.getActivity(activity, 0, Intent(activity, activity?.javaClass).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0)
            }
        }
    }

    //创建回调
    interface NFCCreateBack{
        fun show(s: String)
    }


    //进入界面调用
    fun resumeNfc(){
        mNfcAdapter?.let {
            //intentFilter过滤----ndef
            val ndefFilter = IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED)
            try {
                //文本类型
                ndefFilter.addDataType("text/plain")
            } catch (e: MalformedMimeTypeException) {
                e.printStackTrace()
            }
            //intentFilter过滤----非ndef
            val techFilter = IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED)
            val tag = IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED)

            //intentFilter过滤器列表
            mIntentFilter = arrayOf(ndefFilter,techFilter,tag)
            //匹配的数据格式列表
            mTechList = arrayOf(
                arrayOf(MifareClassic::class.java.name),
                arrayOf(NfcA::class.java.name),
                arrayOf(Ndef::class.java.name),
                arrayOf(NdefFormatable::class.java.name)
            )
            it.enableForegroundDispatch(activity, pIntent, mIntentFilter, mTechList)
        }
    }

    //去除NFC监听
    fun pauseNfc(){
        mNfcAdapter?.disableForegroundDispatch(activity)
    }

    //处理接收到的NFC数据
    fun resolveIntent(intent: Intent,nfcBack: NFCBack){
        val action = intent.action
        Log.e("NFC_TAG", "action Type: $action")
       if (NfcAdapter.ACTION_TECH_DISCOVERED == intent.action) {
            val tag1: Parcelable? = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG)
            if (tag1 != null) {
                Log.e("NFC_TAG", "解析数据: ${dumpTagData66(tag1,nfcBack)}")
                dumpTagData66(tag1,nfcBack)
            }
        }else {
           val tag: Tag? = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG)
           var tagIDHex: String = ""
           var tagIDDec: String = ""
           val tagIDSize: String = "1024"
           if (tag != null) {
               val nfcId = dumpTagData(tag)
               tagIDHex = getHex(tag.id).toString()
               tagIDDec = getDec(tag.id).toString()
               if (nfcId.isNotEmpty()) {
                   Log.e("NFC_TAG", "接收到卡片ID: $nfcId")
               } else {
                   Log.e("NFC_TAG", "识别失败!请重新刷卡!")
               }

           }
           nfcBack.onSuc(NFCBean(tagIDHex,tagIDDec,tagIDSize,""))
       }
    }

    //回调
    interface NFCBack{
        fun onSuc(s: NFCBean)
    }

    //数据内容实体类
    data class NFCBean(
        var tagID_hex: String? = "", //id,不同的解析
        var tagID_dec: String? = "", //id,不同的解析
        var tagID_size: String? = "", //id,不同的解析
        var msg: String? = "" //详细数据
    )

    private fun dumpTagData(p: Tag): String {
        val id = p.id
        return bytesToHexString(id)
    }

    /**
     * 将字节数组转换为字符串
     */
    private fun bytesToHexString(inT: ByteArray): String {
        var i: Int
        var iny: Int
        val hex =
            arrayOf("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F")
        var out = ""
        var j: Int = 0
        while (j < inT.size) {
            iny = inT[j].toInt() and 0xff
            i = iny shr 4 and 0x0f
            out += hex[i]
            i = iny and 0x0f
            out += hex[i]
            ++j
        }
        return out
    }

    //一般公家卡,扫描的信息
    private fun dumpTagData66(p: Parcelable,nfcBack: NFCBack): String {

        var tagIDHex: String = ""
        var tagIDDec: String = ""
        var tagIDSize: String = "1024"
        var msg = ""

        val sb = java.lang.StringBuilder()
        val tag = p as Tag
        val id = tag.id
        sb.append("Tag ID (hex): ").append(getHex(id)).append("\n")
        sb.append("Tag ID (dec): ").append(getDec(id)).append("\n")
        sb.append("ID (reversed): ").append(getReversed(id)).append("\n")
        val prefix = "android.nfc.tech."
        sb.append("Technologies: ")

        tagIDHex = getHex(id).toString()
        tagIDDec = getDec(id).toString()
        for (tech in tag.techList) {
            sb.append(tech.substring(prefix.length))
            sb.append(", ")
        }
        sb.delete(sb.length - 2, sb.length)
        for (tech in tag.techList) {
            if (tech == MifareClassic::class.java.name) {
                sb.append('\n')
                val mifareTag = MifareClassic.get(tag)
                tagIDSize = mifareTag.size.toString()
                var type = "Unknown"
                when (mifareTag.type) {
                    MifareClassic.TYPE_CLASSIC -> type = "Classic"
                    MifareClassic.TYPE_PLUS -> type = "Plus"
                    MifareClassic.TYPE_PRO -> type = "Pro"
                }
                sb.append("Mifare Classic type: ")
                sb.append(type)
                sb.append('\n')
                sb.append("Mifare size: ")
                sb.append("${mifareTag.size} bytes")
                sb.append('\n')
                sb.append("Mifare sectors: ")
                sb.append(mifareTag.sectorCount)
                sb.append('\n')
                sb.append("Mifare blocks: ")
                sb.append(mifareTag.blockCount)
            }
            if (tech == MifareUltralight::class.java.name) {
                sb.append('\n')
                val mifareUlTag: MifareUltralight = MifareUltralight.get(tag)
                var type = "Unknown"
                when (mifareUlTag.type) {
                    MifareUltralight.TYPE_ULTRALIGHT -> type = "Ultralight"
                    MifareUltralight.TYPE_ULTRALIGHT_C -> type = "Ultralight C"
                }
                sb.append("Mifare Ultralight type: ")
                sb.append(type)
            }
        }
        msg = sb.toString()
        nfcBack.onSuc(NFCBean(tagIDHex,tagIDDec,tagIDSize,msg))
        return sb.toString()
    }

    private fun getHex(bytes: ByteArray): String? {
        val sb = StringBuilder()
        for (i in bytes.indices.reversed()) {
            val b = bytes[i].toInt() and 0xff
            if (b < 0x10) sb.append('0')
            sb.append(Integer.toHexString(b))
            if (i > 0) {
                sb.append(" ")
            }
        }
        return sb.toString()
    }

    private fun getDec(bytes: ByteArray): Long {
        var result: Long = 0
        var factor: Long = 1
        for (i in bytes.indices) {
            val value = bytes[i].toLong() and 0xffL
            result += value * factor
            factor *= 256L
        }
        return result
    }

    private fun getReversed(bytes: ByteArray): Long {
        var result: Long = 0
        var factor: Long = 1
        for (i in bytes.indices.reversed()) {
            val value = bytes[i].toLong() and 0xffL
            result += value * factor
            factor *= 256L
        }
        return result
    }

    /**
     * 跳转到NFC设置界面*/
    fun jumpNFC(){
        val setNfc = Intent(Settings.ACTION_NFC_SETTINGS)
        activity!!.startActivity(setNfc)
    }

    /**
     * 该设备是否支持NFC*/
    fun getNFC(context: Context): Boolean{
        val packageManager = context.packageManager
        return packageManager.hasSystemFeature(PackageManager.FEATURE_NFC)
    }
}
相关推荐
宾有为1 小时前
【Android】Proxyman 抓 HTTP 数据包
android·网络协议·http·抓包·proxyman
尹中文3 小时前
FileProvider高版本使用,跨进程传输文件
android
南宫真汀3 小时前
Android学生信息管理APP的设计与开发
android
牧杉-惊蛰4 小时前
表格的选择弹窗,选中后返显到表格中
android
CodeCraft Studio4 小时前
【实用教程】如何利用 JxBrowser 在 Kotlin 中实现屏幕共享
android·开发语言·kotlin
徐浪老师5 小时前
用 Android Studio 从零开发一个多功能计算器应用
android·ide·android studio
小黄人软件5 小时前
【android USB 串口通信助手】stm32 源码demo 单片机与手机通信 Android studio 20241118
android·stm32·单片机
梁辰兴5 小时前
移动应用开发:Android Studio实现简易注册页(数据存放以SharedPreferences形式)
android·ide·android studio·登录页·移动应用开发
未来之窗软件服务5 小时前
android studio无法下载,Could not GET xxx, Received status code 400
android·ide·android studio