糖尿病康复,内容丰富有趣,生活中的好帮手!
糖尿病康复 > NFC读取数据

NFC读取数据

时间:2019-07-18 21:32:46

相关推荐

NFC读取数据

1、在配置文件中监听NFC动作,这种配置随时监听nfc动作,无论app是否在前台,类似微信支付宝那种

在AndroidManifest.xml中

<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" />

nfc_tech_filter.xml

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"><tech-list><tech>android.nfc.tech.NfcA</tech><tech>android.nfc.tech.NfcB</tech><tech>android.nfc.tech.NfcF</tech><tech>android.nfc.tech.NfcV</tech><tech>android.nfc.tech.IsoDep</tech><tech>android.nfc.tech.MifareClassic</tech><tech>android.nfc.tech.MifareUltralight</tech><tech>android.nfc.tech.NdefFormatable</tech><tech>android.nfc.tech.Ndef</tech><tech>android.nfc.tech.NfcBarcode</tech></tech-list></resources>

2、在页面中动态注册监听,只在本页中起效

private NfcAdapter nfcAdapter;@Overrideprotected void onResume() {super.onResume();nfcAdapter = NfcAdapter.getDefaultAdapter(this);PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);IntentFilter[] intentFilters = new IntentFilter[]{new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED)};String[][] techList = new String[][]{new String[]{IsoDep.class.getName()}, {NfcV.class.getName()}, {NfcF.class.getName()},};nfcAdapter.enableForegroundDispatch(this, pendingIntent, intentFilters, techList);}}@Overrideprotected void onPause() {super.onPause();if (nfcAdapter != null) {nfcAdapter.disableForegroundDispatch(this);}}

3、权限

<uses-permission android:name="android.permission.NFC" /><uses-feature android:name="android.hardware.nfc" android:required="true" />

4、在onNewIntent中处理数据

@Overrideprotected void onNewIntent(Intent intent) {super.onNewIntent(intent);if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())) {Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);if (tag != null) {//读取IsoDep标签IsoDep isoDep = IsoDep.get(tag);try {isoDep.connect();//根据指令获取指定数据byte[] ppseRes = isoDep.transceive(Iso7816.selectByName(DFN_PPSE));} catch (Exception e) {e.printStackTrace();} finally {try {isoDep.close();} catch (IOException e) {}}//读取MifareClassic标签MifareClassic mfc = MifareClassic.get(tag);try {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";}}Log.i("NFC", "MifareClassic数据:" + metaInfo);} catch(Exception e) {e.printStackTrace();} finally {try {mfc.close();} catch (IPException e) {}}}}}

如果觉得《NFC读取数据》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。