gary-rowe / hid4java

A cross-platform Java Native Access (JNA) wrapper for the libusb/hidapi library. Works out of the box on Windows/Mac/Linux.
MIT License
229 stars 71 forks source link

Event hidDataReceived not working. #130

Closed cracktichi closed 1 year ago

cracktichi commented 2 years ago

Hello all,

I want to get data from HID device so i used event hidDataReceived from class https://github.com/gary-rowe/hid4java/blob/develop/src/main/java/org/hid4java/HidServicesListener.java. But when i build this lib from this github (Not using veriosn 0.7.0 from Maven because it dont have above event), hidDataReceived not working on both Window 11 x64 and Linux x64(Raspberrypi 4). I'm tested device on Linux and other event working perfect(hidDeviceAttached, hidDeviceDetached). I'm checked comunicate of HID device (Barcode scanner) on Linux at path dev/input/by-id and i had received data from device. So make sure this is not device issue. Below is my implement:

public abstract class BarcodeDeviceService implements HidServicesListener {
    private HidServices hidServices;
    /**
     * Constructor
     */
    public BarcodeDeviceService() {
        // Configure to use custom specification
        HidServicesSpecification hidServicesSpecification = new HidServicesSpecification();

        // Use the v0.7.0 manual start feature to get immediate attach events
        hidServicesSpecification.setAutoStart(false);

        // Get HID services using custom specification
        hidServices = HidManager.getHidServices(hidServicesSpecification);
        hidServices.addHidServicesListener(this);

        // Manually start the services to get attachment event
        hidServices.start();
    }

    @Override
    public void hidDeviceAttached(HidServicesEvent event) {
        System.out.printf("BarcodeDeviceService hidDevice Attached");
    }

    @Override
    public void hidDeviceDetached(HidServicesEvent event) {
        System.out.printf("BarcodeDeviceService hidDevice Detached");
    }

    @Override
    public void hidFailure(HidServicesEvent event) {
        System.out.printf("BarcodeDeviceService hid Failure");
    }

    @Override
    public void hidDataReceived(HidServicesEvent event) {

        System.out.printf("BarcodeDeviceService hidDataReceived:%n");
        byte[] dataReceived = event.getDataReceived();
        System.out.printf("< [%02x]:", dataReceived.length);
        for (byte b : dataReceived) {
            System.out.printf(" %02x", b);
        }
    }

Could you please help me this issue. Thank you in advance.

permiakover commented 2 years ago

If you need data from HID device, you can implement this via int read(byte[] data) when your target device is attached and open

gary-rowe commented 1 year ago

Given that this has gone quiet I'll mark it as closed. Thank you @permiakover for providing some help here.