From 75417d96b932dc5da0b3457fa1bc663fa64034f7 Mon Sep 17 00:00:00 2001
From: zuoxiao <470321431@qq.com>
Date: 星期三, 29 十一月 2023 17:45:49 +0800
Subject: [PATCH] 修改权限逻辑,获取权限后再生成数据库 数据库保存在sd防止用户删除后没有数据了
---
app/src/main/java/com/dayu/recharge/tools/NfcReadHelper.java | 306 +++++++++++++++++++++++++++++++++++++++++---------
1 files changed, 247 insertions(+), 59 deletions(-)
diff --git a/app/src/main/java/com/dayu/recharge/tools/NfcReadHelper.java b/app/src/main/java/com/dayu/recharge/tools/NfcReadHelper.java
index 5e3b08d..650415d 100644
--- a/app/src/main/java/com/dayu/recharge/tools/NfcReadHelper.java
+++ b/app/src/main/java/com/dayu/recharge/tools/NfcReadHelper.java
@@ -4,7 +4,9 @@
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.MifareClassic;
+import android.util.Log;
+import com.dayu.recharge.card.UserCard;
import com.dayu.recharge.utils.MyCommon;
import java.io.IOException;
@@ -46,73 +48,240 @@
}
/**
- * 璇诲彇NFC鍗$殑鍏ㄩ儴淇℃伅
+ * 鑾峰彇鐢ㄦ埛鍗′俊鎭�
*
- * @param callback
+ * @return
*/
- public void getAllData(final NFCCallListback callback) {
- new Thread(new Runnable() {
- @Override
- public void run() {
- 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, bytes);
- 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(MyCommon.ERROR);
- }
- } catch (IOException e) {
- callback.error(MyCommon.ERROR_MOVE);
- e.printStackTrace();
- } catch (Exception e) {
- callback.error(MyCommon.ERROR);
- e.printStackTrace();
- } finally {
+ 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 {
- mfc.close();
+ 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();
}
}
- }).start();
+ }
+ return null;
}
+
+ public String getCradType() {
+
+ MifareClassic mfc = MifareClassic.get(tag);
+ if (null != mfc) {
+ try {
+ mfc.connect();
+ boolean 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]);
+ Log.i("NFCWreatActivity", "hex===" + hex);
+ return hex;
+ }
+
+ } else {
+ Log.i("NFCWreatActivity", "isOpen===" + isOpen);
+ return "-1";
+ }
+ } catch (IOException e) {
+ return "-1";
+ } finally {
+ try {
+ mfc.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+ return "";
+ }
+
+ /**
+ * 璇诲彇NFC鍗$殑鍏ㄩ儴淇℃伅
+ *
+ * @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(MyCommon.ERROR);
+ }
+ } catch (IOException e) {
+ callback.error(MyCommon.ERROR_MOVE);
+ e.printStackTrace();
+ } catch (Exception e) {
+ callback.error(MyCommon.ERROR);
+ e.printStackTrace();
+ } finally {
+ try {
+ mfc.close();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ }
+ }
+
+
+ /**
+ * 鑾峰彇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(MyCommon.ERROR);
+ }
+ } catch (IOException e) {
+ callback.error(MyCommon.ERROR_MOVE);
+ e.printStackTrace();
+ } catch (Exception e) {
+ callback.error(MyCommon.ERROR);
+ e.printStackTrace();
+ } finally {
+
+ try {
+ mfc.close();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ }
+ }
+
/**
* 璇诲彇NFC鍗$殑鐗瑰畾鎵囧尯淇℃伅
@@ -157,7 +326,10 @@
typeS = "TYPE_UNKNOWN";
break;
}
- boolean isOpen = mfc.authenticateSectorWithKeyA(a, bytes);
+ 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);
@@ -185,7 +357,7 @@
/**
* 杩斿洖鐩戝惉绫�
*/
- public interface NFCCallListback {
+ public interface NFCCallMapback {
/**
* 杩斿洖璇诲彇nfc鍗$殑鍏ㄩ儴淇℃伅
*
@@ -197,6 +369,22 @@
void error(int code);
}
+ /**
+ * 杩斿洖鐩戝惉绫�
+ */
+ public interface NFCCallListback {
+ /**
+ * 杩斿洖璇诲彇nfc鍗$殑鍏ㄩ儴淇℃伅
+ *
+ * @param data 鍓嶉潰浠h〃鎵囧尯 鍥涗釜鍧楃殑鏁版嵁鐢�#鍙烽殧寮�
+ */
+ void callBack(List<byte[]> data);
+
+
+ void error(int code);
+ }
+
+
public interface NFCCallByteback {
/**
* 杩斿洖璇诲彇nfc鍗$殑鍏ㄩ儴淇℃伅
--
Gitblit v1.8.0