// 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();
}
}
}// End of method