Android NFC開發(fā)教程: Mifare Tag讀寫示例
本例針對常用的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值(起到保護的作用),如果鑒權成功:
- 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類。
- Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
- // 4) Get an instance of the Mifare classic card from this TAG
- // intent MifareClassic mfc = MifareClassic.get(tagFromIntent);
下面為讀取Mifare card 的主要代碼:
- // 1) Parse the intent and get the action that triggered this intent
- String action = intent.getAction();
- // 2) Check if it was triggered by a tag discovered interruption.
- if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {
- // 3) Get an instance of the TAG from the NfcAdapter
- Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
- // 4) Get an instance of the Mifare classic card from this TAG
- // intent
- MifareClassic mfc = MifareClassic.get(tagFromIntent);
- MifareClassCard mifareClassCard=null;
- try { // 5.1) Connect to card
- mfc.connect();
- boolean auth = false;
- // 5.2) and get the number of sectors this card has..and loop
- // thru these sectors
- int secCount = mfc.getSectorCount();
- mifareClassCard= new MifareClassCard(secCount);
- int bCount = 0;
- int bIndex = 0;
- for (int j = 0; j < secCount; j++) {
- MifareSector mifareSector = new MifareSector();
- mifareSector.sectorIndex = j;
- // 6.1) authenticate the sector
- auth = mfc.authenticateSectorWithKeyA(j,
- MifareClassic.KEY_DEFAULT);
- mifareSector.authorized = auth;
- if (auth) {
- // 6.2) In each sector - get the block count
- bCount = mfc.getBlockCountInSector(j);
- bCount =Math.min(bCount, MifareSector.BLOCKCOUNT);
- bIndex = mfc.sectorToBlock(j);
- for (int i = 0; i < bCount; i++) {
- // 6.3) Read the block
- byte []data = mfc.readBlock(bIndex);
- MifareBlock mifareBlock = new MifareBlock(data);
- mifareBlock.blockIndex = bIndex;
- // 7) Convert the data into a string from Hex
- // format.
- bIndex++;
- mifareSector.blocks<i> = mifareBlock;
- }
- mifareClassCard.setSector(mifareSector.sectorIndex,
- mifareSector);
- } else { // Authentication failed - Handle it
- }
- }
- ArrayList<String> blockData=new ArrayList<String>();
- int blockIndex=0;
- for(int i=0;i<secCount;i++){
- MifareSector mifareSector=mifareClassCard.getSector(i);
- for(int j=0;j<MifareSector.BLOCKCOUNT;j++){
- MifareBlock mifareBlock=mifareSector.blocks[j];
- byte []data=mifareBlock.getData();
- blockData.add("Block "+ blockIndex++ +" : "+
- Converter.getHexString(data, data.length));
- }
- }
- String []contents=new String[blockData.size()];
- blockData.toArray(contents);
- setListAdapter(new ArrayAdapter<String>(this,
- android.R.layout.simple_list_item_1, contents));
- getListView().setTextFilterEnabled(true);
- } catch (IOException e) {
- Log.e(TAG, e.getLocalizedMessage());
- showAlert(3);
- }finally{
- if(mifareClassCard!=null){
- mifareClassCard.debugPrint();
- }
- }
- }
運行結果: