This website uses cookies for visitor traffic analysis. By using the website, you agree with storing the cookies on your computer.More information

NFC/RFID for Beagleboard xm with Java

NFC reader   Based on my experience with JavaFX, Beagleboard xm, RxTx and GSM devices, I started my new research project: A simple and cheap time tracking solution.

There are hundreds of tools and hardware solutions available for time tracking, but hardware solutions are expensive and tools without hardware are not practically.

The ideal solution would use a Raspberry Pi and a RFID reader together with a simple JavaFX application. Total costs are about EUR 50,- for the hardware and software should be free :) If you need a case for your hardware and a lcd display, it would be more expensive but EUR 150,- should be the limit for a brilliant system.

I ordered my Raspberry Pi some weeks ago but it is still not available and hopefully it will arrive before Xmas? My fallback board is the Beagleboard xm.

The big question was the RFID reader/writer. Wich devices were supported, and were there any Java APIs for the communication? After a short google search I found a lot of recommendations for RFID breakout board, but it was not too cheap and I don't have an Arduino board and don't want one. I found some other boards but I decided to use PN532 NFC RFID module kit. It is still expensive but seems to be fair.

Why this RFID board?

Firstly, because of a mistake. I saw a screenshot of an example application and the frame icon was a Java icon... So I thought the application was written with Java and there must be a Java API. Not really!
The whole kit was available as developer version with a lot of accessoires like two cards, one dongle, additional cables, ....

After reading the documentation and provided information it was clear that the board was perfect for Arduino boards and the software was written in C/C++, but it was still not too expensive.

But no risk, no fun - ordered.

The delivery time was ok and after unpacking I thought it would be easy to test the included RFID cards - Plug and Play? The truth: No way.

First problem

Included cables were built for Arduino board. I kew that the connection could be a problem and ordered an USB TTL module together with the RFID baord.

The USB module has a connector with 4 pins (GND, VCC, RX, TX) and the RFID board has 4 pins (GND, VDD, RX/SDA, TX/SDL). It was not a big problem to connect the right pins after it was clear that the included standard cable will not work.

Solved.

Second problem

The RFID reader supports 3 different modes (HSU, I2C, SPI). The default mode was I2C because of the Arduino support. But in order to support my Beagleboard, HSU was needed. The board has no jumpers and you must solder a little bit to, but with the right tools it's easy.

Solved.

Third problem

No Arduino board, no chance to test the RFID board? Not really.
There is a great open source project with the name libnfc. It has read/write and diagnostic tools and runs on Linux and should run on Windows too. I compiled libnfc on my Ubuntu VM and had the problem that the nfc-list tool reported a timeout after connecting - but the RFID board was found.
Hm...

It was easy to compile libnfc on Ubuntu - after installing some dependencies - so I thought it should be easy to compile it on my Beagleboard xm? It was a bad idea and I spent some extra hours... but it worked.

How? I can't remember every single step, but try following:

Dependencies:

opkg install cc
opkg install gcc-dev
opkg install task-native-sdk
opkg install libusb-1.0-0 libusb-1.0-dev
opkg install libusb-0.1-4 libusb-0.1-dev
opkg install udev-dev
opkg install autoconf

You need following source packages:

pcsc-lite.1.7.0
libnfc-1.5.1

(Newer versions or different version combinations did not work because libraries were not available for my distribution. But this versions are good enough for tests.)

Start with pcsclite:

./bootstrap
./configure --disable-libhal
make install

Copy /usr/local/include/PCSC directory to /usr/include and /usr/local/lib/pkgconfig/libpcsclite.pc to /usr/lib/pkconfig/

Next, compile libnfc:

./configure --with-drivers=pn532_uart,pn53x_usb --enable-serial-autoprobe --enable-debug
make install

If you get the exception similar to ln: symbol not found -lusb, create a symbolic link:

cd /lib
ln -s libusb-0.1.so.4.4.4 libusb.so

Test, in your libnfc directory:

cd utils
./nfc-list
cd ../examples
# hold a card over the RFID board
./nfc-poll

My output:

# ./nfc-list
Connected to NFC device: PN532 (/dev/ttyUSB0) - PN532 v1.6 (0x07)

# ./nfc-poll
NFC device will poll during 30000 ms (20 pollings of 300 ms for 5 modulations)
ISO/IEC 14443A (106 kbps) target:
    ATQA (SENS_RES): 00  04
       UID (NFCID1): 1b  21  4b  e0
      SAK (SEL_RES): 08

Now you know that your hardware is supported and works with CPP libraries. But what about Java?

I found NFC Tools but the online demo did not work with my reader. Another solution was Open-NFC.

Both solutions were feature rich but horrible because the first one did not compile and maven could not resolve all dependencies. The other solution was too complex for my use case. Why the hell are most libraries so complex. Nobody can handle big libraries.

My use-case was simple: I need a small Java library that can be bundled with my application and it should work on Linux, Windows and optionally on MacOS. It should connect to my Reader via RS232. I stopped evaluating libraries and decided to create my own "simple" library for my use-case.

