左晓为主开发手持机充值管理机
zuoxiao
2024-08-21 6a5879320dfac43155d024814a0a4d794a612696
qiheonlinelibrary/src/main/java/com/dayu/qiheonlinelibrary/tools/NfcReadHelper.java
@@ -9,6 +9,7 @@
import com.dayu.baselibrary.tools.HexUtil;
import com.dayu.baselibrary.utils.BaseCommon;
import com.dayu.qiheonlinelibrary.card.ManagerToUserCard;
import com.dayu.qiheonlinelibrary.card.UserCard;
import com.dayu.qiheonlinelibrary.utils.CardCommon;
@@ -135,8 +136,8 @@
                    byte[] data = mfc.readBlock(bIndex + 0);
                    if (data != null && data.length > 0) {
                        String hex = HexUtil.bytesToHex(Arrays.copyOfRange(data, 0, 4));
//                        hex = HexUtil.spaceHex(hex);
//                        hex = HexUtil.HighLowHex(hex);
                        hex = HexUtil.spaceHex(hex);
                        hex = HexUtil.HighLowHex(hex);
                        Log.i("NFCWreatActivity", "hex===" + hex);
                        return hex.toUpperCase();
                    }
@@ -178,6 +179,8 @@
                    byte[] data = mfc.readBlock(bIndex + 0);
                    if (data != null && data.length > 0) {
                        String hex = HexUtil.bytesToHex(Arrays.copyOfRange(data, 0, 4));
                        hex = HexUtil.spaceHex(hex);
                        hex = HexUtil.HighLowHex(hex);
                        strData.append(hex);
                        strData.append(",");
                        Log.i("NFCWreatActivity", "hex===" + hex);
@@ -476,6 +479,71 @@
        }).start();
    }
    /**
     * 获取用户卡信息
     *
     * @return
     */
    public ManagerToUserCard getManagerToUserCardData() {
        ManagerToUserCard managerToUserCard = 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);
                    //获取扇区第一个块对应芯片存储器的位置(我是这样理解的,因为第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) {
                    managerToUserCard = ManagerToUserCard.getBean(list);
                    return managerToUserCard;
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    mfc.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
    /**
     * 返回监听类
     */