左晓为主开发手持机充值管理机
zuoxiao
2024-08-26 12fb3fb97f135bbd78b31a06f8451f9b42a2ed6f
qiheonlinelibrary/src/main/java/com/dayu/qiheonlinelibrary/tools/NFCWriteHelper.java
@@ -11,6 +11,8 @@
import com.dayu.qiheonlinelibrary.card.UserCard;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
 * @author zx
@@ -70,6 +72,7 @@
            int a = 1;
            try {
                MifareClassic mfc = MifareClassic.get(tag);
                List<byte[]> list = new ArrayList<>();
                if (null != mfc) {
                    try {
                        //连接NFC
@@ -96,7 +99,22 @@
                                //写卡
                                mfc.writeBlock(bIndex + b, data);
                            }
                            return true;
                            int bIndex = mfc.sectorToBlock(1);
                            for (int j = 0; j < 3; j++) {
                                //读取数据
                                byte[] data = null;
                                try {
                                    data = mfc.readBlock(bIndex);
                                    bIndex++;
                                    list.add(data);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                            if (userCard.equlsUserCard(list)) {
                                return true;
                            }
                            return false;
                        }
                        return false;
                    } catch (Exception e) {
@@ -241,6 +259,78 @@
    }
    /**
     * 初始化卡
     *
     * @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 {