5

I'm writing an SWT application which needs to sit in the system tray and pop up automatically whenever the user connects some USB device (the application serves as its control panel).

The way to do this in the native environment (win32 in this case, but I should be platform-independent ultimately) is to listen to the WM_DEVICECHANGE event, then checking if my device has been disconnected.

Googling the subject, it seems like SWT does not in fact handle this type of event. Does anyone have any idea as to how to achieve this? My fallback solution would be sampling the USB port every n seconds, looking for the device, but this is a no-no solution as far as I'm concerned...

Thanks and cheers

Shai

Electric Monk
  • 2,192
  • 2
  • 23
  • 33

4 Answers4

1

EDIT: J-16 SDiZ reported that the API exists for Windows too

Here is a very good article about Access USB devices from Java applications

The described jUSB module contains a USBListener object.

Interface implemented by objects that want to monitor USB structure. The order in which these changes are reported is not necessarily going to be the order in which the changes were seen in the real world, and delays also occur.

Youre SWt object can implement this listener and do something, when the method

deviceAdded(Device dev) 

is fired

Markus Lausberg
  • 12,177
  • 6
  • 40
  • 66
0

Java does not provide direct interaction with the devices and with USB. There are several third-party libraries for Java that provide USB-related features (read/write from USB port). For example: http://www.icaste.com/ (commercial)

Zorglub
  • 2,077
  • 1
  • 19
  • 22
  • I don't need USB connection - I'm doing this using a DLL supplied with the device I'm connecting to wrapped by JNA. I need to listen to the OS WM_DEVICE_CHANGED message. – Electric Monk Apr 22 '09 at 14:34
0

Your comment for Zorglub suggests that you have some (presumably) JNI/JNA code to call the 3rd party DLL you mentioned. I assume you could also write a JNI wrapper to the Windows API where you can register to be notified for the _WM_DEVICECHANGE_ event you mentioned.

lothar
  • 19,853
  • 5
  • 45
  • 59
  • that would probably the right thing to do, given enough resources are available for this development. Unfortunately, this is not the case... I settled for an active listener eventually. – Electric Monk Apr 23 '09 at 06:55
0

There is a jUSB port for windows at http://www.steelbrothers.ch/jusb/

J-16 SDiZ
  • 26,473
  • 4
  • 65
  • 84