4
votes

I'm struggling to find a Java API that enables to grab the input of any device connected through USB. I've found jUsb but it seems deprecated and old.

What I would like is:

  • a platform independent API (working on Unix, Windows, Mac)
  • an API that can grab inputs from any device (actually I want to connect a joystick to my USB port)
3
I'd rather be surprised if something like that existed ... sounds like you want a device driver written in javaGerald Schneider
I would be surprised no one has ever wanted to make a (very basic for me) video game, with usb joystick enabled... Anyway, thanks for the "device driver written in Java" suggestion, seems to be want I'm looking for yes.toni07
If you actually only want to use a Joystick, try Jinput java.net/projects/jinputGerald Schneider
Thank you I will take a look on this one. My question was more global than just a joystick as I thought a USB API could be written for any device connected through USB but I might be wrong...toni07
You might be able to read data transmitted over USB, but in order to make heads-or-tails of it, you generally need a device specific driver. This applies to basically anything connected via. USB, so you likely need either a device driver in Java, or a wrapper around an existing driver.Thor84no

3 Answers

3
votes

We had the same problem for our USB sensors: There no 100% Java solution. You will always need an OS specific code that is written in another language (and that has access to the native API).

The most portable solution that we have found is to implement a kind of gateway that will encapsulate USB data over a localhost TCP socket. We have 3 implementations of the gateway (OSX, UNIX and Windows) that will call native API to interact with USB device. The Java code will send/receive USB traffic troughs the TCP gateway. We have done some benchmarks and we have measure that the overall difference between a pure C++ code and the Java implementation is less than 10ms (for a large 45ms transaction), while the latency is negligible (no impact) once you have an open channel.

We have written an post that explain our solution here.

0
votes

You need to install device drivers that can detect your device first.

In the Device Manager -> Ports(COM & LPT) you can see the port to which your device is connected to. eg COM1/COM2/COM3

You can code to add listener events for reading data from these ports. There are many libraries available to work with COM Ports. This link can be useful to you.

I use comm.jar to read inputs from the hand held scanner device.

0
votes

In the past I have used libusb. Not simple but if you root around enough you can get drivers for several operating systems and Java bindings.