日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
AndroidNFC開發(fā)教程:MifareTag讀寫示例

本例針對(duì)常用的Mifare Tag具體說(shuō)明。

Mifare Tag 可以有1K ,2K, 4K,其內(nèi)存分區(qū)大同小異,下圖給出了1K字節(jié)容量的Tag的內(nèi)存分布:

數(shù)據(jù)分為16個(gè)區(qū)(Sector) ,每個(gè)區(qū)有4個(gè)塊(Block) ,每個(gè)塊可以存放16字節(jié)的數(shù)據(jù),其大小為16 X 4 X 16 =1024 bytes。

每個(gè)區(qū)***一個(gè)塊稱為Trailer ,主要用來(lái)存放讀寫該區(qū)Block數(shù)據(jù)的Key ,可以有A,B兩個(gè)Key,每個(gè)Key 長(zhǎng)度為6個(gè)字節(jié),缺省的Key值一般為全FF或是0. 由 MifareClassic.KEY_DEFAULT 定義。

因此讀寫Mifare Tag 首先需要有正確的Key值(起到保護(hù)的作用),如果鑒權(quán)成功:

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

然后才可以讀寫該區(qū)數(shù)據(jù)。

本例定義幾個(gè)Mifare相關(guān)的類 MifareClassCard ,MifareSector, MifareBlock 和MifareKey 以方便讀寫Mifare Tag.

Android 系統(tǒng)來(lái)檢測(cè)到NFC Tag, 將其封裝成Tag類,存放到Intent的NfcAdapter.EXTRA_TAG Extra 數(shù)據(jù)包中,可以使用MifareClassic.get(Tag) 獲取對(duì)象的 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. }  

運(yùn)行結(jié)果:


當(dāng)前文章:AndroidNFC開發(fā)教程:MifareTag讀寫示例
瀏覽路徑:http://www.dlmjj.cn/article/coiepjp.html