Android NFC開(kāi)發(fā)教程: Mifare Tag讀寫(xiě)示例
本例針對(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è)塊稱(chēng)為T(mén)railer ,主要用來(lái)存放讀寫(xiě)該區(qū)Block數(shù)據(jù)的Key ,可以有A,B兩個(gè)Key,每個(gè)Key 長(zhǎng)度為6個(gè)字節(jié),缺省的Key值一般為全FF或是0. 由 MifareClassic.KEY_DEFAULT 定義。
因此讀寫(xiě)Mifare Tag 首先需要有正確的Key值(起到保護(hù)的作用),如果鑒權(quán)成功:
- auth = mfc.authenticateSectorWithKeyA(j, MifareClassic.KEY_DEFAULT);
然后才可以讀寫(xiě)該區(qū)數(shù)據(jù)。
本例定義幾個(gè)Mifare相關(guān)的類(lèi) MifareClassCard ,MifareSector, MifareBlock 和MifareKey 以方便讀寫(xiě)Mifare Tag.
Android 系統(tǒng)來(lái)檢測(cè)到NFC Tag, 將其封裝成Tag類(lèi),存放到Intent的NfcAdapter.EXTRA_TAG Extra 數(shù)據(jù)包中,可以使用MifareClassic.get(Tag) 獲取對(duì)象的 MifareClassic類(lèi)。
- 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();
- }
- }
- }
運(yùn)行結(jié)果:






















