由于朋侪必要个读取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 asis: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. 启动页面代码
package com.wkq.nfcimport android.content.Intentimport android.nfc.NdefMessageimport android.nfc.NdefRecord.createMimeimport android.nfc.NfcAdapterimport android.nfc.NfcEventimport android.nfc.Tagimport android.os.Bundleimport android.widget.Toastimport androidx.appcompat.app.AppCompatActivityimport androidx.databinding.DataBindingUtilimport 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读取卡片数据的操纵,具体的数据处理处罚只是简朴的处理处罚了北京公交卡的数据,具体项目业务上必要读取什么卡数据必要项目中具体行止理处罚.
看都看了,点个赞呗 |