<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <LinearLayout android:id="@+id/layout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/tv1" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/et1" android:text="N"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="车号:"/> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/carCode"/> </LinearLayout> </LinearLayout> </android.support.constraint.ConstraintLayout>
package com.example.meng.myapplication; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.graphics.Color; import android.nfc.NfcAdapter; import android.nfc.Tag; import android.nfc.tech.IsoDep; import android.nfc.tech.MifareClassic; import android.nfc.tech.NfcA; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private IntentFilter mIntentFilter[]; private Intent mIntent; private PendingIntent mPendingIntent; private NfcAdapter mNfcAdapter; private String[][] mTechLists; private EditText mSector;//扇区 private EditText mCarCode;//车号
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView tv = findViewById(R.id.tv1); mSector = findViewById(R.id.et1); mNfcAdapter = NfcAdapter.getDefaultAdapter(this); if (mNfcAdapter == null) { tv.setText("当前手机不支持NFC"); return; } else if (!mNfcAdapter.isEnabled()) { tv.setText("手机支持NFC,请启动NFC功能"); return; } else if (mNfcAdapter.isEnabled()) { tv.setText("已经开启NFC功能"); } else { tv.setText("未知问题"); return; } mIntent = new Intent(this, MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); mPendingIntent = PendingIntent.getActivity(this, 0, mIntent, PendingIntent.FLAG_UPDATE_CURRENT); try { mIntentFilter = new IntentFilter[]{new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED, "*/*")}; } catch (Exception e) { e.printStackTrace(); } mTechLists = new String[][]{new String[]{MifareClassic.class.getName()}, {IsoDep.class.getName()},{NfcA.class.getName()}}; } @Override public void onNewIntent(Intent intent) { int i=Integer.parseInt(mSector.getText().toString()); Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); MifareClassic mifareClassic = MifareClassic.get(tag); // byte[] ids = tag.getId(); // String str= bytes2HexString(ids); String str2 = ""; //密码 //byte[] bytes ="ffffffffffff".getBytes();//不好用 //byte[] bytes={(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff,(byte)0xff};// //Toast.makeText(this, "OK", Toast.LENGTH_SHORT).show(); try { mifareClassic.connect(); //获取扇区数 //str2 = mifareClassic.getSectorCount() + ""; //获取总块数 //str2 = mifareClassic.getBlockCount() + ""; //验证密码 boolean isOpen = mifareClassic.authenticateSectorWithKeyA(i, bytes);//MifareClassic.KEY_DEFAULT if (isOpen) { //String carCode = new String(mifareClassic.readBlock(n),"GB2312").replace("*","");// Toast.makeText(this, carCode, Toast.LENGTH_LONG).show(); return; }else { Toast.makeText(this, "密码错误", Toast.LENGTH_LONG).show(); return; } } catch (Exception e) { e.printStackTrace(); str2 = e.getMessage(); Toast.makeText(this, str2, Toast.LENGTH_LONG).show(); return; } finally { try { mifareClassic.close(); } catch (Exception e) { e.printStackTrace(); str2 = e.getMessage(); Toast.makeText(this, str2, Toast.LENGTH_LONG).show(); return; } } //Toast.makeText(this, str2, Toast.LENGTH_LONG).show(); } // @Override public void onResume() { super.onResume(); //设置处理优于所有其他NFC的处理 if (mNfcAdapter != null) mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, mIntentFilter, mTechLists); } //暂停Activity,界面获取焦点 @Override public void onPause() { super.onPause(); // if (mNfcAdapter != null) mNfcAdapter.disableForegroundDispatch(this); } }