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,最後一個塊的記憶體結構如下:
Block 0 Data 16bytesBlock 1 Data 16 bytesBlock 2 Data 16 bytesBlock 3 Trailer 16 bytesTrailer:Key A: 6 bytesAccess Conditions: 4 bytesKey B: 6 bytes 所以在寫卡的記憶體的時候,一般不能寫每個sector的最後一個block,除非你有要修改KEY和存取權限的需求。如果KEY A 被你不小心修改掉了,而你不知道修改成什麼,那與之對應的那個sector你就沒有辦法訪問了。因為在MifareClassic中,如果你要讀取資料,那麼必須要有這個資料地址所在的sector的許可權,這個許可權就是這個sector的trailer的keyA或KEY B。讀資料的例子:
//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中擷取的tagMifareClassic 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(); } } }
完整的程式碼範例在這裡下載