Http://www.cnblogs.com/haoxinyue/archive/2012/05/03/2479599.html
Let's take a look at the mifareclassic protocol.
In the android SDK documentationMifareClassic
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
."
Therefore, the nfca protocol is compatible with the mifareclassic protocol. We can use the nfca class in Android to process the rfidcard assigned to mifareclassic.
Generally, the mifareclassic RF Card has three memory sizes:
1 K: 16 partitions (sector). Each partition has 4 blocks and each block has 16 bytes of data.
2 K: 32 partitions. Each partition has 4 blocks and each block has 16 bytes of data.
4 K: 64 partitions. Each partition has 4 blocks and each block has 16 bytes of data.
For all mifareclassic-based cards, the last block in each zone is trailer and 16 bytes are used to store the key in the read/write area. There can be two keys A and B, each key is 6 bytes long. The default key is generally FF or 0. The memory structure of the last block is as follows:
Block 0 Data 16 bytesblock 1 Data 16 bytesblock 2 Data 16 bytesblock 3 trailer 16 bytestrailer: Key A: 6 bytesaccess conditions: 4 byteskey B: 6 bytes so when writing the card's memory, generally, you cannot write the last block of each sector unless you need to modify the key and access permissions. If you accidentally modify key A, but you do not know what it is, you cannot access the corresponding sector. In mifareclassic, if you want to read data, you must have the permission of the sector where the data address is located. This permission is the Keya or key B of the trailer of the sector. Example of reading data:
// Tag is the tag obtained in onnewintent in the previous article.
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(); } }
Example of writing data:
// Tag is the tagmifareclassic MC = mifareclassic obtained in onnewintent in the previous article. 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 ();}}}
Download the complete sample code here