JPOS Sample Application

Overview

This guide provides a demo sample source for the JPOS application. The sample application includes code snippets for basic operations.

Note: The demo application in this guide is intended for tutorial purposes only and should not be used in production environments.


Create the Project

Start by creating a new project in IntelliJ . For assistance, see the Create the Project tutorial

For more detailed information, Please refer Chapter 33 from the Unified POS specification.

Sample Application Source Code

This sample JavaPOS application demonstrates how to open, claim, and enable an RFID reader, read RFID tags, and handle data events by printing the tag IDs. It also includes utility methods for converting between hex strings and byte arrays.


import jpos.JposException;
import jpos.RFIDScanner;
import jpos.events.*;

public class JavaPosMain {
    static String logicalName = "ZebraRFIDScanners";
    static RFIDScanner reader = null;
    private static DataListener dataListener;

    public static void main(String[] args) throws JposException {

        //Create Reader instance
        reader = new RFIDScanner();

        //1. Connect
        // Establish connection to the reader (Discover and connect automatically)
        reader.open(logicalName);
        reader.claim(1000);

        //2. Configure
        //Enable device
        reader.setDeviceEnabled(true);

        //Enable data events
        reader.addDataListener(dataEventListener);
        reader.setDataEventEnabled(true);

        // Reading tags for 5 seconds without adding filter
        reader.readTags(RFID_RT_ID, new byte[0], new byte[0], 0, 0, 5000, hexStringToByteArray("00"));

        // Release reader
        reader.release();

        //Dispose reader
        reader.close();

    }


    static DataListener dataEventListener = new DataListener() {
        @Override
        public void dataOccurred(DataEvent dataEvent) {
            System.out.println("dataOccurred STATUS : " + dataEvent.getStatus() );
            try {
                if(reader.getTagCount() > 0 ){
                    try{
                        reader.firstTag();
                        String firstTag = convertByteArrayToHexString(reader.getCurrentTagID());
                        System.out.println("Tag ID "+firstTag);
                        for(int index = 0; index < reader.getTagCount()-1;index++){
                            reader.nextTag();
                            String tagID = convertByteArrayToHexString(reader.getCurrentTagID());
                            System.out.println("Tag ID "+tagID);
                        }
                    } catch (JposException e) {
                        e.printStackTrace();
                    }
                }else{
                    System.out.println("Tag Data is Empty ");
                }
            } catch (JposException e) {
                throw new RuntimeException(e);
            }
        }
    };

    // HexString to ByteArray Conversion
    public static byte[] hexStringToByteArray(String sHex) {
        System.out.println(" sHex : " + sHex);
        try {

            if (sHex == null)
                throw new IllegalArgumentException();
            sHex = sHex.replaceAll("\\s", "");
            if (sHex.length() % 2 != 0)
                throw new IllegalArgumentException();
            char[] chars = sHex.toCharArray();
            int len = chars.length;
            byte[] data = new byte[len / 2];
            for (int i = 0; i < len; i += 2)
                data[i / 2] = (byte) ((Character.digit(chars[i], 16) << 4) + Character.digit(chars[i + 1], 16));
            return data;
        } catch (Exception e) {
            e.printStackTrace();
            return new byte[0];
        }
    }

    //ByteArray to HexString conversion
    public static String convertByteArrayToHexString(byte[] byteArray) {
        StringBuilder hexStringBuilder = new StringBuilder();
        for (byte b : byteArray) {
            hexStringBuilder.append(String.format("%02X", b));
        }
        return hexStringBuilder.toString();
    }

}


Code Snippet for Starting and Stopping Read Operations



// Inventory strated
reader.startReadTags(RFID_RT_ID, new byte[0], new byte[0], 0, 0, hexStringToByteArray("00"));

//Running inventory for 5 seconds
try{
    Thread.sleep(5000);
    System.out.println("Inventory Progress_1");
} catch ( InterruptedException jposException) {
    jposException.printStackTrace();
}

//Invnetory stopped after 5 seconds
reader.stopReadTags(hexStringToByteArray("00"));

//Wait for a second
try{
    Thread.sleep(1000);
    System.out.println("Inventory Progress_1");
} catch ( InterruptedException jposException) {
    jposException.printStackTrace();
}

//Process the Tags 
try {
    if(reader.getTagCount() > 0 ){
        try{
            reader.firstTag();
            String firstTag = convertByteArrayToHexString(reader.getCurrentTagID());
            System.out.println("Tag ID "+firstTag);
            for(int index = 0; index < reader.getTagCount()-1;index++){
                reader.nextTag();
                String tagID = convertByteArrayToHexString(reader.getCurrentTagID());
                System.out.println("Tag ID "+tagID);
            }
        } catch (JposException e) {
            e.printStackTrace();
        }
    }else{
        System.out.println("Tag Data is Empty ");
    }
} catch (JposException e) {
    throw new RuntimeException(e);
}
                           
                                                
            

Code Snippet for Adding a Filter



        // Adding filter with filterID and filter mask
        reader.readTags(RFID_RT_ID, hexStringToByteArray("E200470644106821C10B010D"), hexStringToByteArray("FFFFFFFF"), 0, 0, 5000, hexStringToByteArray("00"));
        //filter for E2004706 tag

    

Code Snippet for Reading Partial/Full User Memory



        //Reading with full user data 
        reader.readTags(RFID_RT_FULLUSERDATA, new byte[0], new byte[0], 0, 0, 5000, hexStringToByteArray("00"));

        //Reading with partial user data of length 6 bytes from offset 0
        reader.readTags(RFID_RT_PARTIALUSERDATA, new byte[0], new byte[0], 0, 6, 5000, hexStringToByteArray("00"));