Hi Dan, and all
I wrote a little piece of code to detect the attach of my Palm device by
using UsbServicesListener. But it looks that the listener isn't working.
When I press the sync button of my Palm USB cradle, the monitor thread
dose not print out the information.
Did I correctly implement the UsbServicesListner interface or I
misunderstood the use case?
My code is attached.
Thanks a lot!
-JoeyS
import java.io.*;
import java.util.*;
import javax.usb.*;
import javax.usb.event.*;
import javax.usb.util.*;
public class testUSB {
UsbServices services = null;
UsbServicesMonThread monitor = null;
public void initialize() {
if (services == null) {
try {
services = UsbHostManager.getUsbServices();
} catch (UsbException uE) {
throw new RuntimeException("Error: " + uE.getMessage());
} catch (SecurityException sE) {
throw new RuntimeException("Error: " + sE.getMessage());
}
}
if (monitor == null) {
monitor = new UsbServicesMonThread();
}
if (services != null && monitor != null) {
services.addUsbServicesListener(monitor);
monitor.start();
}
}
class UsbServicesMonThread extends Thread implements UsbServicesListener {
public void usbDeviceAttached(UsbServicesEvent event) {
System.out.println("Device Attached!");
UsbDevice device = event.getUsbDevice();
UsbDeviceDescriptor deviceDesc = device.getUsbDeviceDescriptor();
if ((deviceDesc.idProduct() == 0x03) && (deviceDesc.idVendor() == 0x830)) {
/* do handshaking packets here */
System.out.println("Find Palm m515!");
}
}
public void usbDeviceDetached(UsbServicesEvent event) {
System.out.println("Device Detached");
}
public void run() {
System.out.println("Start to run Monitor");
for (;;);
}
}
public static void main(String[] args) {
testUSB myTest = new testUSB();
myTest.initialize();
System.out.println("Wait for the event being fired");
for (;;);
}
}