From 15a3fc02c120ff630a9721a2f48a82f76675213f Mon Sep 17 00:00:00 2001
From: zuojincheng <lf_zuo@163.com>
Date: 星期一, 07 四月 2025 09:04:20 +0800
Subject: [PATCH] feat(generallibrary): 添加新卡开卡界面并更新扫描图标

---
 baselibrary/src/main/java/com/dayu/baselibrary/tools/nfc/NativeNfcWriteHelper.java |  104 +++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 91 insertions(+), 13 deletions(-)

diff --git a/baselibrary/src/main/java/com/dayu/baselibrary/tools/nfc/NativeNfcWriteHelper.java b/baselibrary/src/main/java/com/dayu/baselibrary/tools/nfc/NativeNfcWriteHelper.java
index ef15209..c427898 100644
--- a/baselibrary/src/main/java/com/dayu/baselibrary/tools/nfc/NativeNfcWriteHelper.java
+++ b/baselibrary/src/main/java/com/dayu/baselibrary/tools/nfc/NativeNfcWriteHelper.java
@@ -24,6 +24,9 @@
     private static NativeNfcWriteHelper helper;
 
     public NativeNfcWriteHelper(Intent intent, Activity activity) {
+    }
+
+    public void setIntent(Intent intent) {
         this.tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
     }
 
@@ -37,6 +40,7 @@
         if (helper == null) {
             helper = new NativeNfcWriteHelper(intent, activity);
         }
+        helper.setIntent(intent);
         return helper;
     }
 
@@ -47,9 +51,10 @@
      * @param userCard 鐢ㄦ埛鍗″唴瀹�
      * @param
      */
