May 172011
 

I have decided to work with the ThinkGear Communications
Driver to utilize data from my MindWave. After reading through the developer
guides and driver references this seemed like it would be a very simple and
enjoyable experience. Since a recent class had me doing a lot of basic
graphical work in Java, I decided it would be best to exercise those skills
during this project.

I added the provided Java class into my project and dropped
the DLL into my working directory and immediately encountered an UnsatisfiedLinkException…
Apparently I needed to learn a little something about the JNI before going
further. The error didn’t occur when I loaded the library, but did occur when I
called a method form that library. This means the DLL was being found, but the
methods weren’t.

I began looking into the JNI and found my basic error: a
very specific naming convention is applied to methods when they are exported
from the DLL. When I added the class into my project I had added it into a
package, thus changing the naming convention expected of the method from:

Java_ThinkGear_GetDriverVersion

To:

Java_info_projectportfolio_dcheath_thinkgear_ThinkGear_GetDriverVersion

Because of this the Java class was unable to find the methods,
as it was looking for the wrong method name. I dropped this class into the
default package (that basically means it’s in no package) and encountered a new
issue.

I don’t know how to import a class that is in no package. First
I tried:

Import ThinkGear;

But the compiler didn’t accept this. A quick Google search
tells me that referencing items in the default package became unavailable in
JDK 1.4… So the file is in the default package, I can’t move it into a package
and I can reference it from within a package. To be sure this was the real
issue I moved all my classes into the default package, and instantly I was able
to get the driver version and get connection IDs.

It doesn’t seem like a good idea to write every project that
is going to use this API in the default package, throw good practice to the
wind and hope my classes never collide, so I think it’s time to consider
options:

  1. Write to NeuroSky and ask them to recreate the
    class inside a package and generate a new DLL
  2. Create another DLL using the JNI whose whole
    purpose is to interact with this DLL and allow me to create the class in
    another package
  3. Use dynamic class loading to feed the ThinkGear.class
    file into my program and allow me to work with it,  through reflection

Option one sounds very good, but since I can’t guarantee
that it will be done (or when), it’s best to carry out my own solution while I
wait. Option two just sounds clunky, creating a JNI DLL to interface with
another JNI DLL just to rename the methods? Option three sounds pretty good,
but still a little chunkier than I’d like… It’s definitely the first thing I’m
going to try.

So the next step is to learn more about reflection and how to
access methods in a Class object.

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

This site uses Akismet to reduce spam. Learn how your comment data is processed.