AndroidStudioNFC读写卡程序

人生必须的知识就是引人向光明方面的明灯。这篇文章主要讲述AndroidStudioNFC读写卡程序 相关的知识,希望能为你提供帮助。
上一章简单的介绍了 一下NFC android 中的认识 和配置,这次认识一下NFC 卡片。
卡片一共 分为16个扇区(sector),每个扇区又分为4个块(block) 每个块 包含16个字节,
 
比如“ 1234567890123456” , “ ASDFGHJKLQWERTYU” ,可以存取一些信息,
因为卡片的类型不同,所以在存取的时候需要注意一下,
比如 对于一般的 MifareClassic 来说,  第一扇区 第一块 一般会被厂家占用,这里是不能被写入数据的,每一扇区的 最后一块,也就是第四block,是用来存放密码或控制位的,其余的三个块是数据区,这里可以存放我们的数据。
而对于  MifareUltralight 来说, 一般 前四扇区 是不能够 写数据的,而且没有密码。
 
当你在第几扇区第几块中写的数据,就需要 在第几扇区第几块中读出来。
写数据流程为: data (符合要求的16位) 

public void writeTag(Tag tag) {MifareClassic mfc = MifareClassic.get(tag); try { mfc.connect(); boolean auth = false; short sectorAddress = 1; auth = mfc.authenticateSectorWithKeyA(sectorAddress, MifareClassic.KEY_DEFAULT); if (auth) { // the last block of the sector is used for KeyA and KeyB cannot be overwritted mfc.writeBlock(4, "1313838438000000".getBytes()); mfc.writeBlock(5, "ASDFGHJKLl000000".getBytes()); mfc.close(); Toast.makeText(this, "写入成功", Toast.LENGTH_SHORT).show(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { mfc.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace();

我们把数据写入了第一扇区的第四和第五块,
然后从第四区和第五区读出来
public String readTag(Tag tag) { MifareClassic mfc = MifareClassic.get(tag); for (String tech : tag.getTechList()) { System.out.println(tech); } boolean auth = false; //读取TAGtry { String metaInfo = ""; //Enable I/O operations to the tag from this TagTechnology object. mfc.connect(); int type = mfc.getType(); //获取TAG的类型 int sectorCount = mfc.getSectorCount(); //获取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; } metaInfo += "卡片类型:" + typeS + "\n共" + sectorCount + "个扇区\n共" + mfc.getBlockCount() + "个块\n存储空间: " + mfc.getSize() + "B\n"; for (int j = 0; j < sectorCount; j++) { //Authenticate a sector with key A. auth = mfc.authenticateSectorWithKeyA(j, MifareClassic.KEY_NFC_FORUM); int bCount; int bIndex; if (auth) { metaInfo += "Sector " + j + ":验证成功\n"; // 读取扇区中的块 bCount = mfc.getBlockCountInSector(j); bIndex = mfc.sectorToBlock(j); for (int i = 0; i < bCount; i++) { byte[] data = https://www.songbingjia.com/android/mfc.readBlock(bIndex); metaInfo +="Block " + bIndex + " : " + bytesToHexString(data) + "\n"; bIndex++; } } else { metaInfo += "Sector " + j + ":验证失败\n"; } } return metaInfo; } catch (Exception e) { Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show(); e.printStackTrace(); } finally { if (mfc != null) { try { mfc.close(); } catch (IOException e) { Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG) .show(); } } } return null;


【AndroidStudioNFC读写卡程序】上面是把 卡片的所有信息 读出来,如果需要第四和第五的,自己可以改动取值。
因为存在卡片中的为16进制(不是16进制字符串),所以需要 先转为16进制字符串,然后再转为自己用的字符串就可以了
 
代码就不贴了,网上一搜一大把,关键是 认识 自己需要什么样的 类型,不要用错了
 

    推荐阅读