左晓为主开发手持机充值管理机
zuoxiao
2023-11-22 a6fdb620fa48f6c78f066d8e01b69d2570d3baa7
app/src/main/java/com/dayu/recharge/tools/NfcReadHelper.java
@@ -4,6 +4,7 @@
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;
@@ -46,7 +47,11 @@
        return helper;
    }
    /**
     * 获取用户卡信息
     *
     * @return
     */
    public UserCard getUserCardData() {
        UserCard userCard = null;
        Map<String, List<byte[]>> map = new HashMap<>();
@@ -63,7 +68,10 @@
                boolean flag = false;
                List<byte[]> list = new ArrayList<>();
                //验证扇区密码,否则会报错(链接失败错误)
                boolean isOpen = mfc.authenticateSectorWithKeyA(1, bytes);
                boolean isOpen = mfc.authenticateSectorWithKeyA(1, defauleKey);
                if (!isOpen){
                    isOpen=   mfc.authenticateSectorWithKeyA(1, companyKey);
                }
                if (isOpen) {
                    //获取扇区里面块的数量
                    int bCount = mfc.getBlockCountInSector(1);
@@ -103,6 +111,41 @@
        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卡的全部信息
@@ -128,7 +171,10 @@
                        for (int i = 0; i < count; i++) {
                            List<byte[]> list = new ArrayList<>();
                            //验证扇区密码,否则会报错(链接失败错误)
                            boolean isOpen = mfc.authenticateSectorWithKeyA(i, bytes);
                            boolean isOpen = mfc.authenticateSectorWithKeyA(i, defauleKey);
                            if (!isOpen){
                                isOpen = mfc.authenticateSectorWithKeyA(i, companyKey);
                            }
                            if (isOpen) {
                                //获取扇区里面块的数量
                                int bCount = mfc.getBlockCountInSector(i);
@@ -216,7 +262,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);