1

When I am going to connect device through USB port, I want to detect it immediately. I am searching for a Java API, my main target is Linux OS.

Does anyone know an API like that?

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Sachin J
  • 2,081
  • 12
  • 36
  • 50

2 Answers2

1

You can do something like this:

try {
    String command = "lsusb"; // you may add some param if you want
                              // or use adb for instance
    Process child = Runtime.getRuntime().exec(command);

    // Get output stream to write from it
    OutputStream out = child.getOutputStream();

    // TODO parsing lsusb output

    out.close();
} catch (IOException e) {
}
CAMOBAP
  • 5,523
  • 8
  • 58
  • 93
  • Thanks for reply. Its detects attached mouse. Let me clear my scenario , when I am connecting an android os device to my machine I want to fire an event which will show that device is found. – Sachin J Nov 19 '12 at 10:07
  • Try replace `lsusb` with `adb devices | awk 'NR>1 {print $1}'`. Pay attention that you need installed `adb` – CAMOBAP Nov 19 '12 at 11:47
0

would also consider monitoring USB events by DBus interface

there is DBus-Java library here and a similar thread but on python here

Community
  • 1
  • 1
Raber
  • 2,040
  • 16
  • 13