左晓为主开发手持机充值管理机
zuoxiao
2023-11-14 5a843ba11b991722f8f2af386f0e546e2769f7c3
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package com.dayu.recharge.tools;
 
import android.content.Intent;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.MifareClassic;
 
import java.io.IOException;
import java.util.ArrayList;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * @author zx
 * @date 2018/4/23 14:31
 * email 1058083107@qq.com
 * description nfc读取工具类
 */
public class NfcReadHelper extends BaseNFCHelper {
 
    private Tag tag;
    //    private NFCCallback callback;
    private static NfcReadHelper helper;
 
 
    public NfcReadHelper(Intent intent) {
        this.tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
 
    }
 
    /**
     * 单例初始化
     *
     * @param intent
     * @return
     */
    public static NfcReadHelper getInstence(Intent intent) {
        if (helper == null) {
            helper = new NfcReadHelper(intent);
        }
        return helper;
    }
 
    /**
     * 设置NFC卡的密码
     *
     * @param str
     * @return
     */
//    public NfcReadHelper setPassword(String str) {
//
//        if (null != str && (str.length() <= 6)) {
//            for (int i = 0; i < str.length(); i++) {
//                bytes[i] = (byte) str.charAt(i);
//            }
//        }
//        return helper;
//    }
 
    /**
     * 读取NFC卡的全部信息
     *
     * @param callback
     */
    public void getAllData(final NFCCallListback callback) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Map<String, List<byte[]>> map = new HashMap<>();
                MifareClassic mfc = MifareClassic.get(tag);
                if (null != mfc) {
                    try {
                        //链接NFC
                        mfc.connect();
                        //获取扇区数量
                        int count = mfc.getSectorCount();
                        //存储空间
                        int size = mfc.getSize();
                        //用于判断时候有内容读取出来
                        boolean flag = false;
                        for (int i = 0; i < count; i++) {
                            List<byte[]> list = new ArrayList<>();
                            //验证扇区密码,否则会报错(链接失败错误)
                            boolean isOpen = mfc.authenticateSectorWithKeyA(i, bytes);
                            if (isOpen) {
                                //获取扇区里面块的数量
                                int bCount = mfc.getBlockCountInSector(i);
                                //获取扇区第一个块对应芯片存储器的位置(我是这样理解的,因为第0扇区的这个值是4而不是0)
                                int bIndex = mfc.sectorToBlock(i);
                                //String data1 = "";
                                for (int j = 0; j < bCount; j++) {
                                    //读取数据
                                    byte[] data = null;
                                    try {
                                        data = mfc.readBlock(bIndex);
                                        bIndex++;
                                        list.add(data);
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }
                                flag = true;
                            }
                            map.put(i + "", list);
                        }
                        if (flag) {
                            callback.callBack(map);
                        } else {
                            callback.error();
                        }
                    } catch (Exception e) {
                        callback.error();
                        e.printStackTrace();
                    } finally {
                        try {
                            mfc.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }).start();
    }
 
    /**
     * 读取NFC卡的特定扇区信息
     *
     * @param a        扇区
     * @param b        块
     * @param callback
     */
    public void getData(final int a, final int b, final NFCCallByteback callback) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Map<String, List<String>> map = new HashMap<>();
                MifareClassic mfc = MifareClassic.get(tag);
                if (null != mfc) {
                    try {
                        mfc.connect();
                        int count = mfc.getSectorCount();
                        if (a < 0 || a > count - 1) {
                            callback.error();
                            return;
                        }
                        int bCount = mfc.getBlockCountInSector(a);
                        if (b < 0 || b > bCount - 1) {
                            callback.error();
                            return;
                        }
 
                        int type = mfc.getType();//获取TAG的类型
                        String typeS = "";
                        switch (type) {
                            case MifareClassic.TYPE_CLASSIC:
                                typeS = "TYPE_CLASSIC";
                                break;
                            case MifareClassic.TYPE_PLUS:
                                typeS = "TYPE_PLUS";
                                break;
                            case MifareClassic.TYPE_PRO:
                                typeS = "TYPE_PRO";
                                break;
                            case MifareClassic.TYPE_UNKNOWN:
                                typeS = "TYPE_UNKNOWN";
                                break;
                        }
                        boolean isOpen = mfc.authenticateSectorWithKeyA(a, bytes);
                        if (isOpen) {
                            int bIndex = mfc.sectorToBlock(a);
                            byte[] data = mfc.readBlock(bIndex + b);
                            callback.callBack(data);
                        } else {
                            callback.error();
                        }
                    } catch (Exception e) {
                        callback.error();
                        e.printStackTrace();
                    } finally {
                        try {
                            mfc.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }).start();
    }
 
    /**
     * 返回监听类
     */
    public interface NFCCallListback {
        /**
         * 返回读取nfc卡的全部信息
         *
         * @param data 前面代表扇区 四个块的数据用#号隔开
         */
        void callBack(Map<String, List<byte[]>> data);
 
 
        void error();
    }
 
    public interface NFCCallByteback {
        /**
         * 返回读取nfc卡的全部信息
         *
         * @param data 前面代表扇区 四个块的数据用#号隔开
         */
 
        void callBack(byte[] data);
 
        void error();
    }
 
}