左晓为主开发手持机充值管理机
zuoxiao
2023-11-22 1ffa3cb19f95855d44fbc9485f4eb3286b12e359
app/src/main/java/com/dayu/recharge/tools/NFCWriteHelper.java
@@ -24,10 +24,10 @@
 * email 1058083107@qq.com
 * description
 */
public class NFCWriteHelper extends BaseNFCHelper{
public class NFCWriteHelper extends BaseNFCHelper {
    private Tag tag;
    private NFCWriteHelper.NFCCallback callback;
    private static NFCWriteHelper helper;
    private static int PASSWORD_LENTH = 6;
@@ -67,12 +67,12 @@
    /**
     * 写卡
     *
     * @param str      书写内容,16个字节
     * @param a        书写的扇区 (从0开始数)
     * @param b        书写的块(从0开始数)
     * @param callback 返回监听
     * @param str 书写内容,16个字节
     * @param a   书写的扇区 (从0开始数)
     * @param b   书写的块(从0开始数)
     * @param
     */
    public void writeData(byte[] str, int a, int b, NFCWriteHelper.NFCCallback callback) {
    public boolean writeData(byte[] str, int a, int b) {
        if (str.length <= 16) {
            try {
                MifareClassic mfc = MifareClassic.get(tag);
@@ -84,15 +84,13 @@
                        int count = mfc.getSectorCount();
                        //如果传进来的扇区大了或者小了直接退出方法
                        if (a > count - 1 || a < 0) {
                            callback.isSusses(false);
                            return;
                            return false;
                        }
                        //获取写的扇区的块的数量
                        int bCount = mfc.getBlockCountInSector(a);
                        //如果输入的块大了或者小了也是直接退出
                        if (b > bCount - 1 || b < 0) {
                            callback.isSusses(false);
                            return;
                            return false;
                        }
                        //验证扇区密码
                        boolean isOpen = mfc.authenticateSectorWithKeyA(a, bytes);
@@ -100,13 +98,12 @@
                            int bIndex = mfc.sectorToBlock(a);
                            //写卡
                            mfc.writeBlock(bIndex + b, str);
                            callback.isSusses(true);
                            return;
                            return true;
                        }
                        callback.isSusses(false);
                        return false;
                    } catch (Exception e) {
                        e.printStackTrace();
                        callback.isSusses(false);
                        return false;
                    } finally {
                        try {
                            mfc.close();
@@ -117,8 +114,10 @@
                }
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
        return false;
    }