|  |  | 
 |  |  | import android.util.Log; | 
 |  |  |  | 
 |  |  | import com.dayu.baselibrary.tools.HexUtil; | 
 |  |  | import com.dayu.baselibrary.utils.BaseCommon; | 
 |  |  | import com.dayu.qihealonelibrary.card.UserCard; | 
 |  |  | import com.dayu.qihealonelibrary.utils.MyCommon; | 
 |  |  | import com.dayu.qihealonelibrary.utils.CardCommon; | 
 |  |  |  | 
 |  |  |  | 
 |  |  | import java.io.IOException; | 
 |  |  | import java.util.ArrayList; | 
 |  |  | import java.util.Arrays; | 
 |  |  | import java.util.HashMap; | 
 |  |  | import java.util.List; | 
 |  |  | import java.util.Map; | 
 |  |  | 
 |  |  |         return null; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public String getCradType() { | 
 |  |  |     /** | 
 |  |  |      * 获取卡号 | 
 |  |  |      * | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     public String getCardNumber() { | 
 |  |  |         MifareClassic mfc = MifareClassic.get(tag); | 
 |  |  |         if (null != mfc) { | 
 |  |  |             try { | 
 |  |  |                 mfc.connect(); | 
 |  |  |                 //获取当前卡号 | 
 |  |  |                 boolean isOpen = mfc.authenticateSectorWithKeyA(0, defauleKey); | 
 |  |  |                 if (!isOpen) { | 
 |  |  |                     isOpen = mfc.authenticateSectorWithKeyA(0, companyKey); | 
 |  |  |                 } | 
 |  |  |                 if (isOpen) { | 
 |  |  |                     int bIndex = mfc.sectorToBlock(0); | 
 |  |  |                     byte[] data = mfc.readBlock(bIndex + 0); | 
 |  |  |                     if (data != null && data.length > 0) { | 
 |  |  |                         String hex = HexUtil.bytesToHex(Arrays.copyOfRange(data, 0, 4)); | 
 |  |  | //                        hex = HexUtil.spaceHex(hex); | 
 |  |  | //                        hex = HexUtil.HighLowHex(hex); | 
 |  |  |                         Log.i("NFCWreatActivity", "hex===" + hex); | 
 |  |  |                         return hex.toUpperCase(); | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |  | 
 |  |  |             } catch (IOException e) { | 
 |  |  |                 return BaseCommon.CARD_TYPE_ERROR1; | 
 |  |  |             } finally { | 
 |  |  |                 try { | 
 |  |  |                     mfc.close(); | 
 |  |  |                 } catch (IOException e) { | 
 |  |  |                     e.printStackTrace(); | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         return ""; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 获取卡片类型和卡号 | 
 |  |  |      * | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     public String getCradTypeAndCardNumber() { | 
 |  |  |  | 
 |  |  |         MifareClassic mfc = MifareClassic.get(tag); | 
 |  |  |         if (null != mfc) { | 
 |  |  |             try { | 
 |  |  |                 mfc.connect(); | 
 |  |  |                 boolean isOpen = mfc.authenticateSectorWithKeyA(1, defauleKey); | 
 |  |  |                 StringBuilder strData = new StringBuilder(); | 
 |  |  |                 //获取当前卡号 | 
 |  |  |                 boolean isOpen = mfc.authenticateSectorWithKeyA(0, defauleKey); | 
 |  |  |                 if (!isOpen) { | 
 |  |  |                     isOpen = mfc.authenticateSectorWithKeyA(0, companyKey); | 
 |  |  |                 } | 
 |  |  |                 if (isOpen) { | 
 |  |  |                     int bIndex = mfc.sectorToBlock(0); | 
 |  |  |                     byte[] data = mfc.readBlock(bIndex + 0); | 
 |  |  |                     if (data != null && data.length > 0) { | 
 |  |  |                         String hex = HexUtil.bytesToHex(Arrays.copyOfRange(data, 0, 4)); | 
 |  |  |                         strData.append(hex); | 
 |  |  |                         strData.append(","); | 
 |  |  |                         Log.i("NFCWreatActivity", "hex===" + hex); | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |                 //获取卡片类型 | 
 |  |  |                 isOpen = mfc.authenticateSectorWithKeyA(1, defauleKey); | 
 |  |  |                 if (!isOpen) { | 
 |  |  |                     isOpen = mfc.authenticateSectorWithKeyA(1, companyKey); | 
 |  |  |                 } | 
 |  |  | 
 |  |  |                     byte[] data = mfc.readBlock(bIndex + 0); | 
 |  |  |                     if (data != null && data.length > 0) { | 
 |  |  |                         String hex = HexUtil.byteToHex(data[0]); | 
 |  |  |                         strData.append(hex); | 
 |  |  |                         Log.i("NFCWreatActivity", "hex===" + hex); | 
 |  |  |                         return hex; | 
 |  |  |                         return strData.toString().toUpperCase(); | 
 |  |  |                     } | 
 |  |  |  | 
 |  |  |                 } else { | 
 |  |  |                     Log.i("NFCWreatActivity", "isOpen===" + isOpen); | 
 |  |  |                     return "-1"; | 
 |  |  |                     return BaseCommon.CARD_TYPE_ERROR2; | 
 |  |  |                 } | 
 |  |  |             } catch (IOException e) { | 
 |  |  |                 return "-1"; | 
 |  |  |                 return BaseCommon.CARD_TYPE_ERROR1; | 
 |  |  |             } finally { | 
 |  |  |                 try { | 
 |  |  |                     mfc.close(); | 
 |  |  | 
 |  |  |                 if (flag) { | 
 |  |  |                     callback.callBack(map); | 
 |  |  |                 } else { | 
 |  |  |                     callback.error(MyCommon.ERROR); | 
 |  |  |                     callback.error(CardCommon.ERROR); | 
 |  |  |                 } | 
 |  |  |             } catch (IOException e) { | 
 |  |  |                 callback.error(MyCommon.ERROR_MOVE); | 
 |  |  |                 callback.error(CardCommon.ERROR_MOVE); | 
 |  |  |                 e.printStackTrace(); | 
 |  |  |             } catch (Exception e) { | 
 |  |  |                 callback.error(MyCommon.ERROR); | 
 |  |  |                 callback.error(CardCommon.ERROR); | 
 |  |  |                 e.printStackTrace(); | 
 |  |  |             } finally { | 
 |  |  |                 try { | 
 |  |  | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     public List<byte[]> getOnesectorData() { | 
 |  |  |         MifareClassic mfc = null; | 
 |  |  |         try { | 
 |  |  |             mfc = MifareClassic.get(tag); | 
 |  |  |         } catch (Exception e) { | 
 |  |  |             e.printStackTrace(); | 
 |  |  |         } | 
 |  |  |         List<byte[]> list = new ArrayList<>(); | 
 |  |  |         if (null != mfc) { | 
 |  |  |             try { | 
 |  |  |                 //链接NFC | 
 |  |  |                 mfc.connect(); | 
 |  |  |                 //用于判断时候有内容读取出来 | 
 |  |  |                 boolean flag = false; | 
 |  |  |  | 
 |  |  |                 //验证扇区密码,否则会报错(链接失败错误) | 
 |  |  |                 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 { | 
 |  |  |                             data = mfc.readBlock(bIndex); | 
 |  |  |                             bIndex++; | 
 |  |  |                             list.add(data); | 
 |  |  |                         } catch (Exception e) { | 
 |  |  |                             e.printStackTrace(); | 
 |  |  |                         } | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |  | 
 |  |  |             } catch (IOException e) { | 
 |  |  |                 e.printStackTrace(); | 
 |  |  |             } catch (Exception e) { | 
 |  |  |                 e.printStackTrace(); | 
 |  |  |             } finally { | 
 |  |  |                 try { | 
 |  |  |                     mfc.close(); | 
 |  |  |                     return list; | 
 |  |  |                 } catch (Exception e) { | 
 |  |  |                     e.printStackTrace(); | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         return null; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 获取1扇区信息 | 
 |  |  | 
 |  |  |                 if (flag) { | 
 |  |  |                     callback.callBack(list); | 
 |  |  |                 } else { | 
 |  |  |                     callback.error(MyCommon.ERROR); | 
 |  |  |                     callback.error(CardCommon.ERROR); | 
 |  |  |                 } | 
 |  |  |             } catch (IOException e) { | 
 |  |  |                 callback.error(MyCommon.ERROR_MOVE); | 
 |  |  |                 callback.error(CardCommon.ERROR_MOVE); | 
 |  |  |                 e.printStackTrace(); | 
 |  |  |             } catch (Exception e) { | 
 |  |  |                 callback.error(MyCommon.ERROR); | 
 |  |  |                 callback.error(CardCommon.ERROR); | 
 |  |  |                 e.printStackTrace(); | 
 |  |  |             } finally { | 
 |  |  |  | 
 |  |  | 
 |  |  |                         mfc.connect(); | 
 |  |  |                         int count = mfc.getSectorCount(); | 
 |  |  |                         if (a < 0 || a > count - 1) { | 
 |  |  |                             callback.error(MyCommon.ERROR); | 
 |  |  |                             callback.error(CardCommon.ERROR); | 
 |  |  |                             return; | 
 |  |  |                         } | 
 |  |  |                         int bCount = mfc.getBlockCountInSector(a); | 
 |  |  |                         if (b < 0 || b > bCount - 1) { | 
 |  |  |                             callback.error(MyCommon.ERROR); | 
 |  |  |                             callback.error(CardCommon.ERROR); | 
 |  |  |                             return; | 
 |  |  |                         } | 
 |  |  |  | 
 |  |  | 
 |  |  |                             byte[] data = mfc.readBlock(bIndex + b); | 
 |  |  |                             callback.callBack(data); | 
 |  |  |                         } else { | 
 |  |  |                             callback.error(MyCommon.ERROR); | 
 |  |  |                             callback.error(CardCommon.ERROR); | 
 |  |  |                         } | 
 |  |  |                     } catch (IOException e) { | 
 |  |  |                         callback.error(MyCommon.ERROR_MOVE); | 
 |  |  |                         callback.error(CardCommon.ERROR_MOVE); | 
 |  |  |                         e.printStackTrace(); | 
 |  |  |                     } catch (Exception e) { | 
 |  |  |                         callback.error(MyCommon.ERROR); | 
 |  |  |                         callback.error(CardCommon.ERROR); | 
 |  |  |                     } finally { | 
 |  |  |                         try { | 
 |  |  |                             mfc.close(); |