The previous example describes the general steps for detecting and reading and writing NFC tags. This example describes the common Mifare tags.
The Mifare Tag can be 1 K, 2 K, and 4 K. The memory partition is similar, and the memory distribution of the 1 K-byte Tag is given:
Data is divided into 16 segments, each with 4 blocks. Each Block can store 16 bytes of data. The size is 16X4X16 = 1024 bytes.
The last Block in each zone is called A Trailer. It is mainly used to store the keys used to read and write Block data in the zone. It can have two keys A and B, each of which is 6 bytes in length, the default Key value is generally full FF or 0. by MifareClassic. KEY_DEFAULT definition.
Therefore, the Mifare Tag must be read and written with the correct Key value (for protection). If authentication succeeds
Auth = mfc. authenticateSectorWithKeyA (j,
MifareClassic. KEY_DEFAULT );
Then you can read and write the data in the zone.
This example defines several Mifare-related classes MifareClassCard, MifareSector, MifareBlock, and MifareKey to facilitate Mifare Tag reading and writing.
The Android system detects NFC tags, encapsulates them into Tag classes, and stores them in the Intent NfcAdapter. EXTRA_TAG Extra data packet. You can use MifareClassic. get (Tag) to obtain the MifareClassic class of the object.
[Java]
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 );
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 );
The following code reads Mifare card:
[Java]
// 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. debuuplint ();
}
}
} // End of method
// 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. debuuplint ();
}
}
} // End of method
Download this example: http://www.bkjia.com/uploadfile/2012/0517/20120517093850748.zip
Excerpted from the mobile app