左晓为主开发手持机充值管理机
zuojincheng
2 天以前 dd0f9e5f533d868d68c5fc343a44356b537b3988
baselibrary/src/main/java/com/dayu/baselibrary/tools/nfc/NativeNfcWriteHelper.java
@@ -25,7 +25,6 @@
    private static NativeNfcWriteHelper helper;
    public void setIntent(Intent intent) {
        this.tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    }
@@ -43,7 +42,6 @@
        helper.setIntent(intent);
        return helper;
    }
    /**
     * 写卡
@@ -109,6 +107,85 @@
        return false;
    }
    /**
     * 写卡(带回调)
     *
     * @param userCard 用户卡内容
     * @param sector   扇区
     * @param callBack 回调接口
     */
    public boolean writeUserData(BaseUserCardCard userCard, int sector, NFCCallBack callBack) {
        if (userCard != null) {
            int a = sector;
            try {
                MifareClassic mfc = MifareClassic.get(tag);
                if (null != mfc) {
                    try {
                        // 连接NFC
                        mfc.connect();
                        // 验证扇区密码
                        boolean isOpen = false;
                        for (int i = 0; i < listKeyA.size(); i++) {
                            if (mfc.authenticateSectorWithKeyA(a, listKeyA.get(i))) {
                                isOpen = true;
                                if (listKeyA.get(i).equals(defauleKey)) {
                                    // 当前为默认白卡密码时写卡时修改密码
                                    changePasword(a, mfc);
                                }
                                break;
                            }
                        }
                        if (isOpen) {
                            for (int b = 0; b < 3; b++) {
                                byte[] data;
                                if (b == 0) {
                                    data = userCard.getZeroBytes();
                                } else if (b == 1) {
                                    data = userCard.getOneBytes();
                                } else {
                                    data = userCard.getTwoBytes();
                                }
                                int bIndex = mfc.sectorToBlock(a);
                                // 写卡
                                mfc.writeBlock(bIndex + b, data);
                            }
                            if (callBack != null) {
                                callBack.isSusses(true, "用户数据写入成功");
                            }
                            return true;
                        }
                        if (callBack != null) {
                            callBack.isSusses(false, "扇区" + a + "密码验证失败");
                        }
                        return false;
                    } catch (Exception e) {
                        e.printStackTrace();
                        if (callBack != null) {
                            callBack.isSusses(false, "扇区" + a + "写卡异常: " + e.getMessage());
                        }
                        return false;
                    } finally {
                        try {
                            mfc.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                if (callBack != null) {
                    callBack.isSusses(false, "写卡异常: " + e.getMessage());
                }
                return false;
            }
        } else {
            if (callBack != null) {
                callBack.isSusses(false, "用户卡数据为空");
            }
        }
        return false;
    }
    /**
     * 写卡
@@ -245,7 +322,6 @@
        }
        return false;
    }
    /**
     * 修改密码
@@ -399,8 +475,6 @@
        return false;
    }
    /**
     * 初始化卡
     *
@@ -467,6 +541,5 @@
        }
        return false;
    }
}