左晓为主开发手持机充值管理机
zuoxiao
2024-08-21 6a5879320dfac43155d024814a0a4d794a612696
qiheonlinelibrary/src/main/java/com/dayu/qiheonlinelibrary/tools/NFCWriteHelper.java
@@ -241,6 +241,79 @@
    }
    /**
     * 初始化卡
     *
     * @return
     */
    public boolean initCard() {
        try {
            MifareClassic mfc = MifareClassic.get(tag);
            if (null != mfc) {
                try {
                    //连接NFC
                    mfc.connect();
                    //获取扇区数量
                    int count = mfc.getSectorCount();
                    byte[] data = new byte[16];
                    String initData = "FFFFFFFFFFFFFF078069FFFFFFFFFFFF";
                    byte[] initDataBytes = HexUtil.hexToByteArray(initData);
                    for (int sector = 0; sector < count; sector++) {
                        //验证扇区密码
                        boolean isOpen = mfc.authenticateSectorWithKeyA(sector, defauleKey);
                        if (!isOpen) {
                            isOpen = mfc.authenticateSectorWithKeyA(sector, companyKey2);
                            if (isOpen){
                                mfc.authenticateSectorWithKeyB(sector, companyKey2);
                            }
                        }
                        if (!isOpen) {
                            isOpen = mfc.authenticateSectorWithKeyA(sector, companyKey);
                            if (isOpen){
                                mfc.authenticateSectorWithKeyB(sector, companyKey);
                            }
                        }
                        if (isOpen) {
                            //获取写的扇区的块的数量
                            int blockCount = mfc.getBlockCountInSector(sector);
                            int blockIndex = mfc.sectorToBlock(sector);
                            for (int block = 0; block < blockCount ; block++) {
                                // 跳过第 0 扇区的第 0 块
                                if (sector == 0 && block == 0) {
                                    blockIndex++;
                                    continue;
                                }
                                if (block < 3) {
                                    mfc.writeBlock(blockIndex, data);
                                } else {
                                    mfc.writeBlock(blockIndex, initDataBytes);
                                }
                                //写卡
                                blockIndex++;
                            }
                        }
                    }
                    return true;
                } catch (Exception e) {
                    e.printStackTrace();
                    return false;
                } finally {
                    try {
                        mfc.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return false;
    }
    /**
     * 返回监听类
     */
    public interface NFCCallback {