轉自:http://www.cnblogs.com/haoxinyue/archive/2012/05/03/2479599.html
先了解一下MifareClassic協議
在android sdk 的文檔中,描述道 “all MifareClassic
I/O operations will be supported, andMIFARE_CLASSIC
NDEF
tags will also be supported. In either case,NfcA
will also be enumerated on the tag, because
all MIFARE Classic tags are alsoNfcA
.” 所以說NFCA協議是兼容MifareClassic 協議的, 我們可以通過NfcA在android的相關類來處理給予MifareClassic
的RFID卡。
一般來說,給予MifareClassic的射頻卡,一般內存大小有3種:
1K: 16個分區(sector),每個分區4個塊(block),每個塊(block) 16個byte數據
2K: 32個分區,每個分區4個塊(block),每個塊(block) 16個byte數據
4K:64個分區,每個分區4個塊(block),每個塊(block) 16個byte數據
對於所有基於MifareClassic的卡來說,每個區最後一個塊叫Trailer,16個byte, 主要來存放讀寫該區的key,可以有A,B兩個KEY,每個key長6byte,默認的key一般是FF 或 0,最後一個塊的內存結構如下:
//tag 就是在上一篇中onNewIntent中獲取的tag
MifareClassic mc = MifareClassic.get(tag); short startAddress = 0; short endAddress = 5; byte[] data = new byte[(endAddress - startAddress + 1 ) * ByteCountPerBlock]; try { mc.connect();for (short i = startAddress; i <= endAddress; i++ ,time++) { boolean auth = false; short sectorAddress = getSectorAddress(i); auth = mc.authenticateSectorWithKeyA(sectorAddress, MifareClassic.KEY_DEFAULT); if (auth){ //the last block of the sector is used for KeyA and KeyB cannot be overwritted short readAddress = (short)(sectorAddress == 0 ? i : i + sectorAddress); byte[] response = mc.readBlock(readAddress); CombineByteArray(data, response, time * ByteCountPerBlock); } else{ throw new NfcException(NfcErrorCode.TemporaryError, "Authorization Error."); } } mc.close(); } catch (NfcException ne) { throw ne; } catch (IOException e) { throw new NfcException(NfcErrorCode.TemporaryError, "Get response, what it is not successfully.", e); } finally { try { mc.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
//tag 就是在上一篇中onNewIntent中獲取的tag MifareClassic mc = MifareClassic.get(tag); try { mc.connect(); boolean auth = false; short sectorAddress = 0 auth = mc.authenticateSectorWithKeyA(sectorAddress, MifareClassic.KEY_DEFAULT); if (auth) { //the last block of the sector is used for KeyA and KeyB cannot be overwritted mc.writeBlock(readAddress, dataTemp); mc.close(); } }finally { try { mc.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }