hid4java icon indicating copy to clipboard operation
hid4java copied to clipboard

How can i read the scanner data.

Open dearming623 opened this issue 1 year ago • 3 comments

I have confirmed that VENDOR_ID and PRODUCT_ID are correct.

Found problem in hidDataReceived did not trigger. The reason is that the read function has not read data.

Please look at the problem. Thank you all.

The operating environment is in the window system. hidapi is version 0.14.0 The device is a scanning gun for HIM

Here's my code

public class BarcodeScannerReader {

private static final int VENDOR_ID = 0x1eab;
private static final int PRODUCT_ID = 0x1a03;

public static void main(String[] args) throws InterruptedException {
    HidServices hidServices = HidManager.getHidServices();
    hidServices.addHidServicesListener(new HidServicesListener() {
        @Override
        public void hidDeviceAttached(HidServicesEvent event) {
            System.out.println("Device attached: " + event.getHidDevice());
        }

        @Override
        public void hidDeviceDetached(HidServicesEvent event) {
            System.out.println("Device detached: " + event.getHidDevice());
        }

        @Override
        public void hidFailure(HidServicesEvent event) {
            System.out.println("HID failure: " + event);
        }

        @Override
        public void hidDataReceived(HidServicesEvent event) {
            System.out.println("HID hidDataReceived: " + event);
        }
    });

    hidServices.start();
    HidDevice hidDevice = hidServices.getHidDevice(VENDOR_ID, PRODUCT_ID, null);

    if (hidDevice != null) {
        System.out.println("Found device: " + hidDevice);
        byte[] data = new byte[10];
        while (true) {
            int bytesRead = hidDevice.read(data, 1000);
            if (bytesRead > 0) {
                String barcode = new String(data, 0, bytesRead);
                System.out.println("Barcode scanned: " + barcode);
            }
        }
    } else {
        System.out.println("Device not found!");
    }

    hidServices.shutdown();
}

}

dearming623 avatar Mar 30 '24 20:03 dearming623

Can you verify if you're using the latest develop-SNAPSHOT release please?

gary-rowe avatar Apr 01 '24 10:04 gary-rowe

Can you verify if you're using the latest develop-SNAPSHOT release please?

I have checked out the latest source code and added a test class to the example.

dearming623 avatar Apr 02 '24 18:04 dearming623

Thanks for the additional information. Does your device need any kind of initialisation to get it started? You might find the Fido2AuthenticationExample useful if you haven't already researched it.

gary-rowe avatar Apr 15 '24 09:04 gary-rowe