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 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 finished, 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 “” tab, overwriting all of the default code:

package readertest;

import java.util.List;
import java.math.BigInteger;
import javax.smartcardio.*;

public class tagscan {
	static String bin2hex(byte[] data) {
	    return String.format("%0" + (data.length * 2) + "X", new BigInteger(1,data));
 public static void main(String[] args) {

  try {
   // Display the list of terminals
   TerminalFactory factory = TerminalFactory.getDefault();
   List<CardTerminal> terminals = factory.terminals().list();
   System.out.println("Terminals: " + terminals);

   // Use the first terminal
   CardTerminal terminal = terminals.get(0);

   // Connect wit hthe card
   Card card = terminal.connect("*");
   System.out.println("Card: " + card);
   CardChannel channel = card.getBasicChannel();

   // Send test command
   ResponseAPDU response = channel.transmit(new CommandAPDU( new byte[] { (byte) 0xFF, (byte) 0xCA, (byte) 0x00, (byte) 0x00, (byte) 0x00 }));
   System.out.println("Response: " + response.toString());
   if (response.getSW1() == 0x63 && response.getSW2() == 0x00)  System.out.println("Failed");
   System.out.println("UID: " + bin2hex(response.getData()));
   // Disconnect the card

  } catch(Exception e) {

   System.out.println("Ouch: " + e.toString());


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.



Add a Comment

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