NederlandsKlik deze knop voor de Nederlandstalige website

ACR122U NFC: programming with Java and Eclipse on a Raspberry Pi

After a post on how to get the ACR122U USB NFC reader/writer running on the Raspberry Pi 2, it’s now time to start writing our own programs for it. In this article we will be looking at the Java programming language. First we will install the ACR122U NFC drivers and set up the popular Eclipse IDE with Java support on the Raspberry Pi 2. Then we will look at some code and see how we can read and write NFC tags using Java.

This article is a work in progress. More information will be added soon.

Preparing the Raspberry Pi 2

First,  download Raspbian (full version) from the raspberrypi.org webiste and write it to an SD card. I downloaded “Raspbian Jessie with Pixel” (version February 2017, release date 2017-02-16, kernel version 4.4) and wrote the image to an 8GB MicroSD card.

Then, connect the ACR122U to your Raspberry Pi and boot the Pi from the SD card.

Installing the ACR122U NFC drivers

Now we will install the drivers for the ACR122U NFC. See the earlier post “ACR122U USB NFC reader on a Raspberry Pi” for full instructions. Come back to this page when you have completed the installation succesfully.

Setting up Eclipse IDE for Java on the Raspberry

We will be developing our Java programs using the Eclipse IDE (Integrated Development Environment). At the time of writing, the version in the repositories is Eclipse 3.8. We will also need to install the Eclipse Java development tools (JDT).

Download and install Eclipse and the JDT:
sudo apt-get install eclipse eclipse-jdt

After installing is finihed, you should reboot the Raspberry Pi:
sudo reboot

After rebooting, you will find Eclipse in the Raspbian menu under “Programming”.  Start Eclipse from the menu. This will probably take a while, just the first time, so do not abort the process. On a Raspberry Pi 2, it took a couple of minutes, with processor usage stable around 25%.

When the “Workspace launcher” appears, check the option “Use this as the default and do not ask again” and click OK. Wait while Eclipse continues starting up. When Eclipse is finally ready, close the “Welcome” tab.

Writing our first Java program

We can create our Java project:

  • Select File > New > Project > Java project and click Next
  • Project name: type ‘NFC’
  • JRE: check ‘Use default JRE (currently ‘java-7-openjdk-armhf’)‘ and click Finish
  • When asked ‘Open associated perspective?’: click Yes.

Create a package:

  • Select File > New > Package
  • Name: type ‘readertest’ and click Finish

Create the class:

  • Select ‘readertest’ in the package explorer on the left
  • Select File > New > Class
  • Name: type ‘tagscan’

Then copy and paste the code below into the “tagscan.java” tab, overwriting all of the default code:

Run the code:

  • Select Run > Run (Ctrl + F11)

You should now see some output in the Console window. If a tag reader/writer (for instance the ACR122U NFC) is present, the program will output some of the device’s details. If also a tag is present, the program will print it’s Unique ID (UID).

This article is a work in progress. More information will be added soon.

NFC ACR122U RFID Contactless smart Reader + 5pcs Mifare IC Card
Tested and recommended by OneGuyOneBlog.com:

NFC ACR122U RFID Contactless smart Reader + 5pcs Mifare IC Card
13 Comments

Add a Comment

Your email address will not be published. Required fields are marked *