您好,登錄后才能下訂單哦!
本篇文章為大家展示了Android NFC開發(fā)中Mifare Tag讀寫的示例分析,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
針對(duì)常用的Mifare Tag具體說明。
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 ,主要用來存放讀寫該區(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)成功:
auth = mfc.authenticateSectorWithKeyA(j, MifareClassic.KEY_DEFAULT);
然后才可以讀寫該區(qū)數(shù)據(jù)。
本例定義幾個(gè)Mifare相關(guān)的類 MifareClassCard ,MifareSector, MifareBlock 和MifareKey 以方便讀寫Mifare Tag.
Android 系統(tǒng)來檢測(cè)到NFC Tag, 將其封裝成Tag類,存放到Intent的NfcAdapter.EXTRA_TAG Extra 數(shù)據(jù)包中,可以使用MifareClassic.get(Tag) 獲取對(duì)象的 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(); } } }
運(yùn)行結(jié)果:
上述內(nèi)容就是Android NFC開發(fā)中Mifare Tag讀寫的示例分析,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。