
AndroID实现读取NFC卡卡号示例,具体如下:
1.权限
<uses-permission androID:name="androID.permission.NFC" /> <uses-feature androID:name="androID.harDWare.nfc" androID:required="true" />
2.注册(静态)
<intent-filter> <action androID:name="androID.nfc.action.TAG_disCOVERED" /> <data androID:mimeType="text/plain" /> </intent-filter>
3.Activity
初始化
//初始化NfcAdapter mNfcAdapter = NfcAdapter.getDefaultAdapter(this); // 初始化PendingIntent,当有NFC设备连接上的时候,就交给当前Activity处理 pi = PendingIntent.getActivity(this,new Intent(this,getClass()) .addFlags(Intent.FLAG_ACTIVITY_SINGLE_top),0);
启动
@OverrIDe protected voID onResume() { super.onResume(); mNfcAdapter.enableForegrounddispatch(this,pi,null,null); //启动 }获取数据
@OverrIDe protected voID onNewIntent(Intent intent) { super.onNewIntent(intent); // 当前app正在前端界面运行,这个时候有intent发送过来,那么系统就会调用onNewIntent回调方法,将intent传送过来 // 我们只需要在这里检验这个intent是否是NFC相关的intent,如果是,就调用处理方法 if (NfcAdapter.ACTION_TAG_disCOVERED.equals(intent.getAction())) { processIntent(intent); } }解析
/** * Parses the NDEF Message from the intent and prints to the TextVIEw */ private voID processIntent(Intent intent) { //取出封装在intent中的TAG Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); String CardID =ByteArrayToHexString(tagFromIntent.getID()); }private String ByteArrayToHexString(byte[] inarray) { int i,j,in; String[] hex = { "0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F" }; String out = ""; for (j = 0; j < inarray.length; ++j) { in = (int) inarray[j] & 0xff; i = (in >> 4) & 0x0f; out += hex[i]; i = in & 0x0f; out += hex[i]; } return out; }4.完整参考
<?xml version="1.0" enCoding="utf-8"?><manifest xmlns:androID="http://schemas.androID.com/apk/res/androID" package="cn.com.Jslh.zjcdprogrect"> <uses-permission androID:name="androID.permission.NFC" /> <uses-permission androID:name="androID.permission.INTERNET" /> <uses-feature androID:name="androID.harDWare.nfc" androID:required="true" /> <application androID:name=".common.MyApplication" androID:allowBackup="true" androID:icon="@mipmap/ic_launcher" androID:label="@string/app_name" androID:roundIcon="@mipmap/ic_launcher_round" androID:supportsRtl="true" androID:theme="@style/Apptheme"> <activity androID:name=".LoginActivity"> <intent-filter> <action androID:name="androID.intent.action.MAIN" /> <category androID:name="androID.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity androID:name=".saoka.WorkActivity"> <intent-filter> <action androID:name="androID.nfc.action.TAG_disCOVERED" /> <data androID:mimeType="text/plain" /> </intent-filter> <!--<Meta-data androID:name="androID.nfc.action.TECH_disCOVERED" androID:resource="@xml/nfc_tech_filter" />--> </activity> </application></manifest>
package cn.com.Jslh.zjcdprogrect.saoka;import androID.app.PendingIntent;import androID.content.Context;import androID.content.Intent;import androID.content.IntentFilter;import androID.nfc.NfcAdapter;import androID.nfc.Tag;import androID.os.Bundle;import androID.support.v7.app.AppCompatActivity;import cn.com.Jslh.zjcdprogrect.R;public class WorkActivity extends AppCompatActivity { private NfcAdapter mNfcAdapter; private PendingIntent pi; private IntentFilter tagDetected; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_work); //初始化NfcAdapter mNfcAdapter = NfcAdapter.getDefaultAdapter(this); //初始化PendingIntent // 初始化PendingIntent,当有NFC设备连接上的时候,就交给当前Activity处理 pi = PendingIntent.getActivity(this,0); // 新建IntentFilter,使用的是第二种的过滤机制// tagDetected = new IntentFilter(NfcAdapter.ACTION_TECH_disCOVERED);// tagDetected.addcategory(Intent.category_DEFAulT); } @OverrIDe protected voID onNewIntent(Intent intent) { super.onNewIntent(intent); // 当前app正在前端界面运行,这个时候有intent发送过来,那么系统就会调用onNewIntent回调方法,将intent传送过来 // 我们只需要在这里检验这个intent是否是NFC相关的intent,如果是,就调用处理方法 if (NfcAdapter.ACTION_TAG_disCOVERED.equals(intent.getAction())) { processIntent(intent); } } @OverrIDe protected voID onResume() { super.onResume(); mNfcAdapter.enableForegrounddispatch(this,null); } /** * Parses the NDEF Message from the intent and prints to the TextVIEw */ private voID processIntent(Intent intent) { //取出封装在intent中的TAG Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); String CardID =ByteArrayToHexString(tagFromIntent.getID()); } public static voID startActivity(Context context){ Intent intent = new Intent(); intent.setClass(context,WorkActivity.class); context.startActivity(intent); } private String ByteArrayToHexString(byte[] inarray) { int i,"F" }; String out = ""; for (j = 0; j < inarray.length; ++j) { in = (int) inarray[j] & 0xff; i = (in >> 4) & 0x0f; out += hex[i]; i = in & 0x0f; out += hex[i]; } return out; }}以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android实现读取NFC卡卡号示例全部内容,希望文章能够帮你解决Android实现读取NFC卡卡号示例所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)