AndroidNFC开发教程:MifareTag读写示例

本例针对常用的Mifare Tag具体说明。

Mifare Tag 可以有1K ,2K, 4K,其内存分区大同小异,下图给出了1K字节容量的Tag的内存分布:

数据分为16个区(Sector) ,每个区有4个块(Block) ,每个块可以存放16字节的数据,其大小为16 X 4 X 16 =1024 bytes。

每个区***一个块称为Trailer ,主要用来存放读写该区Block数据的Key ,可以有A,B两个Key,每个Key 长度为6个字节,缺省的Key值一般为全FF或是0. 由 MifareClassic.KEY_DEFAULT 定义。

因此读写Mifare Tag 首先需要有正确的Key值(起到保护的作用),如果鉴权成功:

 
 
 
  1. auth = mfc.authenticateSectorWithKeyA(j, MifareClassic.KEY_DEFAULT); 

然后才可以读写该区数据。

本例定义几个Mifare相关的类 MifareClassCard ,MifareSector, MifareBlock 和MifareKey 以方便读写Mifare Tag.

Android 系统来检测到NFC Tag, 将其封装成Tag类,存放到Intent的NfcAdapter.EXTRA_TAG Extra 数据包中,可以使用MifareClassic.get(Tag) 获取对象的 MifareClassic类。

 
 
 
  1. Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); 
  2. // 4) Get an instance of the Mifare classic card from this TAG 
  3. // intent MifareClassic mfc = MifareClassic.get(tagFromIntent);  

下面为读取Mifare card 的主要代码:

 
 
 
  1. // 1) Parse the intent and get the action that triggered this intent  
  2. String action = intent.getAction();  
  3. // 2) Check if it was triggered by a tag discovered interruption.  
  4. if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {  
  5. // 3) Get an instance of the TAG from the NfcAdapter  
  6. Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);  
  7. // 4) Get an instance of the Mifare classic card from this TAG  
  8. // intent  
  9. MifareClassic mfc = MifareClassic.get(tagFromIntent);  
  10. MifareClassCard mifareClassCard=null;  
  11.   
  12. try { // 5.1) Connect to card  
  13. mfc.connect();  
  14. boolean auth = false;  
  15. // 5.2) and get the number of sectors this card has..and loop  
  16. // thru these sectors  
  17. int secCount = mfc.getSectorCount();  
  18. mifareClassCard= new MifareClassCard(secCount);  
  19. int bCount = 0;  
  20. int bIndex = 0;  
  21. for (int j = 0; j < secCount; j++) {  
  22. MifareSector mifareSector = new MifareSector();  
  23. mifareSector.sectorIndex = j;  
  24. // 6.1) authenticate the sector  
  25. auth = mfc.authenticateSectorWithKeyA(j,  
  26. MifareClassic.KEY_DEFAULT);  
  27. mifareSector.authorized = auth;  
  28. if (auth) {  
  29. // 6.2) In each sector - get the block count  
  30. bCount = mfc.getBlockCountInSector(j);  
  31. bCount =Math.min(bCount, MifareSector.BLOCKCOUNT);  
  32. bIndex = mfc.sectorToBlock(j);  
  33. for (int i = 0; i < bCount; i++) {  
  34.   
  35. // 6.3) Read the block  
  36. byte []data = mfc.readBlock(bIndex);  
  37. MifareBlock mifareBlock = new MifareBlock(data);  
  38. mifareBlock.blockIndex = bIndex;  
  39. // 7) Convert the data into a string from Hex  
  40. // format.  
  41.   
  42. bIndex++;  
  43. mifareSector.blocks = mifareBlock;  
  44.   
  45. }  
  46. mifareClassCard.setSector(mifareSector.sectorIndex,  
  47. mifareSector);  
  48. } else { // Authentication failed - Handle it  
  49.   
  50. }  
  51. }  
  52. ArrayList blockData=new ArrayList();  
  53. int blockIndex=0;  
  54. for(int i=0;i
  55.   
  56. MifareSector mifareSector=mifareClassCard.getSector(i);  
  57. for(int j=0;j
  58. MifareBlock mifareBlock=mifareSector.blocks[j];  
  59. byte []data=mifareBlock.getData();  
  60. blockData.add("Block "+ blockIndex++ +" : "+  
  61. Converter.getHexString(data, data.length));  
  62. }  
  63. }  
  64. String []contents=new String[blockData.size()];  
  65. blockData.toArray(contents);  
  66. setListAdapter(new ArrayAdapter(this,  
  67. android.R.layout.simple_list_item_1, contents));  
  68. getListView().setTextFilterEnabled(true);  
  69.   
  70. } catch (IOException e) {  
  71. Log.e(TAG, e.getLocalizedMessage());  
  72. showAlert(3);  
  73. }finally{  
  74.   
  75. if(mifareClassCard!=null){  
  76. mifareClassCard.debugPrint();  
  77. }  
  78. }  

运行结果:

网站栏目:AndroidNFC开发教程:MifareTag读写示例
本文URL:http://www.hantingmc.com/qtweb/news20/391120.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联