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:
- Unity (duh)
- Myo Armband (another duh)
- 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.
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.
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)
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.
- 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.
- 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
- 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.