Hi @gary-rowe
I have realised that, for some reason, when we call hidService.start() by the first time, no HidServiceEvent is handled by the listener.
Here the code I use to start HidService. It's very similar to the one in the project's example
public static void startHIDService() {
// System info to assist with library detection
log.info("Platform architecture: " + Platform.ARCH);
log.info("Resource prefix: " + Platform.RESOURCE_PREFIX);
// Configure to use custom specification
HidServicesSpecification hidServicesSpecification = new HidServicesSpecification();
hidServicesSpecification.setAutoShutdown(true);
hidServicesSpecification.setScanInterval(500);
hidServicesSpecification.setPauseInterval(5000);
hidServicesSpecification.setScanMode(ScanMode.SCAN_AT_FIXED_INTERVAL_WITH_PAUSE_AFTER_WRITE);
//HidApi.useLibUsbVariant = false;
// Get HID services using custom specification
hidServices = HidManager.getHidServices(hidServicesSpecification);
hidServices.addHidServicesListener(new HidServicesListener() {
@Override
public void hidDeviceAttached(HidServicesEvent event) {
log.info("Device attached: " + event);
}
@Override
public void hidDeviceDetached(HidServicesEvent event) {
log.error("Device detached: " + event);
}
@Override
public void hidFailure(HidServicesEvent event) {
log.error("HID failure: " + event);
}
});
// Start the services
log.info("Starting HID services.");
hidServices.start();
}
Note that, the listener, has not been implemented in the class itself. It's defined on-the-fly by an anonymous class
hidServices.addHidServicesListener(new HidServicesListener() {
...
});
In production, it's a first-citizen class with its own file.
I was expecting that, during the HidService initialization, any HID device already attached will cause a HidServiceEvent. Inspecting hid4java classes, I have found that, somehow, I'm right.
HidService.start() calls HidDeviceManager.start() and ...
public class HidDeviceManager {
...
public void start() {
// Check for previous start
if (this.isScanning()) {
return;
}
// Perform a one-off scan to populate attached devices
scan();
// Ensure we have a scan thread available
configureScanThread(getScanRunnable());
}
...
}
The method scan()
for (HidDevice attachedDevice : attachedHidDeviceList) {
if (!this.attachedDevices.containsKey(attachedDevice.getId())) {
// Device has become attached so add it but do not open
attachedDevices.put(attachedDevice.getId(), attachedDevice);
// Fire the event on a separate thread
listenerList.fireHidDeviceAttached(attachedDevice);
}
}
The first time any device is mapped into attachedDevices should result in a new call to stenerList.fireHidDeviceAttached(attachedDevice);
Given the code above, I should see something like this in the console
Platform architecture: x86-64
Resource prefix: linux-x86-64
Starting HID services.
Device attached: HidServicesEvent{hidDevice=HidDevice [path=/dev/hidraw0, vendorId=0x483, productId=0x5750, serialNumber=STM3210, releaseNumber=0x200, manufacturer=STMicroelectronics, product=STM32 Custm HID, usagePage=0x0, usage=0x0, interfaceNumber=0]}
Enumerating attached devices...
HidDevice [path=/dev/hidraw0, vendorId=0x483, productId=0x5750, serialNumber=STM3210, releaseNumber=0x200, manufacturer=STMicroelectronics, product=STM32 Custm HID, usagePage=0x0, usage=0x0, interfaceNumber=0]
Waiting 30s to demonstrate attach/detach handling. Watch for a slow response after write if configured.
Note the output after Starting HID services. starting with Device attached: HidServicesEvent{.
At the moment, the output is
Platform architecture: x86-64
Resource prefix: linux-x86-64
Starting HID services.
Enumerating attached devices...
HidDevice [path=/dev/hidraw0, vendorId=0x483, productId=0x5750, serialNumber=STM3210, releaseNumber=0x200, manufacturer=STMicroelectronics, product=STM32 Custm HID, usagePage=0x0, usage=0x0, interfaceNumber=0]
Waiting 30s to demonstrate attach/detach handling. Watch for a slow response after write if configured.
What am I doing wrong? I'm missing something, but I don't know what
Thank you in advance.
Hi @gary-rowe
I have realised that, for some reason, when we call
hidService.start()by the first time, noHidServiceEventis handled by the listener.Here the code I use to start
HidService. It's very similar to the one in the project's exampleNote that, the listener, has not been implemented in the class itself. It's defined on-the-fly by an anonymous class
In production, it's a first-citizen class with its own file.
I was expecting that, during the
HidServiceinitialization, any HID device already attached will cause aHidServiceEvent. Inspecting hid4java classes, I have found that, somehow, I'm right.HidService.start()callsHidDeviceManager.start()and ...The method
scan()The first time any device is mapped into
attachedDevicesshould result in a new call tostenerList.fireHidDeviceAttached(attachedDevice);Given the code above, I should see something like this in the console
Platform architecture: x86-64 Resource prefix: linux-x86-64 Starting HID services. Device attached: HidServicesEvent{hidDevice=HidDevice [path=/dev/hidraw0, vendorId=0x483, productId=0x5750, serialNumber=STM3210, releaseNumber=0x200, manufacturer=STMicroelectronics, product=STM32 Custm HID, usagePage=0x0, usage=0x0, interfaceNumber=0]} Enumerating attached devices... HidDevice [path=/dev/hidraw0, vendorId=0x483, productId=0x5750, serialNumber=STM3210, releaseNumber=0x200, manufacturer=STMicroelectronics, product=STM32 Custm HID, usagePage=0x0, usage=0x0, interfaceNumber=0] Waiting 30s to demonstrate attach/detach handling. Watch for a slow response after write if configured.Note the output after
Starting HID services.starting withDevice attached: HidServicesEvent{.At the moment, the output is
What am I doing wrong? I'm missing something, but I don't know what
Thank you in advance.