zuoxiao
2024-03-04 275fbc47873d5c4f78a57b4fa58a08e2e7af2dc2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package com.dayu.recharge.tools;
 
import android.app.Activity;
import android.content.Intent;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.MifareClassic;
import android.util.Log;
 
import java.io.IOException;
 
/**
 * @author zx
 * @date 2018/4/25 8:08
 * email 1058083107@qq.com
 * description
 */
public class NFCWriteHelper extends BaseNFCHelper {
 
    private Tag tag;
 
    private static NFCWriteHelper helper;
    private static int PASSWORD_LENTH = 12;
 
    public NFCWriteHelper(Intent intent, Activity activity) {
        super(activity);
        this.tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    }
 
    /**
     * 单例初始化
     *
     * @param intent
     * @return
     */
    public static NFCWriteHelper getInstence(Intent intent, Activity activity) {
        if (helper == null) {
            helper = new NFCWriteHelper(intent, activity);
        }
        return helper;
    }
 
    /**
     * 设置NFC卡的读取密码
     *
     * @param str
     * @return
     */
    public NFCWriteHelper setReadPassword(String str) {
        if (null != str && (str.length() <= PASSWORD_LENTH)) {
            for (int i = 0; i < str.length(); i++) {
                defauleKey[i] = (byte) str.charAt(i);
            }
        }
        return helper;
    }
 
    /**
     * 写卡
     *
     * @param str 书写内容,16个字节
     * @param a   书写的扇区 (从0开始数)
     * @param b   书写的块(从0开始数)
     * @param
     */
    public boolean writeData(byte[] str, int a, int b) {
        Log.i("NFCWreatActivity", "writeData: a=" + a + " b=" + b);
        //写卡时修改所有密码
        changePasword(a);
        if (str.length <= 16) {
            try {
                MifareClassic mfc = MifareClassic.get(tag);
                if (null != mfc) {
                    try {
                        //连接NFC
                        mfc.connect();
                        //获取扇区数量
                        int count = mfc.getSectorCount();
                        //如果传进来的扇区大了或者小了直接退出方法
                        if (a > count - 1 || a < 0) {
                            return false;
                        }
                        //获取写的扇区的块的数量
                        int bCount = mfc.getBlockCountInSector(a);
                        //如果输入的块大了或者小了也是直接退出
                        if (b > bCount - 1 || b < 0) {
                            return false;
                        }
                        //验证扇区密码
                        boolean isOpen = mfc.authenticateSectorWithKeyA(a, defauleKey);
                        if (!isOpen) {
                            isOpen = mfc.authenticateSectorWithKeyA(a, companyKey);
                        }
                        if (isOpen) {
                            int bIndex = mfc.sectorToBlock(a);
                            //写卡
                            mfc.writeBlock(bIndex + b, str);
                            return true;
                        }
                        return false;
                    } 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;
    }
 
 
    /**
     * 修改密码
     *
     * @param a 书写的扇区
     *          //     * @param callback 返回监听
     */
    public boolean changePasword(int a) {
        MifareClassic mfc = MifareClassic.get(tag);
        byte[] data = new byte[16];
        if (null != mfc) {
            try {
                mfc.connect();
 
                int count = mfc.getSectorCount();
                boolean isOpen = mfc.authenticateSectorWithKeyA(a, defauleKey);
 
                //验证是否是默认密码,当默认密码时修改密码
                if (isOpen) {
                    //将密码转换为keyA
                    byte[] dataA = HexUtil.hexToByteArray(companyKeyA);
                    for (int i = 0; i < dataA.length; i++) {
                        data[i] = dataA[i];
                    }
                    //输入控制位
                    data[6] = (byte) 0xFF;
                    data[7] = (byte) 0x07;
                    data[8] = (byte) 0x80;
                    data[9] = (byte) 0x69;
                    byte[] dataB = HexUtil.hexToByteArray(companyKeyB);
                    //将密码转换为KeyB
                    for (int i = 0; i < dataB.length; i++) {
                        data[i + 10] = dataB[i];
                    }
                    int bIndex = mfc.sectorToBlock(a);
                    int bCount = mfc.getBlockCountInSector(a);
                    //写到扇区的最后一个块
                    mfc.writeBlock(bIndex + bCount - 1, data);
//                    byte[] dataa = mfc.readBlock(bIndex + bCount - 1);
//                    // 修改密码 A
//                    mfc.writeBlock(mfc.sectorToBlock(a) + 3, dataA);
//                    // 修改密码 B
//                    mfc.writeBlock(mfc.sectorToBlock(a) + 7, dataB);
                }
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
//                callback.isSusses(false);
            } finally {
                try {
                    mfc.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return false;
    }
 
    /**
     * 返回监听类
     */
    public interface NFCCallback {
        /**
         * 返回是否成功
         *
         * @param flag
         */
        void isSusses(boolean flag);
    }
}