The topic (NFC, RFID, smart cards) is really complex but I had a PDF that described how read/write basically should work. And I had a reference implementation in cpp. After some problems and night hacking, the problem was solved and I had my library that simply worked (only 2 source files and ~ 1000 LoC).

My first application only reads content from cards that are over the reader. Here is the output:

SAM = true
LOOP PAUSE
UUID length: 4
UUID: 1b 21 4b e0
Authentication receive: 00 FF 03 FD D5 41 00 EA
Authentication successful
Read block successfully:
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

Read block successfully:
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

Read block successfully:
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

Read block successfully:
00 00 00 00 00 00 FF 07 80 69 FF FF FF FF FF FF

And it supports writing to the card, but don't overwrite the 4th block because it contains security settings. If you overwrite the 4th block with wrong values, the whole sector is unusable.

What'll come next? A JavaFX UI for in/out tracking.
The library itself is free but the source is not yet available because it needs some refactoring and beautifying.

Have fun ;-)

17 Responses to “NFC/RFID for Beagleboard xm with Java”

  1. Solerman Kaplon says:

    Not having an Arduino board: ok, but I'm interested to know why you wouldn't want one ? Do you have any specific complaints against it?

  2. rjahn says:

    The main reason is: I have too many toys at the moment (GSM modules, Beagleboard-xm, RFID reader, touch screens, ...) and an additional toy would reduce my sleep to a horrible minimum!

    Another reason is that it would be too much hardware for my use-case. The solution should be as simple and cheap as possible.

    To be honest... I was considering whether I should order an Arduino because it's fantastic, but maybe for one of my next experiments ;-)

  3. Gerrit says:

    Nice work! Because I have a NFC module based on the same chip (PN 532) in combination with a I2C -> RS232 module I'm really interested in the Java lib you wrote to access the NFC module. Question: Is the code available somewhere on the web or would it be possible to get it fro you?
    Keep up the nice work,
    Cheers,
    Gerrit

  4. rjahn says:

    Hi Gerrit, the code is licensed under Apache 2.0, but I need some time to move the code to a public repository. I'll let you know if its available.

  5. Martynas says:

    Could you post dimensions (width and length) of this NFC board? I can't seem to find them anywhere.

  6. rjahn says:

    No problem!

    width = 4 cm
    length = 4,9 cm

  7. Carlos Rosales says:

    Hi rjahn, very interesting subjet. I'm trying to use your library but I'm having some issues.

    Can I contact you via email?

    Thanks!

  8. Stefanie says:

    Hi,

    I use your libary but I have problems about reading the tag because I use Mifare Ultralight tags and no Mifare Classic tags. Dos anybody have some hints for me what I have to change?

    Thanks a lot!

  9. rjahn says:

    I've no experience with such tags. Did writing work?

    The library is not complete because it was designed for one use-case. There are complete NFC libs available!

    Edit:

    It shouldn't be a big problem to change the eNFC source code to support ultralight cards...

  10. Stefanie says:

    Thanks for your fast reply!
    No - the libary found the card but doen't get the ACK frame.
    I know that there are libs - but I use a beaglebone and want to use java and other libs are really big and complex, or do you have a recommendable other lib?

  11. rjahn says:

    :) Yes, the lib is small and the complexity of all other solutions was the reason for developing a small one. But I didn't own different tags, so classic was my first choice.

    open-nfc is popular and should work, but I've no experience with embedded devices (yes, complex+)

    BTW. The eNFC project contains some test cases. You could start with some basic tests because ultralight isn't too different to classic. There is good documentation available:

    UL: http://www.nxp.com/documents/data_sheet/MF0ICU1.pdf, (http://www.nxp.com/documents/application_note/AN1303.pdf) or Classic 1K: http://www.nxp.com/documents/data_sheet/MF1S503x.pdf

    It took about 1 day to implement current eNFC functionality (without refactoring), so it shouldn't be more than some hours to change it to support ultralight.

  12. Stefanie says:

    I will going on to try it ;)
    Thank you very much for your fast replys!

  13. rossse says:

    Hi!
    One question - have you already tried to emulate an ISO14443-3?
    I want to extend your java lib - but I'm not able to init the PN532 as a Target.
    Thanks!

  14. rjahn says:

    We didn't try ISO14443-3. The library was a short research project and not designed as universal solution.

    Do you have example (C/C++) code for PN532. I could help with some input if possible. The implementation with spec only is not a problem but needs more time!

  15. rossse says:

    Thanks for the response.
    I know that this lib isn't a universal solution, but it's a good basis, because it's not so large.
    I want to set the PN532 to the TgInitAsTarget mode, so that I'm able to emulate an tag and also to do p2p communciation. But first I'd like to emulate a card, what a Android smartphone could read.
    I found some C code at https://github.com/Seeed-Studio/PN532/blob/master/PN532/emulatetag.cpp
    But I'm not so well skilled in C.
    I would be very very grateful, if you could give me some input.
    Thanks

  16. rjahn says:

    Nice implementation. It shouldn't be too hard for you, to implement this with Java.

Leave a Reply

Spam protection by WP Captcha-Free