昨天同事找我帮忙看看 rfid 开发的时候,一扫描就直接弹出activity的问题。
网上也有蛮多例子,但是能直接拿来参考写实际项目的没几个,系统提供的demo呢,一扫描标签就弹出activity,也是开发中不想要的。于是进入苦逼模式,进过分析和翻资料、百度、终于解决了。下面是代码:
先看项目图片, 我是直接用demo修改的:
1、清单文件:
需求添加权限啦什么,自己看啊,
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.nl.nfcdemo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="15" /> <!-- 添加nfc权限 --> <uses-permission android:name="android.permission.NFC" /> <uses-feature android:name="android.hardware.nfc" android:required="true" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.nl.nfcdemo.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
2、MainAcivity类。
package com.nl.nfcdemo; import android.app.Activity; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.IntentFilter.MalformedMimeTypeException; import android.nfc.NfcAdapter; import android.nfc.Tag; import android.nfc.tech.MifareClassic; import android.nfc.tech.NfcV; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private final String ACTION_NAME = "android.nfc.action.TECH_DISCOVERED"; NfcAdapter nfcAdapter; TextView promt; private PendingIntent mPendingIntent; private IntentFilter [] mIntentFilters; private String[][] mTechLists; @Override public void onCreate(Bundle savedInstanceState){ super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); promt = (TextView) findViewById (R.id.promt); // 获取默认的NFC控制器 nfcAdapter = NfcAdapter.getDefaultAdapter (this); if (nfcAdapter == null) { promt.setText ("设备不支持NFC!"); finish (); return; } if (!nfcAdapter.isEnabled ()) { promt.setText ("请在系统设置中先启用NFC功能!"); finish (); return; } initNFC(); } /** *@Description: 初始化 *@Author:杨攀 *@Since: 2016年4月27日下午5:38:15 */ private void initNFC(){ //绑定Intent Intent mIntent = new Intent (ACTION_NAME); mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); IntentFilter techFilter = new IntentFilter (nfcAdapter.ACTION_TECH_DISCOVERED); try { techFilter.addDataType ("text/plain"); mIntentFilters = new IntentFilter[]{techFilter}; } catch (MalformedMimeTypeException e) { e.printStackTrace(); } mTechLists = new String[][] { new String[] {"android.nfc.tech.NfcA"}, new String[]{"android.nfc.tech.NfcB"}, new String[]{"android.nfc.tech.NfcF"}, new String[]{"android.nfc.tech.NfcV"}, new String[]{"android.nfc.tech.Ndef"}, new String[]{"android.nfc.tech.NdefFormatable"}, new String[]{"android.nfc.tech.IsoDep"}, new String[]{"android.nfc.tech.MifareClassic"}, new String[]{"android.nfc.tech.MifareUltralight"} }; } /** *@Description: 拦截系统的 intent, 然后触发读写操作 *@Author:杨攀 *@Since: 2016年4月27日下午5:08:20 *@param intent */ @Override protected void onNewIntent(Intent intent){ super.onNewIntent (intent); // 得到是否检测到ACTION_TECH_DISCOVERED触发 if (NfcAdapter.ACTION_TECH_DISCOVERED.equals (intent.getAction ())) { // 处理该intent processIntent (intent); } } /** *@Description: 注册 意图 *@Author:杨攀 *@Since: 2016年4月27日下午5:34:21 */ @Override protected void onResume(){ super.onResume (); nfcAdapter.enableForegroundDispatch (this, mPendingIntent, mIntentFilters, mTechLists); } /** *@Description: 窗口关闭的时候,注销意图 *@Author:杨攀 *@Since: 2016年4月27日下午5:34:35 */ @Override protected void onPause(){ super.onPause (); //注销注册 nfcAdapter.disableForegroundDispatch(this); } @Override protected void onDestroy(){ super.onDestroy (); } // 字符序列转换为16进制字符串 private String bytesToHexString(byte[] src){ StringBuilder stringBuilder = new StringBuilder ("0x"); if (src == null || src.length <= 0) { return null; } char[] buffer = new char[2]; for ( int i = 0 ; i < src.length ; i++ ) { buffer[0] = Character.forDigit ((src[i] >>> 4) & 0x0F, 16); buffer[1] = Character.forDigit (src[i] & 0x0F, 16); System.out.println (buffer); stringBuilder.append (buffer); } return stringBuilder.toString (); } /** * 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); for ( String tech : tagFromIntent.getTechList () ) { System.out.println (tech); } boolean auth = false; // 读取TAG MifareClassic mfc = MifareClassic.get (tagFromIntent); String metaInfo = "本标签的UID为" + Coverter.getUid (intent) + "\n"; if (mfc != null) { try { // Enable I/O operations to the tag from this TagTechnology // object. mfc.connect (); int type = mfc.getType ();// 获取TAG的类型 int sectorCount = mfc.getSectorCount ();// 获取TAG中包含的扇区数 String typeS = ""; switch (type) { case MifareClassic.TYPE_CLASSIC: typeS = "TYPE_CLASSIC"; break; case MifareClassic.TYPE_PLUS: typeS = "TYPE_PLUS"; break; case MifareClassic.TYPE_PRO: typeS = "TYPE_PRO"; break; case MifareClassic.TYPE_UNKNOWN: typeS = "TYPE_UNKNOWN"; break; } metaInfo += "卡片类型:" + typeS + "\n共" + sectorCount + "个扇区\n共" + mfc.getBlockCount () + "个块\n存储空间: " + mfc.getSize () + "B\n"; for ( int j = 0 ; j < sectorCount ; j++ ) { // Authenticate a sector with key A. auth = mfc.authenticateSectorWithKeyA (j, MifareClassic.KEY_DEFAULT); int bCount; int bIndex; if (auth) { metaInfo += "Sector " + j + ":验证成功\n"; // 读取扇区中的块 bCount = mfc.getBlockCountInSector (j); bIndex = mfc.sectorToBlock (j); for ( int i = 0 ; i < bCount ; i++ ) { byte[] data = mfc.readBlock (bIndex); metaInfo += "Block " + bIndex + " : " + bytesToHexString (data) + "\n"; bIndex++; } } else { metaInfo += "Sector " + j + ":验证失败\n"; } } } catch (Exception e) { e.printStackTrace (); } } promt.setText (metaInfo); } }
经过运行,可以。我在上传 项目的压缩包。需要的自行下载。
相关推荐
android系统开发RFID读写功能的SDK
android系统开发RFID读写功能的SDK,绝对可用,一般比较难找
诺塔斯智能科技为非接触式IC卡读写器二次开发SDK提供了丰富的版本、针对不同的使用环境,我们提供了C#、C++、JAVA、dephi、Android、JS、Liunx、VC等多个版本,可以根据自己需要有针对性下载。
一款RFID读卡器Android app,可以用于Android学习
基于4412的RFID开发源码,基于android开发,希望对大家有帮助
RFID开发模块Demo,实现了简单的读取数据的功能,能够满足正常读取RFID数据,暂未实现写入功能
Android手持机PDA开发、PSAM验证、IC卡NFC读取(CPU、M1等)、RFID读取、包含说明文档及使用文档
该系统最初由安迪·鲁宾开发,后被Google公司收购并注资,随后与多家硬件制造商、软件开发商及电信营运商共同研发改良。 Android操作系统的特点包括: 开放源代码:Android系统采用开放源代码模式,允许开发者自由...
嵌入式开发rk3066/rk3188 rfid rc522芯片模块驱动及测试例子,工具
嵌入式 Android开发 智能家居 RFID 空调 灯光 门
基于Android系统的RFID手持终端低功耗设计.pdf
优博讯PDA开发包,带DEMO。
为了更好地满足开发者的需求,东集公司提供了丰富的Android开发SDK及示例,帮助开发者快速集成和定制设备功能。 该SDK包含了丰富的API接口和文档,使得开发者能够轻松调用设备的各项功能,如条码扫描、RFID读写、...
Tq210搭载Android4.2系统构建之RFID.pdf
最专业的条码、RFID设备及软件定制开发商。很多成功案例可参考! 玖锐技术提供产品及经营范围(很高的性价比),和ERP软件是互补关系,缺一不可。 如果您是有知道此方面的需求信息,只需一个电话给我们,互惠互利,...