Android实现读取NFC卡卡号示例
Android实现读取NFC卡卡号示例,具体如下:
1.权限
<uses-permissionandroid:name="android.permission.NFC"/> <uses-feature android:name="android.hardware.nfc" android:required="true"/>
2.注册(静态)
<intent-filter> <actionandroid:name="android.nfc.action.TAG_DISCOVERED"/> <dataandroid:mimeType="text/plain"/> </intent-filter>
3.Activity
初始化
//初始化NfcAdapter mNfcAdapter=NfcAdapter.getDefaultAdapter(this); //初始化PendingIntent,当有NFC设备连接上的时候,就交给当前Activity处理 pi=PendingIntent.getActivity(this,0,newIntent(this,getClass()) .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),0);
启动
@Override protectedvoidonResume(){ super.onResume(); mNfcAdapter.enableForegroundDispatch(this,pi,null,null);//启动 }
获取数据
@Override protectedvoidonNewIntent(Intentintent){ super.onNewIntent(intent); //当前app正在前端界面运行,这个时候有intent发送过来,那么系统就会调用onNewIntent回调方法,将intent传送过来 //我们只需要在这里检验这个intent是否是NFC相关的intent,如果是,就调用处理方法 if(NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())){ processIntent(intent); } }
解析
/** *ParsestheNDEFMessagefromtheintentandprintstotheTextView */ privatevoidprocessIntent(Intentintent){ //取出封装在intent中的TAG TagtagFromIntent=intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); StringCardId=ByteArrayToHexString(tagFromIntent.getId()); } privateStringByteArrayToHexString(byte[]inarray){ inti,j,in; String[]hex={"0","1","2","3","4","5","6","7","8","9","A", "B","C","D","E","F"}; Stringout=""; 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]; } returnout; }
4.完整参考
<?xmlversion="1.0"encoding="utf-8"?> <manifestxmlns:android="http://schemas.android.com/apk/res/android" package="cn.com.jslh.zjcdprogrect"> <uses-permissionandroid:name="android.permission.NFC"/> <uses-permissionandroid: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"> <activityandroid:name=".LoginActivity"> <intent-filter> <actionandroid:name="android.intent.action.MAIN"/> <categoryandroid:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <activityandroid:name=".saoka.WorkActivity"> <intent-filter> <actionandroid:name="android.nfc.action.TAG_DISCOVERED"/> <dataandroid:mimeType="text/plain"/> </intent-filter> <!--<meta-dataandroid:name="android.nfc.action.TECH_DISCOVERED"android:resource="@xml/nfc_tech_filter"/>--> </activity> </application> </manifest>
packagecn.com.jslh.zjcdprogrect.saoka; importandroid.app.PendingIntent; importandroid.content.Context; importandroid.content.Intent; importandroid.content.IntentFilter; importandroid.nfc.NfcAdapter; importandroid.nfc.Tag; importandroid.os.Bundle; importandroid.support.v7.app.AppCompatActivity; importcn.com.jslh.zjcdprogrect.R; publicclassWorkActivityextendsAppCompatActivity{ privateNfcAdaptermNfcAdapter; privatePendingIntentpi; privateIntentFiltertagDetected; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_work); //初始化NfcAdapter mNfcAdapter=NfcAdapter.getDefaultAdapter(this); //初始化PendingIntent //初始化PendingIntent,当有NFC设备连接上的时候,就交给当前Activity处理 pi=PendingIntent.getActivity(this,0,newIntent(this,getClass()) .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),0); //新建IntentFilter,使用的是第二种的过滤机制 //tagDetected=newIntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED); //tagDetected.addCategory(Intent.CATEGORY_DEFAULT); } @Override protectedvoidonNewIntent(Intentintent){ super.onNewIntent(intent); //当前app正在前端界面运行,这个时候有intent发送过来,那么系统就会调用onNewIntent回调方法,将intent传送过来 //我们只需要在这里检验这个intent是否是NFC相关的intent,如果是,就调用处理方法 if(NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())){ processIntent(intent); } } @Override protectedvoidonResume(){ super.onResume(); mNfcAdapter.enableForegroundDispatch(this,pi,null,null); } /** *ParsestheNDEFMessagefromtheintentandprintstotheTextView */ privatevoidprocessIntent(Intentintent){ //取出封装在intent中的TAG TagtagFromIntent=intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); StringCardId=ByteArrayToHexString(tagFromIntent.getId()); } publicstaticvoidstartActivity(Contextcontext){ Intentintent=newIntent(); intent.setClass(context,WorkActivity.class); context.startActivity(intent); } privateStringByteArrayToHexString(byte[]inarray){ inti,j,in; String[]hex={"0","1","2","3","4","5","6","7","8","9","A", "B","C","D","E","F"}; Stringout=""; 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]; } returnout; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。