From feb7add1979c830e0d940286242a7c489f2f8f96 Mon Sep 17 00:00:00 2001 From: zuoxiao <470321431@qq.com> Date: 星期五, 27 九月 2024 09:23:34 +0800 Subject: [PATCH] 优化NFC模块架构,将一些方法抽调到父类中。 --- qihealonelibrary/src/main/java/com/dayu/qihealonelibrary/tools/NfcReadHelper.java | 454 +++---------------------------------------------------- 1 files changed, 31 insertions(+), 423 deletions(-) diff --git a/qihealonelibrary/src/main/java/com/dayu/qihealonelibrary/tools/NfcReadHelper.java b/qihealonelibrary/src/main/java/com/dayu/qihealonelibrary/tools/NfcReadHelper.java index dff0d49..f17e28b 100644 --- a/qihealonelibrary/src/main/java/com/dayu/qihealonelibrary/tools/NfcReadHelper.java +++ b/qihealonelibrary/src/main/java/com/dayu/qihealonelibrary/tools/NfcReadHelper.java @@ -2,23 +2,12 @@ import android.app.Activity; import android.content.Intent; -import android.nfc.NfcAdapter; -import android.nfc.Tag; -import android.nfc.tech.MifareClassic; -import android.util.Log; -import com.dayu.baselibrary.tools.HexUtil; -import com.dayu.baselibrary.utils.BaseCommon; +import com.dayu.baselibrary.tools.nfc.BaseNfcReadHelper; +import com.dayu.baselibrary.tools.nfc.NfcReadAdapter; import com.dayu.qihealonelibrary.card.UserCard; -import com.dayu.qihealonelibrary.utils.CardCommon; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; import java.util.List; -import java.util.Map; /** * @author zx @@ -26,16 +15,18 @@ * email 1058083107@qq.com * description nfc璇诲彇宸ュ叿绫� */ -public class NfcReadHelper extends BaseNFCHelper { +public class NfcReadHelper extends QHBaseNFCHelper { - private Tag tag; + // private NFCCallback callback; private static NfcReadHelper helper; + private NfcReadAdapter adapter; public NfcReadHelper(Intent intent, Activity activity) { super(activity); - this.tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); + adapter = new NfcReadAdapter(intent, activity); + } /** @@ -57,60 +48,10 @@ * @return */ public UserCard getUserCardData() { - UserCard userCard = null; - Map<String, List<byte[]>> map = new HashMap<>(); - MifareClassic mfc = MifareClassic.get(tag); - if (null != mfc) { - try { - //閾炬帴NFC - mfc.connect(); - //鑾峰彇鎵囧尯鏁伴噺 - int count = mfc.getSectorCount(); - //瀛樺偍绌洪棿 - int size = mfc.getSize(); - //鐢ㄤ簬鍒ゆ柇鏃跺�欐湁鍐呭璇诲彇鍑烘潵 - boolean flag = false; - List<byte[]> list = new ArrayList<>(); - //楠岃瘉鎵囧尯瀵嗙爜锛屽惁鍒欎細鎶ラ敊锛堥摼鎺ュけ璐ラ敊璇級 - boolean isOpen = mfc.authenticateSectorWithKeyA(1, defauleKey); - if (!isOpen) { - isOpen = mfc.authenticateSectorWithKeyA(1, companyKey); - } - if (isOpen) { - //鑾峰彇鎵囧尯閲岄潰鍧楃殑鏁伴噺 - int bCount = mfc.getBlockCountInSector(1); - //鑾峰彇鎵囧尯绗竴涓潡瀵瑰簲鑺墖瀛樺偍鍣ㄧ殑浣嶇疆锛堟垜鏄繖鏍风悊瑙g殑锛屽洜涓虹0鎵囧尯鐨勮繖涓�兼槸4鑰屼笉鏄�0锛� - int bIndex = mfc.sectorToBlock(1); - //String data1 = ""; - for (int j = 0; j < bCount; j++) { - //璇诲彇鏁版嵁 - byte[] data = null; - try { - data = mfc.readBlock(bIndex); - bIndex++; - list.add(data); - } catch (Exception e) { - e.printStackTrace(); - } - } - flag = true; - } - - if (flag) { - userCard = UserCard.getBean(list); - return userCard; - } - } catch (IOException e) { - e.printStackTrace(); - } catch (Exception e) { - e.printStackTrace(); - } finally { - try { - mfc.close(); - } catch (Exception e) { - e.printStackTrace(); - } - } + try { + return (UserCard) adapter.getUserCardData(new UserCard()); + } catch (Exception e) { + e.printStackTrace(); } return null; } @@ -121,34 +62,10 @@ * @return */ public String getCardNumber() { - MifareClassic mfc = MifareClassic.get(tag); - if (null != mfc) { - try { - mfc.connect(); - //鑾峰彇褰撳墠鍗″彿 - boolean isOpen = mfc.authenticateSectorWithKeyA(0, defauleKey); - if (!isOpen) { - isOpen = mfc.authenticateSectorWithKeyA(0, companyKey); - } - 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)); - Log.i("NFCWreatActivity", "hex===" + hex); - return hex; - } - } - - } catch (IOException e) { - return BaseCommon.CARD_TYPE_ERROR1; - } finally { - try { - mfc.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } + try { + return adapter.getCardNumber(); + } catch (Exception e) { + e.printStackTrace(); } return ""; } @@ -160,54 +77,10 @@ * @return */ public String getCradTypeAndCardNumber() { - - MifareClassic mfc = MifareClassic.get(tag); - if (null != mfc) { - try { - mfc.connect(); - StringBuilder strData = new StringBuilder(); - //鑾峰彇褰撳墠鍗″彿 - boolean isOpen = mfc.authenticateSectorWithKeyA(0, defauleKey); - if (!isOpen) { - isOpen = mfc.authenticateSectorWithKeyA(0, companyKey); - } - 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)); - strData.append(hex); - strData.append(","); - Log.i("NFCWreatActivity", "hex===" + hex); - } - } - //鑾峰彇鍗$墖绫诲瀷 - isOpen = mfc.authenticateSectorWithKeyA(1, defauleKey); - if (!isOpen) { - isOpen = mfc.authenticateSectorWithKeyA(1, companyKey); - } - if (isOpen) { - int bIndex = mfc.sectorToBlock(1); - byte[] data = mfc.readBlock(bIndex + 0); - if (data != null && data.length > 0) { - String hex = HexUtil.byteToHex(data[0]); - strData.append(hex); - Log.i("NFCWreatActivity", "hex===" + hex); - return strData.toString(); - } - } else { - Log.i("NFCWreatActivity", "isOpen===" + isOpen); - return BaseCommon.CARD_TYPE_ERROR2; - } - } catch (IOException e) { - return BaseCommon.CARD_TYPE_ERROR1; - } finally { - try { - mfc.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } + try { + return adapter.getCradTypeAndCardNumber(); + } catch (Exception e) { + e.printStackTrace(); } return ""; } @@ -217,189 +90,22 @@ * * @param callback */ - public void getAllData(final NFCCallMapback callback) { - Map<String, List<byte[]>> map = new HashMap<>(); - MifareClassic mfc = MifareClassic.get(tag); - if (null != mfc) { - try { - //閾炬帴NFC - mfc.connect(); - //鑾峰彇鎵囧尯鏁伴噺 - int count = mfc.getSectorCount(); - //瀛樺偍绌洪棿 - int size = mfc.getSize(); - //鐢ㄤ簬鍒ゆ柇鏃跺�欐湁鍐呭璇诲彇鍑烘潵 - boolean flag = false; - for (int i = 0; i < count; i++) { - List<byte[]> list = new ArrayList<>(); - //楠岃瘉鎵囧尯瀵嗙爜锛屽惁鍒欎細鎶ラ敊锛堥摼鎺ュけ璐ラ敊璇級 - boolean isOpen = mfc.authenticateSectorWithKeyA(i, defauleKey); - if (!isOpen) { - isOpen = mfc.authenticateSectorWithKeyA(i, companyKey); - } - if (isOpen) { - //鑾峰彇鎵囧尯閲岄潰鍧楃殑鏁伴噺 - int bCount = mfc.getBlockCountInSector(i); - //鑾峰彇鎵囧尯绗竴涓潡瀵瑰簲鑺墖瀛樺偍鍣ㄧ殑浣嶇疆锛堟垜鏄繖鏍风悊瑙g殑锛屽洜涓虹0鎵囧尯鐨勮繖涓�兼槸4鑰屼笉鏄�0锛� - int bIndex = mfc.sectorToBlock(i); - //String data1 = ""; - for (int j = 0; j < bCount; j++) { - //璇诲彇鏁版嵁 - byte[] data = null; - try { - data = mfc.readBlock(bIndex); - bIndex++; - list.add(data); - } catch (Exception e) { - e.printStackTrace(); - } - } - flag = true; - } - map.put(i + "", list); - } - if (flag) { - callback.callBack(map); - } else { - callback.error(CardCommon.ERROR); - } - } catch (IOException e) { - callback.error(CardCommon.ERROR_MOVE); - e.printStackTrace(); - } catch (Exception e) { - callback.error(CardCommon.ERROR); - e.printStackTrace(); - } finally { - try { - mfc.close(); - } catch (Exception e) { - e.printStackTrace(); - } - } + public void getAllData(final BaseNfcReadHelper.NFCCallMapback callback) { + try { + adapter.getAllData(callback); + } catch (Exception e) { + e.printStackTrace(); } } public List<byte[]> getOnesectorData() { - MifareClassic mfc = null; try { - mfc = MifareClassic.get(tag); + return adapter.getOnesectorData(); } catch (Exception e) { e.printStackTrace(); - } - List<byte[]> list = new ArrayList<>(); - if (null != mfc) { - try { - //閾炬帴NFC - mfc.connect(); - //鐢ㄤ簬鍒ゆ柇鏃跺�欐湁鍐呭璇诲彇鍑烘潵 - boolean flag = false; - - //楠岃瘉鎵囧尯瀵嗙爜锛屽惁鍒欎細鎶ラ敊锛堥摼鎺ュけ璐ラ敊璇級 - boolean isOpen = mfc.authenticateSectorWithKeyA(1, companyKey); - if (!isOpen) { - isOpen = mfc.authenticateSectorWithKeyA(1, defauleKey); - } - if (isOpen) { - //鑾峰彇鎵囧尯閲岄潰鍧楃殑鏁伴噺 - int bCount = mfc.getBlockCountInSector(1); - //鑾峰彇鎵囧尯绗竴涓潡瀵瑰簲鑺墖瀛樺偍鍣ㄧ殑浣嶇疆锛堟垜鏄繖鏍风悊瑙g殑锛屽洜涓虹0鎵囧尯鐨勮繖涓�兼槸4鑰屼笉鏄�0锛� - int bIndex = mfc.sectorToBlock(1); - //String data1 = ""; - for (int j = 0; j < bCount; j++) { - //璇诲彇鏁版嵁 - byte[] data = null; - try { - data = mfc.readBlock(bIndex); - bIndex++; - list.add(data); - } catch (Exception e) { - e.printStackTrace(); - } - } - } - - } catch (IOException e) { - e.printStackTrace(); - } catch (Exception e) { - e.printStackTrace(); - } finally { - try { - mfc.close(); - return list; - } catch (Exception e) { - e.printStackTrace(); - } - } } return null; - } - - /** - * 鑾峰彇1鎵囧尯淇℃伅 - * - * @param callback - */ - public void getOneSectorData(NFCCallListback callback) { - - - MifareClassic mfc = null; - try { - mfc = MifareClassic.get(tag); - } catch (Exception e) { - e.printStackTrace(); - } - if (null != mfc) { - try { - //閾炬帴NFC - mfc.connect(); - //鐢ㄤ簬鍒ゆ柇鏃跺�欐湁鍐呭璇诲彇鍑烘潵 - boolean flag = false; - List<byte[]> list = new ArrayList<>(); - //楠岃瘉鎵囧尯瀵嗙爜锛屽惁鍒欎細鎶ラ敊锛堥摼鎺ュけ璐ラ敊璇級 - boolean isOpen = mfc.authenticateSectorWithKeyA(1, companyKey); - if (!isOpen) { - isOpen = mfc.authenticateSectorWithKeyA(1, defauleKey); - } - if (isOpen) { - //鑾峰彇鎵囧尯閲岄潰鍧楃殑鏁伴噺 - int bCount = mfc.getBlockCountInSector(1); - //鑾峰彇鎵囧尯绗竴涓潡瀵瑰簲鑺墖瀛樺偍鍣ㄧ殑浣嶇疆锛堟垜鏄繖鏍风悊瑙g殑锛屽洜涓虹0鎵囧尯鐨勮繖涓�兼槸4鑰屼笉鏄�0锛� - int bIndex = mfc.sectorToBlock(1); - //String data1 = ""; - for (int j = 0; j < bCount; j++) { - //璇诲彇鏁版嵁 - byte[] data = null; - try { - data = mfc.readBlock(bIndex); - bIndex++; - list.add(data); - } catch (Exception e) { - e.printStackTrace(); - } - } - flag = true; - } - if (flag) { - callback.callBack(list); - } else { - callback.error(CardCommon.ERROR); - } - } catch (IOException e) { - callback.error(CardCommon.ERROR_MOVE); - e.printStackTrace(); - } catch (Exception e) { - callback.error(CardCommon.ERROR); - e.printStackTrace(); - } finally { - - try { - mfc.close(); - } catch (Exception e) { - e.printStackTrace(); - } - } - } } @@ -410,111 +116,13 @@ * @param b 鍧� * @param callback */ - public void getData(final int a, final int b, final NFCCallByteback callback) { - new Thread(new Runnable() { - @Override - public void run() { - Map<String, List<String>> map = new HashMap<>(); - MifareClassic mfc = MifareClassic.get(tag); - if (null != mfc) { - try { - mfc.connect(); - int count = mfc.getSectorCount(); - if (a < 0 || a > count - 1) { - callback.error(CardCommon.ERROR); - return; - } - int bCount = mfc.getBlockCountInSector(a); - if (b < 0 || b > bCount - 1) { - callback.error(CardCommon.ERROR); - return; - } - - int type = mfc.getType();//鑾峰彇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; - } - boolean isOpen = mfc.authenticateSectorWithKeyA(a, defauleKey); - if (!isOpen) { - isOpen = mfc.authenticateSectorWithKeyA(a, companyKey); - } - if (isOpen) { - int bIndex = mfc.sectorToBlock(a); - byte[] data = mfc.readBlock(bIndex + b); - callback.callBack(data); - } else { - callback.error(CardCommon.ERROR); - } - } catch (IOException e) { - callback.error(CardCommon.ERROR_MOVE); - e.printStackTrace(); - } catch (Exception e) { - callback.error(CardCommon.ERROR); - } finally { - try { - mfc.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - } - } - }).start(); + public void getData(final int a, final int b, final BaseNfcReadHelper.NFCCallByteback callback) { + try { + adapter.getData(a, b, callback); + } catch (Exception e) { + e.printStackTrace(); + } } - /** - * 杩斿洖鐩戝惉绫� - */ - public interface NFCCallMapback { - /** - * 杩斿洖璇诲彇nfc鍗$殑鍏ㄩ儴淇℃伅 - * - * @param data 鍓嶉潰浠h〃鎵囧尯 鍥涗釜鍧楃殑鏁版嵁鐢�#鍙烽殧寮� - */ - void callBack(Map<String, List<byte[]>> data); - - - void error(int code); - } - - /** - * 杩斿洖鐩戝惉绫� - */ - public interface NFCCallListback { - /** - * 杩斿洖璇诲彇nfc鍗$殑鍏ㄩ儴淇℃伅 - * - * @param data 鍓嶉潰浠h〃鎵囧尯 鍥涗釜鍧楃殑鏁版嵁鐢�#鍙烽殧寮� - */ - void callBack(List<byte[]> data); - - - void error(int code); - } - - - public interface NFCCallByteback { - /** - * 杩斿洖璇诲彇nfc鍗$殑鍏ㄩ儴淇℃伅 - * - * @param data 鍓嶉潰浠h〃鎵囧尯 鍥涗釜鍧楃殑鏁版嵁鐢�#鍙烽殧寮� - */ - - void callBack(byte[] data); - - void error(int code); - } } -- Gitblit v1.8.0