Fun with RFID. Using the Nexus S nfc Service

Tutorials with advanced 'difficulty' and more Lines of Code.

Fun with RFID. Using the Nexus S nfc Service

Postby osikes » Thu Jan 27, 2011 6:05 am

This tutorial is about reading rfid cards, but is very specific to a certain chip type. However I feel the information is overal useful. I created the blog entry, and reposting in this forum, because there's practically 0 information on the internet related to this subject, and it would have saved me a TON of time if there had been. Hopefully this can help some poor soul that was stuck in my position. I'm sure there are many code ineffeciencies , and I'd be glad for any suggestions on improvements or rewrites if you know a better way.

The entry is at my blog http://osikes.blogspot.com/2011/01/fun-with-rfid.html.
But I'll post it here as well.


---------------------------FUN WITH RFID-------------------------------------


Recently I've been attempting to take advantage of the fancy new hardware packed into the Nexus S. More specifically reading rfid cards using the Nexus's nfc hardware. This turned out to be a bit more tricky than I thought.

Easy part:

Google provides source code for a demo application that will greatly aid you in figuring out the api. The android.nfc btw is stupid simple. In fact you don't have to worry about connections, listeners, any of that stuff. The google nfc service is always on, constantly polling for new card scans. When it detects a card, it issues a nfc intent. You just have to make sure that your applications manifest file, is setup for that intent.

As in: Make sure to have this in your main activity.
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <intent-filter>
  2. <action android:name="android.nfc.action.TAG_DISCOVERED"/>
  3. <category android:name="android.intent.category.DEFAULT"/>
  4. intent-filter>
  5.  
Parsed in 0.001 seconds, using GeSHi 1.0.8.4

Also make sure you enable nfc permissions and hardware requirement.
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <uses-permission android:name="android.permission.NFC" />
  2. <uses-sdk android:minSdkVersion="9">uses-sdk>
  3. <uses-feature android:name="android.hardware.nfc" android:required="true" />
  4.  
Parsed in 0.001 seconds, using GeSHi 1.0.8.4

Next in your main activity all you have to do is use the getIntent() to retreive information picked up from a scan. Once again , 'YOU DONT HAVE TO WORRY ABOUT LISTENING FOR SCANS ' which is awesome!

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. byte[] byte_id = getIntent().getByteArrayExtra(NfcAdapter.EXTRA_ID);
  2.  
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


This one above line, retrieves the unique id from the rfid chip, and pushes it into a bytearray.

Tricky Part:

Now that you have your card_id in the array, you cant just push it out to screen. By default android attempt to convert the bytearray to UTF-8, so you would get some weird symbols. In my case I needed the hexadecimal from that to store in a DB. I'm going to list the exact sort of rfid chip I dealt with, in hopes that this will help someone else attempting to do something similar. This is because there is 0 information on the internet that will help you figure this out otherwise. With the exception of a technical specification document that I'll link.


My card type and rfid chip:
High quality CR80 30mil white PVC RFID
cards HF Icode SLI
CR80ICODESLI
Operating frequency 125KHZ

Link to documentation: http://www.nxp.com/acrobat_download2/ot ... 052611.pdf

Your chip type is important, because it will let you know how to read your uid. In the case of my chip, it's a 64 uid, with MSB as the left most.

This is very important in being able to correctly convert your bytearray to binary, hex whatever. There's several code examples of how to convert to hex, but unless you know if you're dealing with little endian, big endian, where your lsb, msb is, it won't work, even though you'll get a hex output.

In my case I used the below function

//Modified from function, I found on internet to suit specific needs.(Not trying to take credit :) )

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public String ConvertbyteArrayToHexString(byte in[]) {
  2.        
  3. byte ch = 0x00;
  4. int i = in.length-1;
  5.  
  6. if (in == null)
  7. return null;
  8.  
  9. String HEXSET[] = {"0", "1", "2","3", "4", "5", "6", "7", "8","9", "A", "B", "C", "D", "E","F"};
  10. //Double length, as you're converting an array of 8 bytes, to 16 characters for hexadecimal
  11. StringBuffer out = new StringBuffer(in.length * 2);
  12.  
  13. //You need to iterate from msb to lsb, in the case of using iCode SLI rfid
  14. while (i >= 0) {
  15.         ch = (byte) (in[i] & 0xF0); // Strip off high nibble
  16. ch = (byte) (ch >>> 4); // shift the bits down
  17. ch = (byte) (ch & 0x0F); // must do this is high order bit is on!
  18. out.append(HEXSET[ (int) ch]); // convert the nibble to a String Character
  19. ch = (byte) (in[i] & 0x0F); // Strip off low nibble
  20. out.append(HEXSET[ (int) ch]); // convert the nibble to a String Character
  21. i--;
  22. }
  23. return (new String(out));
  24. }
  25.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


Using this, I was able to successfully parse though the bits of my 8 bytes, and convert all necessary to hex. I really hope this helps someone, and I'm sure there are many glaring inefficiencies with this code, and any comments left with suggestions to improve it are greatly appreciated.
osikes
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Jan 26, 2011 5:53 am

Top

Return to Advanced Tutorials

Who is online

Users browsing this forum: No registered users and 3 guests