Application Performance Using JNI for RFID

Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java Virtual Machine) can recognize the symbols exported by the native program. It defines how functions are to be named. Also it defines how to pass different types of data variable between the native code and Java code.

We had to implement RFID in one of our Java application. According to the application demand we choose to use network based RFID scanners. The manufacturer of the scanner supplied us with C++ SDK along with the scanner. Since the SDK is for C++ we wrote a JNI to integrate it with our application.

The Setback
The JNI is written with four functions, Connect_Scanner, Read_RFID, Clear_Buff and Disconnect_Scanner.

Connect_Scanner: This is to connect to the scanner and expects the IP address and Port number from the Java code. On success or failure of the connection this function returns a status code accordingly.

Read_RFID: On a successful connection the Java code spawns a new thread and calls this function in a continuous loop. Internally the C++ code calls a API function provided by the SDK. One of the parameter of the function was a type struct array. When the API function returns the struct array contains all the RFIDs scanned.

Clear_Buff: So that the same RFID is not scanned more than once a buffer temporarily holds all the scanned RFIDs. This function is used to clear this buffer.

Disconnect_Scanner: This function is called to disconnect the scanner from the application.

We found when we called Read_RFID from the Java code it took little more than 4secs to return. Otherwise when we called the function from the native code itself it was just about 1 to 2 milliseconds. The main reason could be because of the way strings are handled in Java vis-à-vis C++.

The Solution

We compiled the native code in to a separate executable and connected the Java application with a local loop socket. The native executable listens on IP and port 100. The Java code makes a socket client connection to the executable. The functions are then called through the socket.    

Comments (0)

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.