| | |
| | | import android.nfc.Tag; |
| | | import android.nfc.tech.MifareClassic; |
| | | |
| | | import com.dayu.recharge.card.UserCard; |
| | | import com.dayu.recharge.utils.MyCommon; |
| | | |
| | | import java.io.IOException; |
| | |
| | | return helper; |
| | | } |
| | | |
| | | |
| | | public UserCard getUserCardData() { |
| | | UserCard userCard = null; |
| | | 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; |
| | | List<byte[]> list = new ArrayList<>(); |
| | | //验证扇区密码,否则会报错(链接失败错误) |
| | | boolean isOpen = mfc.authenticateSectorWithKeyA(1, bytes); |
| | | 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(); |
| | | } |
| | | } |
| | | flag = true; |
| | | } |
| | | |
| | | if (flag) { |
| | | userCard = UserCard.getBean(list); |
| | | return userCard; |
| | | } |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } finally { |
| | | try { |
| | | mfc.close(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 读取NFC卡的全部信息 |
| | | * |