Search Unity

Calling Java methods doesn't seem to work

Discussion in 'Android' started by TeamDefiant, Mar 16, 2018.

  1. TeamDefiant

    TeamDefiant

    Joined:
    Mar 29, 2017
    Posts:
    50
    Hello, I'm accessing a jar file which contains an SDK to control an Epson Moverio BT350 AR headset.

    I don't get any errors when running my code, but I also don't get the desired effect on the device. I'm trying to disable some hardware buttons, but nothing is happening. (I.E. the buttons remain activated.)

    My C# code is as follows.
    Code (CSharp):
    1. #if ( UNITY_ANDROID && !UNITY_EDITOR )
    2.     AndroidJavaObject activityContext = null;
    3.  
    4.     // First, obtain the current activity context
    5.     using (AndroidJavaClass activityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
    6.     {
    7.         activityContext = activityClass.GetStatic<AndroidJavaObject>("currentActivity");
    8.     }
    9.  
    10.     using (AndroidJavaClass pluginClass = new AndroidJavaClass("com.epson.moverio.btcontrol.Bt3sCustomKey"))
    11.     {
    12.         if (pluginClass != null)
    13.         {
    14.             System.IntPtr getMethodID = AndroidJNIHelper.GetMethodID(pluginClass.GetRawClass(), "isKeyEnable", "(I)Z", false);
    15.             System.IntPtr setMethodID = AndroidJNIHelper.GetMethodID(pluginClass.GetRawClass(), "setKeyEnable", "(IZ)Z", false);
    16.  
    17.             Debug.Log("*****-----***** Disabling buttons");
    18.             Debug.Log( "Is enabled? " + AndroidJNI.CallBooleanMethod(pluginClass.GetRawObject(), getMethodID, makeJParamList(BACK)) );
    19.  
    20.             Debug.Log( "Success? " + AndroidJNI.CallBooleanMethod(pluginClass.GetRawObject(), setMethodID, makeJParamList(BACK, false)) );
    21.  
    22.             Debug.Log( "Is enabled? " + AndroidJNI.CallBooleanMethod(pluginClass.GetRawObject(), getMethodID, makeJParamList(BACK)) );
    23.             }
    24.     }
    25. #endif
    26.  
    The logcat produces the below.
    I'd expect the logcat to display the following (trimmed for simplicity)
    Any ideas what's wrong, or is this an issue with the third party jar file?
    Here's the code for the setKeyEnable method in the jar file. (I CANNOT CHANGE THIS CODE) As you can see, it has its own logging, so I'd expect to see something in the logcat, but nothing appears!

    Code (CSharp):
    1. public boolean setKeyEnable(int PhyKey, boolean enableKey)
    2.   {
    3.     boolean result = true;
    4.     if (mPhyKeyMap.get(Integer.valueOf(PhyKey)) == null)
    5.     {
    6.       Log.e(TAG, "setKeyEnable(): Invalid PhyKey: " + PhyKey);
    7.       return false;
    8.     }
    9.     boolean currentStatus = isKeyEnable(PhyKey);
    10.     if (currentStatus != enableKey) {
    11.       try
    12.       {
    13.         Intent intent = new Intent("Bt3s.CustomkeyEnable");
    14.         intent.putExtra("LinuxPhyKey", (String)mPhyKeyMap.get(Integer.valueOf(PhyKey)));
    15.         intent.putExtra("AndroidPhyKey", PhyKey);
    16.         intent.putExtra("NewStatus", enableKey);
    17.         this.mContext.sendBroadcast(intent);
    18.       }
    19.       catch (Exception e)
    20.       {
    21.         Log.e(TAG, "Fail to setKeyEnable(): " + e.toString());
    22.         result = false;
    23.       }
    24.     } else {
    25.       Log.d(TAG, "setKeyEnable():" + KeyEvent.keyCodeToString(PhyKey) + "is now " + enableKey);
    26.     }
    27.     return result;
    28.   }
     
    Last edited: Mar 16, 2018
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,735
    The setKeyEnable method is not static, so I think you need to use AndroidJavaObject to create an instance of Java class.
     
  3. TeamDefiant

    TeamDefiant

    Joined:
    Mar 29, 2017
    Posts:
    50
    The false at the end of the getMethodID method call denotes non-static, however I've tried the AndroidJavaObject way of doing it, and I got the exact same result. No errors in logcat, no updates to device.
     
  4. TeamDefiant

    TeamDefiant

    Joined:
    Mar 29, 2017
    Posts:
    50