Package org.hid4java

Class HidDeviceManager


  • class HidDeviceManager
    extends java.lang.Object

    Manager to provide the following to HID services:

    • Access to the underlying JNA and hidapi library
    • Device attach/detach detection
    Since:
    0.0.1  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void afterDeviceWrite()
      Indicate that a device write has occurred which may require a change in scanning frequency
      private void configureScanThread​(java.lang.Runnable scanRunnable)
      Configures the scan executor service to allow recovery from stop or pause
      java.util.List<HidDevice> getAttachedHidDevices()  
      private java.lang.Runnable getScanRunnable()  
      boolean isScanning()  
      void scan()
      Updates the device list by adding newly connected devices to it and by removing no longer connected devices.
      void start()
      Starts the manager If already started (scanning) it will immediately return without doing anything Otherwise this will perform a one-off scan of all devices then if the scan interval is zero will stop there or will start the scanning daemon thread at the required interval.
      void stop()
      Stop the scan executor and block until terminated (max 5 seconds)
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • hidServicesSpecification

        private final HidServicesSpecification hidServicesSpecification
        The HID services specification providing configuration parameters
      • attachedDevices

        private final java.util.Map<java.lang.String,​HidDevice> attachedDevices
        The currently attached devices keyed on ID
      • scanThread

        private java.lang.Thread scanThread
        The device enumeration thread We use a Thread instead of Executor since it may be stopped/paused/restarted frequently and executors are more heavyweight in this regard
    • Constructor Detail

      • HidDeviceManager

        HidDeviceManager​(HidServicesListenerList listenerList,
                         HidServicesSpecification hidServicesSpecification)
                  throws HidException
        Constructs a new device manager
        Parameters:
        listenerList - The HID services providing access to the event model
        hidServicesSpecification - Provides various parameters for configuring HID services
        Throws:
        HidException - If USB HID initialization fails
    • Method Detail

      • start

        public void start()
        Starts the manager If already started (scanning) it will immediately return without doing anything Otherwise this will perform a one-off scan of all devices then if the scan interval is zero will stop there or will start the scanning daemon thread at the required interval.
        Throws:
        HidException - If something goes wrong (such as Hidapi not initialising correctly)
      • stop

        public void stop()
        Stop the scan executor and block until terminated (max 5 seconds)
      • scan

        public void scan()
        Updates the device list by adding newly connected devices to it and by removing no longer connected devices. Will fire attach/detach events as appropriate.
      • isScanning

        public boolean isScanning()
        Returns:
        True if the scan thread is running, false otherwise.
      • getAttachedHidDevices

        public java.util.List<HidDevice> getAttachedHidDevices()
        Returns:
        A list of all attached HID devices
      • afterDeviceWrite

        public void afterDeviceWrite()
        Indicate that a device write has occurred which may require a change in scanning frequency
      • configureScanThread

        private void configureScanThread​(java.lang.Runnable scanRunnable)
        Configures the scan executor service to allow recovery from stop or pause
      • getScanRunnable

        private java.lang.Runnable getScanRunnable()