Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Fadi · Jan 26, 2016 at 08:53 PM · androidpluginjar

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 ...

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Jan 26, 2016 at 09:19 PM

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 ^^.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Jan 26, 2016 at 09:31 PM 0
Share

If you still need to access your static method in your custom activity class, it should actually work the way you have it. However you should wrap your class in a using statement:

 using(AndroidJavaClass mainActivityClass = new AndroidJavaClass("com.example.wifimanager.$$anonymous$$ainActivity"))
 {
     msg = mainActivityClass.CallStatic<string>("test");
 }

JNI can be quite frustrating from time to time. As long as the parameter and return types are simple built-in types it's quite easy. It get's tricky with some custom Java types as the AndroidJavaClass / Object wrapper usually gets the signature of the method wrong, so you need to dig your way manually through the AndroidJNI functions to get the method manually.

With the JNI interface you actually should be able to perform any task on the Java side which doesn't require you to implement a custom class to receive some callbacks.

avatar image
0

Answer by I33N · Aug 24, 2016 at 08:56 AM

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by liortal · Aug 25, 2016 at 05:30 AM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

45 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity3D:Field text or type signature not found when i use GetStatic to reach a string inside a Jar android plugin 0 Answers

Calling static jar function from Unity3D 0 Answers

Unity3D:Field text or type signature not found when i use GetStatic to reach a string inside a Jar android plugin 0 Answers

Android plugins add View 1 Answer

Unity3D:Field text or type signature not found when i use GetStatic to reach a string inside a Jar android plugin 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges