A short explaination on using Android NFC API to read a MiFare Classic 1K Card. - Domnic

Tuesday, April 26, 2011

Reading a MiFare Classic 1K from Android using Nexus S.

Ever since Near Field Communication was embedded on mobile phones, loads of new ideas and business proposals made people very busy. So does the Android platform with its API's supporting NFC. Nexus S looks like a state of the art - good starting point if one wants to get past the monotonic Nokia's piece of the cake. I just want to share with you my experience on reading a MiFare Classic tag using the Nexus S..and the Android platform.

You need to have:
A MiFare Classic 1k Tag - ( hopefully you know the keys for its Blocks :=) )
Android SDK and IDE
Preferable a Nexus S (Make sure if the Android version is 2.3.3 and above).

Some Basics about the card:
MiFare classic cards store data in its Sectors. In MiFare classic 1k card there are 16 of them. Each Sector contains 4 blocks. You can store 16 bytes in each block. Making about 1024 bytes of storage space..that explains the 1K part of the card. You can perform common tasks like reading, writing data on these blocks, authentification, navigating the card sectors by incrementing the blocks count. The first sector contains manufacturer’s details and a unique id for the card. This is a read only part.

Each sector on the Mifare card is secured by two 48-bit keys: A and B. The last block in the sector contains these keys, as well as a configuration that defines what each key can do with each block, i.e block 0 could be configured so that
key A could read and write, but if a reader authenticates with key B, the reader would only be able to read that block.

The rest of the memory storage can be read or written using keys A and B. Fresh, empty Mifare cards have all their sectors locked with a pair of default keys FFFFFFFFFFFF or 000000000000.

About the NFC part of Android
Since ver 2.3.3 Gingerbread - Android exposes an API to read a list of card technologies. To perform operations on a tag, there are three things to be noted.

1) The cards store data in a format,
2) Reading and Writing data is done using a protocol
3) Cards support a technology that defines what they are

hence reading and writing to these cards can be done only when the data is arranged in that format. MiFare 1K cards support the NDEF format. It also supports NFC - protocol on the communication level. Precisely - ISO 14443 - 3A specification in short NFCA and it uses the MiFare technology.

Now we need to let the Android know what kind of cards we would be using in our application. This is often defined in an XML file stored in the resource folder ( I have named the file - filter_nfc.xml and stored it in a folder named xml). This resource file contains for example,

Here we have declared a tech-list. This list has to be used in the Manifest file. Imagine you would like to start an activity when a tag is touched. The Manifest file is the right place to let the launcher know what activity is to be called when a particular tag is touched.

In the Manifest file, you would have an element - activity. This would declare the name of the activity, a title for it and some metadata. Ideally you would let the system know that you want to start this activity when you touch a MiFare classic card. You can define you own filters for different activities for a variety of tag and protocol combinations.

When a MiFare tag is discovered, the NFC stack would get the details of the tag and deliver it to a new Intent of this same activity. Hence to handle this, we would need an instance of the PendingIntent from the current activity.

Finally when the pending intent calls the activity again, we like to read the tag. I have put all the steps in the method resolveIntent which would do only the reading part of the tag.

resolveIntent(intent);

Reading the tag
The method looks like

private void resolveIntent(Intent intent){
1) Parse the intent and get the action that triggered this intent
2) Check if it was triggered by a tag discovered interruption.
3) Get an instance of the TAG from the NfcAdapter
4) Get an instance of the Mifare classic card from this TAG instance
5) Connect to card and get the number of sectors this card has..and loop thru these sectors
6) In each sector - get the block count, loop thru the blocks, authenticate the sector and read the data
7) Convert the data into a string from Hex format.
}

filling up with the Android NFC API's

void resolveIntent(Intent intent) { // 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);
byte[] data;

you would have to think about how you like to handle the lifecycle of this activity. I Like to call the reading part of the activity when I detect a tag and if on pause.. would like to disable this. It has been very neatly explained in the Android samples on Foreground dispatch.. I have added this part of the code here too,

Hello Shekhar Josh,the ISO 14443 defines ways of storing and retriving data from MiFare or NFC. usually they are Hex bits on the low level. The snippet tries to do a simple bitshifting and masking in order to get these LSB and MSB using a table.

Thank you soo much. I been through the code . Though a lil beginner in the Java Programming . This is great and kool. I have run into a problem though.When I run this code on the Android Emulator with API level 10 and 2.3.3 SDK it gives an error :

The Application Reading example has stopped unexpectedly .Please Try again.Any way to make this work.Thanks and RegardsVaishali

I need to do something else: to use Nexus S as a TAG, therefore I need to write to its MiFair Classic 1k chipset. Can I do this? (I read that the chipset supports all three NFC modes, therefore it should be ok). I didn't find any way to do this (probaly I need to know the Mifair keys?

Dominic, thanks for posting this example it's been very useful to me.I have encountered a problem, when i try to use my own keys defining a byte array Eclipse tells me that it cannot be resolved. Can you tell me why?Im sorry for my terrible english

Thanks for the sample code, really helps. The problem for me now is when I scan a F08 tag( mifare classic compatible), it shows(authentication failed on block 0), obviously it is because of the key A, when I use TagInfo to scan the tag, I get the data of the last block of the first sector is as follows 000000000000787788c1000000000000. Key A should be all zeros if I am not wrong. Thats not working either, do you know what is going wrong? thx

How i can put this app in default selection list "Select an action" or "Complete action using"? As soon as i touch the card, "Select an action" ask where i want to show this app also with NFC TagReader or NXP tag reader.

Hi Dominic Many thanks for the tutorial it helps a lot.I have a problem reading sectors 1-15. I was able to successfully read sector 0 blocks 0-3. I have used authenticate sector with key A and used KEY_MIFARE_APPLICATION_DIRECTORY. Can you recommend how i can read from sectors 1-15. Thanks again.