Welcome to another Myo Unleashed! This time we have Nicholas Stuart, author of the popular Myo Java bindings. Take it away, Nicholas!

The JVM is a powerful virtual machine that not only powers the Java language, but many other programming languages, such as Scala, Clojure, JRuby, and more. Thanks to the JVM's extensibility, creating a Myo binding for Java means that it is enabled for all these languages well. I'm Nicholas Stuart, and I wrote the Myo Java API bindings.

Setup

I've Maven enabled the myo-java bindings, which means that it's extensible into many other dependency managements tools for Java as well, such as SBT and Spring Roo.

All you need to do is add the maven dependency to your pom.xml's dependencies section:

<dependency>  
    <groupId>com.github.nicholasastuart</groupId>
    <artifactId>myo-java</artifactId>
    <version>0.8.2</version>
</dependency>  

Now you're wired in.

How to

Start with the basics:

public class HelloMyo {  
    public static void main(String[] args) {
    }
}

You'll need to connect to the Hub, with an application identifier that identifies your application. For the sake of simplicity you can make it the fully-qualified name of your class.

Hub hub = new Hub("com.example.HelloMyo");  

After initializing the Hub, we can finally connect to the Myo. We tell the Hub to connect us with a timeout in case it cannot find anything immediately.

Myo myo = hub.waitForMyo(10000);  

The API leverages event handling to register callbacks for events that happen with the Myo. You'll need to create a class that implements the DeviceListener class, or extends the AbstractDeviceListener class if you don't want to implement all the methods.

hub.addListener(new AbstractDeviceListener() {  
    @Override
    public void onPose(Myo myo, long timestamp, Pose pose) {
        System.out.println(String.format("Myo switched to pose %s.", pose.toString()));
    }
});

And now we wait...

while (true) {  
    hub.run(1000 / 20);
}

That's it. Once your Myo is connected, it should start spitting out what pose you are in.

Here is the code all put together:

public class HelloMyo {  
    public static void main(String[] args) {
      Hub hub = new Hub("com.example.HelloMyo");
      Myo myo = hub.waitForMyo(10000);
      hub.addListener(new AbstractDeviceListener() {
          @Override
          public void onPose(Myo myo, long timestamp, Pose pose) {
              System.out.println(String.format("Myo switched to pose %s.", pose.toString()));
          }
      });
      while (true) {
          hub.run(1000 / 20);
      }
    }
}

This is a beginner introduction to using Myo for Java, if you have any questions or discover any bugs, I welcome them all using the Github Issues Tracker or blow up my Twitter feed.

That's it for this Myo Unleashed! If you use these bindings for anything cool, drop us a line, share a video, or tell everyone in the forums!

Newsletter

Enter your email address and get all latest content delivered to your inbox every now and then.