Call function From JAR Lib ?

Hi …

I created a small library On Eclipse to turn Wifi off , and I wanna to call it from Unity

I read a lot and tried a lot of codes but noting work with me , please help me .

Code On Eclipse :

package com.example.wifimanager;

import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Bundle;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    
    static public String test()
    {
    	return "testing message";
    }
    
	public void setEnabled()
	{
		WifiManager wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE); 
		wifiManager.setWifiEnabled(false);
	}
}
  • then exported as JAR file and put it in Plugins/Android/wifiManager.jar

then created a c# script :

	private string msg = "";
	void OnGUI ()
	{
		GUI.Label(new Rect(250,10,500,80),msg);

		if (GUI.Button (new Rect (0, 0, 200, 150), "Test 1")) 
		{
     		AndroidJavaClass cls_jni = new AndroidJavaClass("com.example.wifimanager.MainActivity");
			using(AndroidJavaClass cls_UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) 
			{
			    using(AndroidJavaObject obj_Activity = cls_UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity")) 
			    {
					msg = cls_jni.CallStatic<string>("test");
					cls_jni.CallStatic("setEnabled");
			    }
			}
		}

		if (GUI.Button (new Rect (0, 160, 200, 150), "Test 2")) 
		{
			AndroidJavaClass unityPlayer = new AndroidJavaClass ("com.unity3d.player.UnityPlayer");
			AndroidJavaObject mCurrentActivity = unityPlayer.GetStatic<AndroidJavaObject> ("currentActivity");
			AndroidJavaClass javaClass = new AndroidJavaClass("com.example.wifimanager.MainActivity");
			//javaClass.CallStatic("setEnabled");
			javaClass.Call("setEnabled");
		}


		if (GUI.Button (new Rect (Screen.width-200, 0, 200, 150), "QUIT")) {
			Application.Quit();
		}
	}

but noting work , I need to get the string from the test function and to call the SeEnabled function

any idea ??

Thank you …

I’m not very experienced with Java. I did some things with JNI in the past. I think your main problem is how to get an actual reference to your MainActivity instance. As you know the UnityPlayer activity has the static “currentActivity” field. It’s probably the easiest approach to also implement such a static field in your activity which you can initialize with “this” in onCreate

Now you should be able to use “GetStatic” in Unity on your activity class to get the reference to the instance of your activity. With that instance you can then use “Call” to call your method.

However it’s probably the easiest way to keep things on the Unity side as simple as possible. So you could handle that static field inside your Java class and provide a static method for Unity.

In the end you wouldn’t need the Java class at all. You could call “getSystemService” directly using JNI on Unity’s activity instance and you would get back a reference to the WifiManager.

So without any Java-side class you should be able to do

using(var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
    string wifiServiceName = unityPlayer.Get<string>("WIFI_SERVICE");
    using(var wifiManager = unityPlayer.Call<AndroidJavaObject>("getSystemService", wifiServiceName))
    {
        wifiManager.Call("setWifiEnabled", false);
    }
}

ps: I’ve written this here on UA, so it might contain typos ^^.

Hello,

Have you succeeded in turning of wifi from Unity on Android?
I try to do the same thing without success.

If I do:

using(var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) {
     string wifiServiceName = unityPlayer.Get<string>("WIFI_SERVICE");
     using(var wifiManager = unityPlayer.Call<AndroidJavaObject>("getSystemService", wifiServiceName))
     {
         wifiManager.Call("setWifiEnabled", false);
     }  }

I have an error saying that WIFI_SERVICE doesn’t exist.

If I do:

		using (AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity"))
		{
			using (var wifiManager = activity.Call<AndroidJavaObject>("getSystemService","wifi"))
			{
				wifiManager.Call<AndroidJavaObject>("setWifiEnabled", false);
			}	
		}

I have an error saying that setWifiEnabled is not a function, (nor a static function if I do CallStatic).

I have my manifest.xml correctly merged, I can check that I have all the permissions on the application manager.

I spent few hours trying to figure out how to do that and I am stuck!

Thanks a lot for your help,

Benjamin

There are a few issues with the original code you posted.

com.unity3d.player.UnityPlayer is not an activity. it is a helper class, initialized by Unity that contains a static currentActivity field that is the main activity.

Also, you initialize an AndroidJavaClass object for your own activity and then call a method on it. This is an incorrect use; if you need to access non-static methods, you must create an AndroidJavaObject.

Here’s the code (didn’t test this!) that should work:

if (GUI.Button (new Rect (0, 0, 200, 150), "Test 1")) 
{
    using (AndroidJavaClass cls_UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) 
    {
        // currentActivity will be a reference to your MainActivity class
        using (AndroidJavaObject obj_Activity = cls_UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity")) 
         {

            // test is a static method - invoked with CallStatic
            msg = obj_Activity.CallStatic<string>("test");

            // setEnabled is an instance method - invoked using Call
            obj_Activity.Call("setEnabled");
         }
    }
}

NOTE: you’ll have to define your MainActivity as an activity in the AndroidManifest, such that it will override the default activity that Unity provides.