左晓为主开发手持机充值管理机
baselibrary/src/main/java/com/dayu/baselibrary/tools/nfc/NativeNfcWriteHelper.java
@@ -47,6 +47,7 @@
     * @param userCard 用户卡内容
     * @param
     */
    @Override
    public boolean writeUserData(BaseUserCardCard userCard) {
        if (userCard != null) {
            int a = 1;
@@ -113,7 +114,13 @@
     * @param b   书写的块(从0开始数)
     * @param
     */
    @Override
    public boolean writeData(byte[] str, int a, int b) {
        return writeData(str, a, b, null);
    }
    @Override
    public boolean writeData(byte[] str, int a, int b, NFCCallBack callBack) {
        Log.i("NFCWreatActivity", "writeData: a=" + a + " b=" + b);
        if (str.length <= 16) {
            try {
@@ -126,35 +133,87 @@
                        int count = mfc.getSectorCount();
                        //如果传进来的扇区大了或者小了直接退出方法
                        if (a > count - 1 || a < 0) {
                            if (callBack != null) {
                                callBack.isSusses(false, "扇区错误--" + a);
                            }
                            return false;
                        }
                        //获取写的扇区的块的数量
                        int bCount = mfc.getBlockCountInSector(a);
                        //如果输入的块大了或者小了也是直接退出
                        if (b > bCount - 1 || b < 0) {
                            if (callBack != null) {
                                callBack.isSusses(false, "块区错误--" + b);
                            }
                            return false;
                        }
                        //验证扇区密码
                        boolean isOpen = false;
                        for (int i = 0; i < listKeyA.size(); i++) {
                            if (mfc.authenticateSectorWithKeyA(0, listKeyA.get(i))) {
                        if (listKeyA.size() != 0) {
                            for (int i = 0; i < listKeyA.size(); i++) {
                                if (mfc.authenticateSectorWithKeyA(0, listKeyA.get(i))) {
                                    isOpen = true;
                                    if (listKeyA.get(i).equals(defauleKey)) {
                                        //当前为默认白卡密码时写卡时修改密码
                                        changePasword(a, mfc);
                                    }
                                    break;
                                }
                            }
                        } else if (listA_PS.size() != 0 && listA_PS.size() > a) {
                            if (mfc.authenticateSectorWithKeyA(0, listA_PS.get(a))) {
                                isOpen = true;
                                if (listKeyA.get(i).equals(defauleKey)) {
                                if (listKeyA.get(a).equals(defauleKey)) {
                                    //当前为默认白卡密码时写卡时修改密码
                                    changePasword(a, mfc);
                                }
                                break;
                            }
                        }
                        if (isOpen) {
                            int bIndex = mfc.sectorToBlock(a);
                            //写卡
                            mfc.writeBlock(bIndex + b, str);
                            return true;
                            // 校验写入数据是否正确
                            boolean isVerified = true;
                            // 读取数据验证
                            byte[] readResult = mfc.readBlock(bIndex + b);
                            if (readResult == null) {
                                if (callBack != null) {
                                    callBack.isSusses(false, a + "扇区写卡时再次读取验证时失败");
                                }
                                return false;
                            }
                            for (int i = 0; i < str.length; i++) {
                                if (str[i] != readResult[i]) {
                                    isVerified = false;
                                    break;
                                }
                            }
                            if (isVerified) {
                                if (callBack != null) {
                                    callBack.isSusses(true, "写入成功并验证通过");
                                }
                                return true;
                            } else {
                                if (callBack != null) {
                                    callBack.isSusses(false, "写入验证失败");
                                }
                                return false;
                            }
                        }
                        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 {
@@ -179,12 +238,12 @@
     * @param a 书写的扇区
     *          //     * @param callback 返回监听
     */
    @Override
    public boolean changePasword(int a, MifareClassic mfc) {
        byte[] data = new byte[16];
        if (null != mfc) {
            try {
                //将密码转换为keyA
                byte[] dataA = HexUtil.hexToByteArray(companyKeyA);
                for (int i = 0; i < dataA.length; i++) {
@@ -218,6 +277,7 @@
     *
     * @return
     */
    @Override
    public boolean initCard() {
        try {
            MifareClassic mfc = MifareClassic.get(tag);
@@ -278,4 +338,6 @@
        }
        return false;
    }
}