From 20952db304d0f71e1ce25f3b82114bbadff1ad4a Mon Sep 17 00:00:00 2001
From: zuoxiao <470321431@qq.com>
Date: 星期五, 20 六月 2025 16:51:54 +0800
Subject: [PATCH] feat(generallibrary): 补充补卡功能
---
baselibrary/src/main/java/com/dayu/baselibrary/tools/nfc/NativeNfcReadHelper.java | 89 ++++++++++++++++++++++++++++++++------------
1 files changed, 64 insertions(+), 25 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 4c8aa09..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,10 +33,6 @@
private static NativeNfcReadHelper helper;
- public NativeNfcReadHelper(Intent intent, Activity activity) {
- }
-
-
@Override
public void setIntent(Intent intent) {
this.tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
@@ -49,10 +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);
}
/**
@@ -61,7 +64,7 @@
* @return
*/
@Override
- public BaseUserCardCard getUserCardData(BaseUserCardCard userCardCard) {
+ public BaseUserCardCard getUserCardData(int sectorIndex, BaseUserCardCard userCardCard) {
if (userCardCard != null) {
BaseUserCardCard userCard = null;
Map<String, List<byte[]>> map = new HashMap<>();
@@ -70,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++) {
//璇诲彇鏁版嵁
@@ -387,6 +395,9 @@
@Override
public String getCardNumber() {
+ if (tag == null) {
+ return "";
+ }
MifareClassic mfc = MifareClassic.get(tag);
if (null != mfc) {
try {
@@ -435,6 +446,9 @@
public String getCardNumberNoClose() {
+ if (tag == null) {
+ return "";
+ }
MifareClassic mfc = MifareClassic.get(tag);
if (null != mfc) {
try {
@@ -476,13 +490,19 @@
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) {
@@ -491,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) {
@@ -510,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();
@@ -530,6 +566,7 @@
return BaseCommon.CARD_TYPE_ERROR2;
}
} catch (IOException e) {
+ e.printStackTrace();
return BaseCommon.CARD_TYPE_ERROR1;
} finally {
try {
@@ -667,4 +704,6 @@
}
return null;
}
+
+
}
--
Gitblit v1.8.0