Getting the Myo Armband to work with Unity on Android

Note: This post is no longer supported and is left available for reference only. If you encounter issues please seek help from Myo support.

SO you have Unity and your shiny new Myo armband and you want to make a new Android app that uses this cool new armband? Well at the moment, Myo is not supported natively on Android. But fear not! I am here to help you get around this! So, let’s get to it.

The general idea of this workaround is to send messages containing Myo data, from your android device through Android Studio to Unity.

First you are going to need three things:

  1. Unity (duh)
  2. Myo Armband (another duh)
  3. Android Studio, with android SDK installed (not so duh)

Now that you have these three things set up we can get started.

First let’s look at what is needed in Unity.

You will need to download the Myo SDK from here and install the Unity package into your Unity game. Now that you have that done we can start to discuss what you will need out of the Myo Unity package. You are going to need the prefab named “Hub – 1 Myo” (This tutorial teaches how to send messages from one Myo but can be easily adapted to use two). Go ahead and drag the prefab into your scene.

Myo HUB

This prefab has two parts, the parent gameobject (not important) and the child called “Myo.” This contains all the data from the armband on all SUPPORTED platforms, which at the time of this writing does not include android. So we have to make our own version of this handy prefab and integrate it into the current one.

Now to the code, create an empty gameobject called MessageManager. Now create a C# script with the same name “MessageManager.” This will be our script that receives the messages from Android Studio. Put that script on the gameobject and open the script up.

Paste the following code into the MessageManager script

What this is doing is it is taking the data that is being received and it is applying all of that data to the myo object in our scene. Now if your game already has the Myo prefab integrated into it then you are all set on the Unity side of things, hooray! If not then all you have to do is reference the myo object to get any of its data such as poses and orientation data. Now we are ready to export to Android Studio. To do this click “File” then “Build Settings” and follow the picture below.

BuildSettings

Make sure it is an android build and that you have “Google Android Project” selected. Now build your project and put it somewhere safe. We are going to use it in a second. Next we need to fire up Android studio. When Android studio starts up choose “Import non-Android Studio Project” and select your build folder that we just made. It is going to ask you for a new place to put the project, again put it somewhere safe. Now we are ready to start the android studio side of things! We will need to edit the manifest file as well as some of the unity main xmls. First the manifest file, you will need to add the following permissions.  

Now we have to edit the gradle file called build.gradle (Module: app)

gradle

add the following code inside dependencies

Next, we have to add a new file called CustomScanActivity, I have included the commented files on GitHub here. Just go ahead and replace all of the the files in your current android studio project with those three. I’m going to explain what the three files are doing.

AndroidManifest.xml

      • Tells android what permissions we are allowed to use
      • Sets custom scan to be the first activity to run, so we can connect to our myo armband before gameplay.

CustomScanActivity.java

      • Starts up a scan for any Myo devices in the area, this only works if bluetooth is on.
      • It then sends the myo connected over to the UnityPlayerNativeActivity.java

UnityPlayerNativeActivity.java

      • Starts the Myo hub listener on top of the Unity Game and relays all information to unity via messages.

 

You should be all set now! Run the project to your android device and you should now be able to use your Myo armband just as it was on your computer. If you are having any troubles with the builds let me know. I’ll be improving this tutorial soon.

If you need more help on the android studio side of things you can visit the Myo getting started page here.

NOTE: this tutorial does not include the acceleration data. You should be able to figure this out using the provided files. It also does not work with Google cardboard yet, I am currently working on this problem.

About Patrick Williams

You Might Also Like

7 comments

  1. Afternoon. This is an excellent tutorial which I am bookmarking for when I return home.

    However because normally with written instructions I screw up somewhere. Have you considered making a tutorial video so the user can see the process visually? The odd screenshot is also useful (as you have already done). Thank you again for the method though.

  2. I got to the stage where you edit the Gradle file with its dependencies. When i try to build i keep getting an error pointing to ” compile (‘com.thalmic:myosdk:0.10.+@aar’) ” .

    If you created a step by step video of this tutorial i think it would be rather useful. But other than that thanks for getting me this far.

    1. Oh, you need to change
      url ‘/myorepository’

      To the path where you put your Android Myo SDK

  3. my vrtoolkit plugin cardboard not working, is it for laucher of our game scene? because when i remove it the apps just stuck in customscan activity

    1. Unfortunately, as stated in the post, I do not know how to make this work for VR. Cardboard uses a compressed JAR file that is uneditable and therefore you cannot make a listener that sends messages with it.

  4. This is a great tutorial! Thanks to you, I could compile a Unity based android app with Myo somehow, but it doesn’t work yet. The problem is :

    8329-29653/com.android.game I/Unity﹕ NullReferenceException: Object reference not set to an instance of an object at MessageManager.MyoRotation (System.String param) [0x00000] in :0 (Filename: Line: -1)

    What could be the problem? Can you please give me any advice?

    1. Never mind. It was just my misunderstanding, so I could fix it somehow.
      Now my app with Myo works fine. Thank you!

Comments are closed.