自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Android NFC開發(fā)教程: Mifare Tag讀寫示例

移動開發(fā) Android
本文介紹Mifare Tag的具體規(guī)格,和如何利用Android SDK中的NFC包來讀寫Mifare Tag中的數(shù)據。

本例針對常用的Mifare Tag具體說明。

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

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

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

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

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

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

本例定義幾個Mifare相關的類 MifareClassCard ,MifareSector, MifareBlock 和MifareKey 以方便讀寫Mifare Tag.

Android 系統(tǒng)來檢測到NFC Tag, 將其封裝成Tag類,存放到Intent的NfcAdapter.EXTRA_TAG Extra 數(shù)據包中,可以使用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<i> = mifareBlock;  
  44.   
  45. }  
  46. mifareClassCard.setSector(mifareSector.sectorIndex,  
  47. mifareSector);  
  48. else { // Authentication failed - Handle it  
  49.   
  50. }  
  51. }  
  52. ArrayList<String> blockData=new ArrayList<String>();  
  53. int blockIndex=0;  
  54. for(int i=0;i<secCount;i++){  
  55.   
  56. MifareSector mifareSector=mifareClassCard.getSector(i);  
  57. for(int j=0;j<MifareSector.BLOCKCOUNT;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<String>(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. }  

運行結果:

 

責任編輯:徐川 來源: eoeAndroid
相關推薦

2013-01-23 14:33:04

Android開發(fā)NFC

2013-12-27 14:34:46

Android開發(fā)Android應用短信觸發(fā)示例

2011-10-11 10:33:06

RIM黑莓NFC

2013-12-27 13:49:22

Android開發(fā)Android應用Button

2013-12-27 12:51:44

Android開發(fā)Android應用引路蜂

2011-08-22 12:01:38

iPhone開發(fā)文件

2023-12-22 09:11:45

AndroidNFC移動開發(fā)

2013-01-23 13:35:46

AndroidNFC學生卡

2010-12-23 09:11:17

讀寫Android文件

2015-01-26 13:16:30

NFCAndroid

2015-02-26 14:17:45

2016-11-14 15:40:01

Android

2011-09-07 13:00:36

2011-09-13 17:15:58

Eclipse And

2011-09-14 10:52:39

Android 2.2

2014-08-26 11:46:46

QtAndroid實例教程

2013-12-26 15:18:09

Android開發(fā)安裝開發(fā)環(huán)境

2010-10-13 12:12:19

網絡安全身份驗證HID Global

2013-12-26 15:43:07

Android開發(fā)Android應用Activities

2011-12-30 15:11:36

Adobe視頻PhoneGap
點贊
收藏

51CTO技術棧公眾號