kanishka
kanishka

Reputation: 13

How to create an event listener in java to check hardware changes in MacOS operating systems?

I need an event listener to which can listen to hardware changes. I tried to implement this using following method.

import javax.swing.SwingUtilities;
import com.sun.jna.platform.mac.IOKit.IOIterator;
import com.sun.jna.platform.mac.IOKitUtil;
import  com.sun.jna.platform.mac.IOKit.IORegistryEntry;

import java.util.ArrayList;

public class HardwareListener implements Runnable{

    private boolean running;

    private WinController controller;


    public  HardwareListener(WinController _controller){

        this.controller=_controller;
    }

    @Override
    public void run() {
        running = true;
        int prevCount=0;
        IOIterator iterator= `IOKitUtil.getMatchingServices("IOUSBHostDevice");`
        if(iterator !=null){
          IORegistryEntry device = iterator.next();
          while (device!=null){
              device.release();
              device = iterator.next();
              prevCount++;

          }
        }
        System.out.println("1 - Previous Count: "+prevCount);
        while (running){
            System.out.println("Previous Count: "+prevCount);
            int count =0;
            iterator= IOKitUtil.getMatchingServices("IOUSBHostDevice");
            if (iterator!=null){
                IORegistryEntry device = iterator.next();
                while (device!=null){
                    device.release();
                    device = iterator.next();
                    count++;

                }
                if (prevCount!=count){
                    controller.updateUSBList();
                }
                if(prevCount>count){
                    System.out.println("Device Removed");
                }else if (prevCount<count){
                    System.out.println("Device Added");
                }
            }
            if (iterator!=null){
                iterator.release();
            }
            System.out.println("New Count: "+count);
            prevCount=count;
            this.sleep(2);
        }
    }

    private void sleep(int seconds){
        try {
            Thread.sleep((int)seconds*100);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    public void stop() {
        running = false;
    }
}

The above snippet run in a secondary thread. However, the loop in run() function fails to run properly time to time. For example, a when a usb hub connected, I have the pause the thread for around 2 seconds ignorer for:

IOIterator iterator =  IOKitUtil.getMatchingServices("IOUSBHostDevice");` 

to detect all the devices. Otherwise, it will detect them through multiple loops.

I want the codes after this line to run once iterator has all the hardware changes identified.

Please can someone provide a better solution to achieve this functionality ?

Framework AND Other details.

Upvotes: 0

Views: 71

Answers (0)

Related Questions