左晓为主开发手持机充值管理机
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) {
@@ -262,13 +280,13 @@
                        boolean isOpen = mfc.authenticateSectorWithKeyA(sector, defauleKey);
                        if (!isOpen) {
                            isOpen = mfc.authenticateSectorWithKeyA(sector, companyKey2);
                            if (isOpen){
                            if (isOpen) {
                                mfc.authenticateSectorWithKeyB(sector, companyKey2);
                            }
                        }
                        if (!isOpen) {
                            isOpen = mfc.authenticateSectorWithKeyA(sector, companyKey);
                            if (isOpen){
                            if (isOpen) {
                                mfc.authenticateSectorWithKeyB(sector, companyKey);
                            }
                        }
@@ -276,7 +294,7 @@
                            //获取写的扇区的块的数量
                            int blockCount = mfc.getBlockCountInSector(sector);
                            int blockIndex = mfc.sectorToBlock(sector);
                            for (int block = 0; block < blockCount ; block++) {
                            for (int block = 0; block < blockCount; block++) {
                                // 跳过第 0 扇区的第 0 块
                                if (sector == 0 && block == 0) {
                                    blockIndex++;
@@ -289,7 +307,6 @@
                                    mfc.writeBlock(blockIndex, initDataBytes);
                                }
                                //写卡
                                blockIndex++;
                            }
                        }