NFC with RaspberryPi and Java

For a presentation I did, I needed to come up with a neat example of directly connecting a RaspberryPi with some add-on device. The ITEAD PN532 NFC Module looked perfect for this case, and I started working with it. My end goal was to operate it with Java code.

RaspberryPi with ITEAD PN532 NFC Module
RaspberryPi with ITEAD PN532 NFC Module

The ITEAD blog has a nice example plus a library done in C for interfacing the NFC Module.  It uses the SPI interface and operates on a low level by using the WiringPi library.

The pi4j library is a JNI to the WiringPi C library, so in basic terms it should be able to do the same thing.

I started by porting an Arduino code first (github link) and using the Serial interface for communication, but I failed. Then I started porting the ITEAD provided library and used the SPI interface as in the original code. After some struggle, I managed to devise some working code: https://github.com/hsilomedus/raspi-pn532

Notes:

  • there are two interface implementations: PN532Spi and PN532Serial. The second one is marked as @Deprecated because it’s not functioning for some reasons
  • the SPI implementation at the moment can only get the firmware version and read the passive target id of an NFC tag. Hopefully I’ll implement the other functions soon.
  • before using the SPI interface, make sure that it is enabled. See the notes in the ITEAD blogpost and in the github repo readme.
  • when starting the example, execute java with sudo in order pi4j to work properly.