One of the challenges in creating the content for the Java One 2014 Java SE Embedded Internet of Things Hands-on-Lab concerned interacting, via Java and OSGi, with a USB temperature sensor. Unfortunately a USB communications API is not part of the Java SE standard (as of this post: Halloween 2014). So the question is, how can Java/USB communication be established, and furthermore and how does this work within the OSGi framework?
In looking around at some of the available options, we chose the javahidapi as the basis for this connectivity. As a Java/JNI wrapper around the C/C++ HID API for Linux, MacOS X and Windows, the appeal behind this API is that using it does not require the
use of a custom driver for each device on each platform.
In order to operate within an OSGi framework (in this case Apache Felix 4.4), javahidapi's open source code has been slightly modified/enhanced. The end result is that an OSGi bundle is available that can be dropped into standard OSGi frameworks to support USB communication for HID devices. It does contain a native component, and for the sake of simplicity, we've decided to include a separate jar file for each supported architecture. For the OSGi enthusiast, here's what the generated MANIFEST.MF file looks like for the Linux/armhf (suitable for the Raspberry Pi) architecture:
Bundle-Name: hidapi OSGi Bundle for Linux/armhf
Bundle-NativeCode: native/linux/armv6l/libhidapi-jni-32.so; osname=Linux; processor=armv6l
Created-By: Apache Maven Bundle Plugin
Here are some pre-built hidapi OSGi bundles for popular Linux platforms:
To get a feel for the changes made to the original source, here's what was done:
And here are the associated NetBeans Projects which can be used to build the 4 bundles referenced above.pull down the source for hidapi and build it for inclusion into your project. If anyone is interested in going through this exercise, I'd be glad to post the fruits of their labor here.