本篇文章為大家展示了Android NFC開發(fā)中Mifare Tag讀寫的示例分析,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
成都創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、莘縣網(wǎng)絡(luò)推廣、小程序開發(fā)、莘縣網(wǎng)絡(luò)營(yíng)銷、莘縣企業(yè)策劃、莘縣品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);成都創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供莘縣建站搭建服務(wù),24小時(shí)服務(wù)熱線:13518219792,官方網(wǎng)址:muchs.cn
針對(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)成功:
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類。
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)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)站名稱:AndroidNFC開發(fā)中MifareTag讀寫的示例分析
網(wǎng)站地址:http://muchs.cn/article34/pisgpe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、服務(wù)器托管、標(biāo)簽優(yōu)化、手機(jī)網(wǎng)站建設(shè)、網(wǎng)站導(dǎo)航、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)