Friday, 1 October 2021

AndroidJavaClass and AndroidJavaObject for C# .Net Framework

I have code in Unity that calls a function programmed in Java for Android using AndroidJavaClass and AndroidJavaObject and I would like to pass this code to a dll in C# .Net but I can't find an easy way to call Java functions using C# .Net since the AndroidJavaObject and AndroidJavaClass classes are exclusive to the Unity dll, here is an example of my code:

Java (Android):

public class DeviceManager {
    public interface DeviceListener {
        void DoSomething();
    }

    private DeviceListener deviceListener;

    public DeviceManager(DeviceListener deviceListener){
        this.deviceListener = deviceListener;
    }
    
    public String[] getSomeData(){
        return new String[]{};
    }
}

C# (Unity):

public class DeviceListener : AndroidJavaProxy
{
    DeviceManager deviceManager;

    public DeviceListener(DeviceManager manager) : base("com.myapp.DeviceManager$DeviceListener")
    {
        this.deviceManager = manager;
    }

    public void DoSomething();
}

public class DeviceManager
{
    private AndroidJavaObject jo;

    public DeviceManager(){
         jo = new AndroidJavaObject("com.myapp.DeviceManager", new DeviceListener(this));
    }

    public string[] GetData(){
        AndroidJavaObject returnedData = jo.Call<AndroidJavaObject>("getSomeData");
        return AndroidJNIHelper.ConvertFromJNIArray<string[]>(returnedData.GetRawObject());
    }
}

What is the best way to do that example in C# .Net instead of Unity?

EDIT: my main question is related to know how to get objects and call functions from an .aar file in .NET Framework inside a dll C# file for Unity



from AndroidJavaClass and AndroidJavaObject for C# .Net Framework

No comments:

Post a Comment