From 2b02b6e854a56a511588e4865ddf2c6597675329 Mon Sep 17 00:00:00 2001 From: zuoxiao <470321431@qq.com> Date: 星期一, 16 六月 2025 16:04:54 +0800 Subject: [PATCH] feat(nfc): 添加读卡功能并优化写卡流程 --- baselibrary/src/main/java/com/dayu/baselibrary/tools/nfc/NativeNfcReadHelper.java | 145 +++++++++++++++++++++++++++++++++++++++--------- 1 files changed, 118 insertions(+), 27 deletions(-) diff --git a/baselibrary/src/main/java/com/dayu/baselibrary/tools/nfc/NativeNfcReadHelper.java b/baselibrary/src/main/java/com/dayu/baselibrary/tools/nfc/NativeNfcReadHelper.java index c7a5200..93ac8c3 100644 --- a/baselibrary/src/main/java/com/dayu/baselibrary/tools/nfc/NativeNfcReadHelper.java +++ b/baselibrary/src/main/java/com/dayu/baselibrary/tools/nfc/NativeNfcReadHelper.java @@ -7,6 +7,8 @@ import android.nfc.tech.MifareClassic; import android.util.Log; +import androidx.annotation.NonNull; + import com.dayu.baselibrary.bean.BaseManagerToUserCard; import com.dayu.baselibrary.bean.BaseUserCardCard; import com.dayu.baselibrary.tools.HexUtil; @@ -31,9 +33,11 @@ private static NativeNfcReadHelper helper; - public NativeNfcReadHelper(Intent intent, Activity activity) { + @Override + public void setIntent(Intent intent) { this.tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); } + /** * 鍗曚緥鍒濆鍖� @@ -43,9 +47,15 @@ */ public static NativeNfcReadHelper getInstence(Intent intent, Activity activity) { if (helper == null) { - helper = new NativeNfcReadHelper(intent, activity); + helper = new NativeNfcReadHelper(); } + helper.setIntent(intent); return helper; + } + + + public BaseUserCardCard getUserCardData( BaseUserCardCard userCardCard) { + return getUserCardData(1, userCardCard); } /** @@ -54,8 +64,8 @@ * @return */ @Override - public BaseUserCardCard getUserCardData(BaseUserCardCard userCardCard) { - if (userCardCard!=null){ + public BaseUserCardCard getUserCardData(int sectorIndex, BaseUserCardCard userCardCard) { + if (userCardCard != null) { BaseUserCardCard userCard = null; Map<String, List<byte[]>> map = new HashMap<>(); MifareClassic mfc = MifareClassic.get(tag); @@ -63,26 +73,31 @@ try { //閾炬帴NFC mfc.connect(); - //鑾峰彇鎵囧尯鏁伴噺 - int count = mfc.getSectorCount(); - //瀛樺偍绌洪棿 - int size = mfc.getSize(); + //鐢ㄤ簬鍒ゆ柇鏃跺�欐湁鍐呭璇诲彇鍑烘潵 boolean flag = false; List<byte[]> list = new ArrayList<>(); //楠岃瘉鎵囧尯瀵嗙爜锛屽惁鍒欎細鎶ラ敊锛堥摼鎺ュけ璐ラ敊璇級 boolean isOpen = false; - for (int i = 0; i < listKeyA.size(); i++) { - if (mfc.authenticateSectorWithKeyA(1, listKeyA.get(i))) { + if (!listKeyA.isEmpty()) { + for (int i = 0; i < listKeyA.size(); i++) { + if (mfc.authenticateSectorWithKeyA(sectorIndex, listKeyA.get(i))) { + isOpen = true; + break; + } + } + } else if (!listA_PS.isEmpty()) { + if (mfc.authenticateSectorWithKeyA(sectorIndex, defauleKey)) { isOpen = true; - break; + } else if (mfc.authenticateSectorWithKeyA(sectorIndex, listA_PS.get(sectorIndex))) { + isOpen = true; } } if (isOpen) { //鑾峰彇鎵囧尯閲岄潰鍧楃殑鏁伴噺 - int bCount = mfc.getBlockCountInSector(1); + int bCount = mfc.getBlockCountInSector(sectorIndex); //鑾峰彇鎵囧尯绗竴涓潡瀵瑰簲鑺墖瀛樺偍鍣ㄧ殑浣嶇疆锛堟垜鏄繖鏍风悊瑙g殑锛屽洜涓虹0鎵囧尯鐨勮繖涓�兼槸4鑰屼笉鏄�0锛� - int bIndex = mfc.sectorToBlock(1); + int bIndex = mfc.sectorToBlock(sectorIndex); //String data1 = ""; for (int j = 0; j < bCount; j++) { //璇诲彇鏁版嵁 @@ -118,9 +133,9 @@ return null; } + @Override public String getCradType() { - MifareClassic mfc = MifareClassic.get(tag); if (null != mfc) { try { @@ -380,21 +395,26 @@ @Override public String getCardNumber() { + if (tag == null) { + return ""; + } MifareClassic mfc = MifareClassic.get(tag); if (null != mfc) { try { mfc.connect(); //鑾峰彇褰撳墠鍗″彿 boolean isOpen = false; - if (!listKeyA.isEmpty()){ + if (!listKeyA.isEmpty()) { for (int i = 0; i < listKeyA.size(); i++) { if (mfc.authenticateSectorWithKeyA(0, listKeyA.get(i))) { isOpen = true; break; } } - }else if (!listA_PS.isEmpty()){ - if (mfc.authenticateSectorWithKeyA(0, listA_PS.get(0))){ + } else if (!listA_PS.isEmpty()) { + if (mfc.authenticateSectorWithKeyA(0, defauleKey)) { + isOpen = true; + } else if (mfc.authenticateSectorWithKeyA(0, listA_PS.get(0))) { isOpen = true; } } @@ -424,13 +444,65 @@ return ""; } + + public String getCardNumberNoClose() { + if (tag == null) { + return ""; + } + MifareClassic mfc = MifareClassic.get(tag); + if (null != mfc) { + try { + mfc.connect(); + //鑾峰彇褰撳墠鍗″彿 + boolean isOpen = false; + if (!listKeyA.isEmpty()) { + for (int i = 0; i < listKeyA.size(); i++) { + if (mfc.authenticateSectorWithKeyA(0, listKeyA.get(i))) { + isOpen = true; + break; + } + } + } else if (!listA_PS.isEmpty()) { + if (mfc.authenticateSectorWithKeyA(0, defauleKey)) { + isOpen = true; + } else if (mfc.authenticateSectorWithKeyA(0, listA_PS.get(0))) { + isOpen = true; + } + } + + if (isOpen) { + int bIndex = mfc.sectorToBlock(0); + byte[] data = mfc.readBlock(bIndex + 0); + if (data != null && data.length > 0) { + String hex = HexUtil.bytesToHex(Arrays.copyOfRange(data, 0, 4)); + hex = HexUtil.spaceHex(hex); + hex = HexUtil.HighLowHex(hex); + Log.i("NFCWreatActivity", "hex===" + hex); + return hex.toUpperCase(); + } + } + + } catch (IOException e) { + Log.i("NFCWreatActivity", e.toString()); + return BaseCommon.CARD_TYPE_ERROR1; + } + } + return ""; + } + + @Override + public String getCradTypeAndCardNumber() { + return getCradTypeAndCardNumber(1, 0, 0); + } + + /** * 鑾峰彇鍗$墖绫诲瀷鍜屽崱鍙� * * @return */ - @Override - public String getCradTypeAndCardNumber() { + + public String getCradTypeAndCardNumber(int sectorIndex, int blockIndex, int cardTypeIndex) { MifareClassic mfc = MifareClassic.get(tag); if (null != mfc) { @@ -439,10 +511,18 @@ StringBuilder strData = new StringBuilder(); //鑾峰彇褰撳墠鍗″彿 boolean isOpen = false; - for (int i = 0; i < listKeyA.size(); i++) { - if (mfc.authenticateSectorWithKeyA(0, listKeyA.get(i))) { + if (!listKeyA.isEmpty()) { + for (int i = 0; i < listKeyA.size(); i++) { + if (mfc.authenticateSectorWithKeyA(0, listKeyA.get(i))) { + isOpen = true; + break; + } + } + } else if (!listA_PS.isEmpty()) { + if (mfc.authenticateSectorWithKeyA(0, listA_PS.get(0))) { isOpen = true; - break; + } else if (mfc.authenticateSectorWithKeyA(0, defauleKey)) { + isOpen = true; } } if (isOpen) { @@ -458,17 +538,25 @@ } } //鑾峰彇鍗$墖绫诲瀷 - for (int i = 0; i < listKeyA.size(); i++) { - if (mfc.authenticateSectorWithKeyA(1, listKeyA.get(i))) { + if (!listKeyA.isEmpty()) { + for (int i = 0; i < listKeyA.size(); i++) { + if (mfc.authenticateSectorWithKeyA(sectorIndex, listKeyA.get(i))) { + isOpen = true; + break; + } + } + } else if (!listA_PS.isEmpty()) { + if (mfc.authenticateSectorWithKeyA(sectorIndex, listA_PS.get(sectorIndex))) { isOpen = true; - break; + } else if (mfc.authenticateSectorWithKeyA(sectorIndex, defauleKey)) { + isOpen = true; } } if (isOpen) { - int bIndex = mfc.sectorToBlock(1); + int bIndex = mfc.sectorToBlock(sectorIndex); byte[] data = mfc.readBlock(bIndex + 0); if (data != null && data.length > 0) { - String hex = HexUtil.byteToHex(data[0]); + String hex = HexUtil.byteToHex(data[cardTypeIndex]); strData.append(hex); Log.i("NFCWreatActivity", "hex===" + hex); return strData.toString().toUpperCase(); @@ -478,6 +566,7 @@ return BaseCommon.CARD_TYPE_ERROR2; } } catch (IOException e) { + e.printStackTrace(); return BaseCommon.CARD_TYPE_ERROR1; } finally { try { @@ -615,4 +704,6 @@ } return null; } + + } -- Gitblit v1.8.0