|  |  | 
 |  |  | package com.dayu.recharge.tools; | 
 |  |  |  | 
 |  |  | import android.app.Activity; | 
 |  |  | import android.content.Intent; | 
 |  |  | import android.nfc.NfcAdapter; | 
 |  |  | import android.nfc.Tag; | 
 |  |  | 
 |  |  |  | 
 |  |  | import java.io.IOException; | 
 |  |  | import java.util.ArrayList; | 
 |  |  |  | 
 |  |  | import java.util.HashMap; | 
 |  |  | import java.util.List; | 
 |  |  | import java.util.Map; | 
 |  |  | 
 |  |  |     private static NfcReadHelper helper; | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     public NfcReadHelper(Intent intent) { | 
 |  |  |     public NfcReadHelper(Intent intent, Activity activity) { | 
 |  |  |         super(activity); | 
 |  |  |         this.tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); | 
 |  |  |  | 
 |  |  |     } | 
 |  |  | 
 |  |  |      * @param intent | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     public static NfcReadHelper getInstence(Intent intent) { | 
 |  |  |     public static NfcReadHelper getInstence(Intent intent, Activity activity) { | 
 |  |  |         if (helper == null) { | 
 |  |  |             helper = new NfcReadHelper(intent); | 
 |  |  |             helper = new NfcReadHelper(intent, activity); | 
 |  |  |         } | 
 |  |  |         return helper; | 
 |  |  |     } | 
 |  |  | 
 |  |  |                 List<byte[]> list = new ArrayList<>(); | 
 |  |  |                 //验证扇区密码,否则会报错(链接失败错误) | 
 |  |  |                 boolean isOpen = mfc.authenticateSectorWithKeyA(1, defauleKey); | 
 |  |  |                 if (!isOpen){ | 
 |  |  |                     isOpen=   mfc.authenticateSectorWithKeyA(1, companyKey); | 
 |  |  |                 if (!isOpen) { | 
 |  |  |                     isOpen = mfc.authenticateSectorWithKeyA(1, companyKey); | 
 |  |  |                 } | 
 |  |  |                 if (isOpen) { | 
 |  |  |                     //获取扇区里面块的数量 | 
 |  |  | 
 |  |  |             try { | 
 |  |  |                 mfc.connect(); | 
 |  |  |                 boolean isOpen = mfc.authenticateSectorWithKeyA(1, defauleKey); | 
 |  |  |                 if (!isOpen){ | 
 |  |  |                     isOpen= mfc.authenticateSectorWithKeyA(1, companyKey); | 
 |  |  |                 if (!isOpen) { | 
 |  |  |                     isOpen = mfc.authenticateSectorWithKeyA(1, companyKey); | 
 |  |  |                 } | 
 |  |  |                 if (isOpen) { | 
 |  |  |                     int bIndex = mfc.sectorToBlock(1); | 
 |  |  | 
 |  |  |      * | 
 |  |  |      * @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, defauleKey); | 
 |  |  |                             if (!isOpen){ | 
 |  |  |                                 isOpen = mfc.authenticateSectorWithKeyA(i, companyKey); | 
 |  |  |     public void getAllData(final NFCCallMapback callback) { | 
 |  |  |         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, defauleKey); | 
 |  |  |                     if (!isOpen) { | 
 |  |  |                         isOpen = mfc.authenticateSectorWithKeyA(i, companyKey); | 
 |  |  |                     } | 
 |  |  |                     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(); | 
 |  |  |                             } | 
 |  |  |                             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(MyCommon.ERROR); | 
 |  |  |                         } | 
 |  |  |                     } catch (IOException e) { | 
 |  |  |                         callback.error(MyCommon.ERROR_MOVE); | 
 |  |  |                         e.printStackTrace(); | 
 |  |  |                     } catch (Exception e) { | 
 |  |  |                         callback.error(MyCommon.ERROR); | 
 |  |  |                         e.printStackTrace(); | 
 |  |  |                     } finally { | 
 |  |  |                         flag = true; | 
 |  |  |                     } | 
 |  |  |                     map.put(i + "", list); | 
 |  |  |                 } | 
 |  |  |                 if (flag) { | 
 |  |  |                     callback.callBack(map); | 
 |  |  |                 } else { | 
 |  |  |                     callback.error(MyCommon.ERROR); | 
 |  |  |                 } | 
 |  |  |             } catch (IOException e) { | 
 |  |  |                 callback.error(MyCommon.ERROR_MOVE); | 
 |  |  |                 e.printStackTrace(); | 
 |  |  |             } catch (Exception e) { | 
 |  |  |                 callback.error(MyCommon.ERROR); | 
 |  |  |                 e.printStackTrace(); | 
 |  |  |             } finally { | 
 |  |  |                 try { | 
 |  |  |                     mfc.close(); | 
 |  |  |                 } catch (Exception e) { | 
 |  |  |                     e.printStackTrace(); | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 获取1扇区信息 | 
 |  |  |      * | 
 |  |  |      * @param callback | 
 |  |  |      */ | 
 |  |  |     public void getOneSectorData(NFCCallListback callback) { | 
 |  |  |  | 
 |  |  |  | 
 |  |  |         MifareClassic mfc = null; | 
 |  |  |         try { | 
 |  |  |             mfc = MifareClassic.get(tag); | 
 |  |  |         } catch (Exception e) { | 
 |  |  |             e.printStackTrace(); | 
 |  |  |         } | 
 |  |  |         if (null != mfc) { | 
 |  |  |             try { | 
 |  |  |                 //链接NFC | 
 |  |  |                 mfc.connect(); | 
 |  |  |                 //用于判断时候有内容读取出来 | 
 |  |  |                 boolean flag = false; | 
 |  |  |                 List<byte[]> list = new ArrayList<>(); | 
 |  |  |                 //验证扇区密码,否则会报错(链接失败错误) | 
 |  |  |                 boolean isOpen = mfc.authenticateSectorWithKeyA(1, companyKey); | 
 |  |  |                 if (!isOpen) { | 
 |  |  |                     isOpen = mfc.authenticateSectorWithKeyA(1, defauleKey); | 
 |  |  |                 } | 
 |  |  |                 if (isOpen) { | 
 |  |  |                     //获取扇区里面块的数量 | 
 |  |  |                     int bCount = mfc.getBlockCountInSector(1); | 
 |  |  |                     //获取扇区第一个块对应芯片存储器的位置(我是这样理解的,因为第0扇区的这个值是4而不是0) | 
 |  |  |                     int bIndex = mfc.sectorToBlock(1); | 
 |  |  |                     //String data1 = ""; | 
 |  |  |                     for (int j = 0; j < bCount; j++) { | 
 |  |  |                         //读取数据 | 
 |  |  |                         byte[] data = null; | 
 |  |  |                         try { | 
 |  |  |                             mfc.close(); | 
 |  |  |                             data = mfc.readBlock(bIndex); | 
 |  |  |                             bIndex++; | 
 |  |  |                             list.add(data); | 
 |  |  |                         } catch (Exception e) { | 
 |  |  |                             e.printStackTrace(); | 
 |  |  |                         } | 
 |  |  |                     } | 
 |  |  |                     flag = true; | 
 |  |  |                 } | 
 |  |  |                 if (flag) { | 
 |  |  |                     callback.callBack(list); | 
 |  |  |                 } else { | 
 |  |  |                     callback.error(MyCommon.ERROR); | 
 |  |  |                 } | 
 |  |  |             } catch (IOException e) { | 
 |  |  |                 callback.error(MyCommon.ERROR_MOVE); | 
 |  |  |                 e.printStackTrace(); | 
 |  |  |             } catch (Exception e) { | 
 |  |  |                 callback.error(MyCommon.ERROR); | 
 |  |  |                 e.printStackTrace(); | 
 |  |  |             } finally { | 
 |  |  |  | 
 |  |  |                 try { | 
 |  |  |                     mfc.close(); | 
 |  |  |                 } catch (Exception e) { | 
 |  |  |                     e.printStackTrace(); | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         }).start(); | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 读取NFC卡的特定扇区信息 | 
 |  |  | 
 |  |  |                                 break; | 
 |  |  |                         } | 
 |  |  |                         boolean isOpen = mfc.authenticateSectorWithKeyA(a, defauleKey); | 
 |  |  |                         if (!isOpen){ | 
 |  |  |                         if (!isOpen) { | 
 |  |  |                             isOpen = mfc.authenticateSectorWithKeyA(a, companyKey); | 
 |  |  |                         } | 
 |  |  |                         if (isOpen) { | 
 |  |  | 
 |  |  |     /** | 
 |  |  |      * 返回监听类 | 
 |  |  |      */ | 
 |  |  |     public interface NFCCallListback { | 
 |  |  |     public interface NFCCallMapback { | 
 |  |  |         /** | 
 |  |  |          * 返回读取nfc卡的全部信息 | 
 |  |  |          * | 
 |  |  | 
 |  |  |         void error(int code); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 返回监听类 | 
 |  |  |      */ | 
 |  |  |     public interface NFCCallListback { | 
 |  |  |         /** | 
 |  |  |          * 返回读取nfc卡的全部信息 | 
 |  |  |          * | 
 |  |  |          * @param data 前面代表扇区 四个块的数据用#号隔开 | 
 |  |  |          */ | 
 |  |  |         void callBack(List<byte[]> data); | 
 |  |  |  | 
 |  |  |  | 
 |  |  |         void error(int code); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     public interface NFCCallByteback { | 
 |  |  |         /** | 
 |  |  |          * 返回读取nfc卡的全部信息 |