Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can i read the scanner data. #146

Open
dearming623 opened this issue Mar 30, 2024 · 3 comments
Open

How can i read the scanner data. #146

dearming623 opened this issue Mar 30, 2024 · 3 comments
Assignees
Labels

Comments

@dearming623
Copy link

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();
}

}

@gary-rowe gary-rowe self-assigned this Apr 1, 2024
@gary-rowe
Copy link
Owner

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

@dearming623
Copy link
Author

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.

@gary-rowe
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants