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.
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 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 “tagscan.java” 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 card.disconnect(false); } 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).
Thanks for your response, i’ll try the instructions, but i hve try it on java netbeans program
do you hve instruction how to instal java netbeans on Pi 3 ?
I don’t own a Raspberry Pi 3 (yet), so I can’t help you with that.
ok no problem,
i have tried the program using netbeans on Pi, but the console just showing Terminal [ ]
i think it’s not work, and can’t open / read the nfc , but it’s work in windows, but just read the UID, do you know how i can read data on smartcard ?
help me please
If it shows just ‘Terminal[]’ then it was unable to find the NFC reader.
Try ‘sudo nfc-list’ from the command line, does that find your NFC reader?
I have been a bit busy lately, I will try to update the code to show a read/write example soon.
I had a similar result. I’m using the latest version of everything as of 17/2/18 on a Raspberry-Pi 3.
‘sudo nfc-list’ shows the reader but it wasn’t being picked up by the code.
I found this article, https://stackoverflow.com/questions/12376257/accessing-javax-smartcardio-from-linux-64-bits. It refers to javax.smartcardio library searching for the PC/SC library in the wrong directory. There are a few tips for correcting this and I used the tip from AshanPerera – it has a couple of errors but I corrected them with the first choice on auto-fix and everything worked out well.
Hello, i find your article really helpful and i like it. But, i need the answer. currently i am doing my final year project using rfid tag and acr122u nfc reader. I am already install the driver and also eclipse ide. Then, what should i do if i want to connect the nfc with eclipse ide to make it functional. and any other software needed for this development besides eclpise ide? Thank you for helping me..:)
I don’t understand your question. After installing the driver and Eclipse, you can write Java programs in Eclipse that access the NFC reader and the tags. You do not need other software for development.
Did you try the example code?
i am already try the example code but its not compatible with the acr122u nfc reader. Did you have the sample code of the nfc ? i am glad if you can share it with me 🙂
Hey,
Thank you for the tutorials, VERY helpful. I have a problem though. When I run the code with the card on the reader(I have the same reader as you) the program runs as it should. But when I run it without the card it crashes with CardException: connect() failed. Do you have any idea what causes the connection failure?
Thank you again for the tutorials,
Martin
I’m sorry for the late reply. I tried but I could not replicate the error. Did you already find out what the problem was?
System.out.println(“UID: ” + bin2hex(response.getData()));
suppose i want to display the result in textfield gui. is it possible?
I think that should be possible, it would be very useful. Unfortunately I’m not very skilled in Java, so I would not know how to do that right now.
If I find a way some time, I will add it to the article. Please let me know when you figure it out, I think it will be helpful to others too 🙂
What APDU command do i use print out the NDEF data from an encoded tag.
I’m sorry for the late reply. I have no experience with NDEF yet, so unfortunately I can’t help you with that at the moment.
Hi,
I am running Eclipse Java 2019-06 on my PC and I used your code with a minor tweak and I can read UID’s just fine. Now, how can I write a file to a blank NFC card? I am using NTAG215 cards if it helps.
Hi,
Thank you for the explanations, that was really helpful. Do you also have a java code to “WRITE” with the NFC ? I am currently doing an internship and I have to find a way to write with my ACR122U-A9 on Mifare cards.