MOGA Controller support on Lollipop (In Unity)

Jacob Keane Uncategorized

So, I recently had an issue where a project I was working on needed to work with the MOGA controllers, but also needed to support Lollipop. (Android TV, etc).

However, the MOGA Plugin for Unity3d (https://www.assetstore.unity3d.com/en/#!/content/7305) has not been updated since Feb 04, and doesn’t work on Lollipop!

Here I’ll look at why this doesn’t work, and how I fixed the issue.

Analysis:

Well, first things first, check logcat for errors!

Well – there’s the problem, atleast! In Lollipop, bindService() was changed to no longer support implicit intents, and you must now call them with an explicit intent.

So, easiest fix, if you can, you could just drop your target platform to one lower than 21 (Lollipop), and this should still work!

However, due to needing to support Android TV and a few other reasons, I wasn’t able to do this, so, we’ll have to try a more.. creative solution!

Let’s take a look at where the error is coming from – We can see from the logcat it’s coming from Moga_Controller.init(), but after that it’s handled by Java.

Well, what’s in that function?

…Ah. It just called straight to the java code to handle the initializing there. And the source code for the java plugin isn’t available anywhere…

Well, let’s see if we can take a look – I extracted com.bda.controller.jar file, and uploaded Controller.class to http://www.showmycode.com/, an online Java decompiler

 

Solution:

Well, now we know what the init function is doing, we can replicate that but fix the intent!

To do this, we need to add the java line:  intent.setPackage("com.bda.pivot.mogapgp");

However, instead of creating a seperate jar just for this function, or messing with the original jar, I’ve just written it all using

This is essientally a re-write of the java code, with the following line added:

Which makes the intent explicit.

So, I rewrote the initiation Java code in C# -> Java, in the original init function, and added a line

Feel free to add this to any game your working on if you’re suffering from the same issue! Just go to Moga_Controller.cs and replace the public bool init() function with the following code:

This solved the issue!