-    public boolean writeUserData(BaseUserCardCard userCard) {
+    @Override
+    public boolean writeUserData(BaseUserCardCard userCard, int sector) {
         if (userCard != null) {
-            int a = 1;
+            int a = sector;
             try {
                 MifareClassic mfc = MifareClassic.get(tag);
                 if (null != mfc) {
@@ -113,48 +118,111 @@
      * @param b   涔﹀啓鐨勫潡(浠�0寮�濮嬫暟)
      * @param
      */
+    @Override
     public boolean writeData(byte[] str, int a, int b) {
+        return writeData(str, a, b, null);
+    }
+
+    @Override
+    public boolean writeData(byte[] str, int a, int b, NFCCallBack callBack) {
+        return writeData(str, a, b, true,null);
+    }
+
+    @Override
+    public boolean writeData(byte[] str, int a, int b, boolean isConnect, NFCCallBack callBack) {
         Log.i("NFCWreatActivity", "writeData: a=" + a + " b=" + b);
-        if (str.length <= 16) {
+        if (str.length == 16) {
             try {
                 MifareClassic mfc = MifareClassic.get(tag);
                 if (null != mfc) {
                     try {
                         //杩炴帴NFC
-                        mfc.connect();
+                        if (isConnect){
+                            mfc.connect();
+                        }
                         //鑾峰彇鎵囧尯鏁伴噺
                         int count = mfc.getSectorCount();
                         //濡傛灉浼犺繘鏉ョ殑鎵囧尯澶т簡鎴栬�呭皬浜嗙洿鎺ラ��鍑烘柟娉�
                         if (a > count - 1 || a < 0) {
+                            if (callBack != null) {
+                                callBack.isSusses(false, "鎵囧尯閿欒--" + a);
+                            }
                             return false;
                         }
                         //鑾峰彇鍐欑殑鎵囧尯鐨勫潡鐨勬暟閲�
                         int bCount = mfc.getBlockCountInSector(a);
                         //濡傛灉杈撳叆鐨勫潡澶т簡鎴栬�呭皬浜嗕篃鏄洿鎺ラ��鍑�
                         if (b > bCount - 1 || b < 0) {
+                            if (callBack != null) {
+                                callBack.isSusses(false, "鍧楀尯閿欒--" + b);
+                            }
                             return false;
                         }
                         //楠岃瘉鎵囧尯瀵嗙爜
                         boolean isOpen = false;
-                        for (int i = 0; i < listKeyA.size(); i++) {
-                            if (mfc.authenticateSectorWithKeyA(0, listKeyA.get(i))) {
-                                isOpen = true;
-                                if (listKeyA.get(i).equals(defauleKey)) {
-                                    //褰撳墠涓洪粯璁ょ櫧鍗″瘑鐮佹椂鍐欏崱鏃朵慨鏀瑰瘑鐮�
-                                    changePasword(a, mfc);
+                        if (listKeyA.size() != 0) {
+                            for (int i = 0; i < listKeyA.size(); i++) {
+                                if (mfc.authenticateSectorWithKeyA(0, listKeyA.get(i))) {
+                                    isOpen = true;
+                                    if (listKeyA.get(i).equals(defauleKey)) {
+                                        //褰撳墠涓洪粯璁ょ櫧鍗″瘑鐮佹椂鍐欏崱鏃朵慨鏀瑰瘑鐮�
+                                        changePasword(a, mfc);
+                                    }
+                                    break;
                                 }
-                                break;
+                            }
+                        } else if (listA_PS.size() != 0 && listA_PS.size() > a) {
+                            if (mfc.authenticateSectorWithKeyA(a, defauleKey)){
+                                changePasword(a, mfc);
+                                isOpen = true;
+
+                            }else if (mfc.authenticateSectorWithKeyA(a, listA_PS.get(a))) {
+                                isOpen = true;
                             }
                         }
                         if (isOpen) {
                             int bIndex = mfc.sectorToBlock(a);
                             //鍐欏崱
                             mfc.writeBlock(bIndex + b, str);
-                            return true;
+                            // 鏍¢獙鍐欏叆鏁版嵁鏄惁姝g‘
+                            boolean isVerified = true;
+                            // 璇诲彇鏁版嵁楠岃瘉
+                            byte[] readResult = mfc.readBlock(bIndex + b);
+                            if (readResult == null) {
+                                if (callBack != null) {
+                                    callBack.isSusses(false, a + "鎵囧尯鍐欏崱鏃跺啀娆¤鍙栭獙璇佹椂澶辫触");
+                                }
+                                return false;
+                            }
+                            for (int i = 0; i < str.length; i++) {
+                                if (str[i] != readResult[i]) {
+                                    isVerified = false;
+                                    break;
+                                }
+                            }
+                            if (isVerified) {
+                                if (callBack != null) {
+                                    callBack.isSusses(true, "鍐欏叆鎴愬姛骞堕獙璇侀�氳繃");
+                                }
+                                return true;
+
+                            } else {
+                                if (callBack != null) {
+                                    callBack.isSusses(false, "鍐欏叆楠岃瘉澶辫触");
+                                }
+                                return false;
+                            }
+                        }
+                        if (callBack != null) {
+                            callBack.isSusses(false, a + "鎵囧尯瀵嗙爜閿欒");
                         }
                         return false;
                     } catch (Exception e) {
                         e.printStackTrace();
+                        if (callBack != null) {
+                            callBack.isSusses(false, a + "鎵囧尯鍐欏崱鎶ラ敊" + e.getMessage());
+
+                        }
                         return false;
                     } finally {
                         try {
@@ -166,7 +234,14 @@
                 }
             } catch (Exception e) {
                 e.printStackTrace();
+                if (callBack != null) {
+                    callBack.isSusses(false, a + "鎵囧尯鍐欏崱鎶ラ敊" + e.getMessage());
+                }
                 return false;
+            }
+        } else {
+            if (callBack != null) {
+                callBack.isSusses(false, a + "鎵囧尯鍐欏崱鎶ラ敊锛宐yte鏁扮粍澶у皬涓嶄负16");
             }
         }
         return false;
@@ -179,12 +254,12 @@
      * @param a 涔﹀啓鐨勬墖鍖�
      *          //     * @param callback 杩斿洖鐩戝惉
      */
+    @Override
     public boolean changePasword(int a, MifareClassic mfc) {
 
         byte[] data = new byte[16];
         if (null != mfc) {
             try {
-
                 //灏嗗瘑鐮佽浆鎹负keyA
                 byte[] dataA = HexUtil.hexToByteArray(companyKeyA);
                 for (int i = 0; i < dataA.length; i++) {
@@ -218,6 +293,7 @@
      *
      * @return
      */
+    @Override
     public boolean initCard() {
         try {
             MifareClassic mfc = MifareClassic.get(tag);
@@ -278,4 +354,6 @@
         }
         return false;
     }
+
+
 }

--
Gitblit v1.8.0