I keep finding answered (and many unanswered) questions on how to pass from Unity C# to Java but not the other way around.
So in Unity Plugins/Android i have an aar library that i’m creating as a wrapper for an sdk.
The android app made in unity has to connect with the other device over bluetooth.
In Android Studio i have a PhoneBlueToothActivity where i turn bluetooth and discovery on then i fetch the bonded devices and put their names in an ArrayList and i first want to pass this on to my UnityActivity and then from there to Unity
PhoneBlueToothActivity
Intent sendDevicesToUnity = new Intent(PhoneBluetoothActivity.this, UnityActivity.class);
sendDevicesToUnity.putStringArrayListExtra("bondedDevices", bondedDevices);
UnityActivity
public void AndroidSendDevicesToUnity(){
Intent getBondedDevices = getIntent();
ArrayList<String> list = getBondedDevices.getStringArrayListExtra("bondedDevices");
//Todo: pass this to Unity
}
I’m new to using Android studio, Java and so far i’ve used UnityPlayer.UnitySendMessage but that can’t pass an arrayList of strings so i’m not sure how to handle this and there isn’t much to find on this topic.
Should i put the arrayList into 1 string, use SendMessage and then filter it out or are there better ways?
Since this question was bumped already, I’ll post an answer.
An ArrayList is a Java class, so it can not be directly translated or converted to a C# managed class or array. The easiest solution may be to call ToArray on the ArrayList on the java side which will create a Java native array of the elements. With this it should be possible to use AndroidJNIHelper.ConvertFromJNIArray to actually convert the java array into a C# array. Of course this only works if the types can be marshalled. Though converting java strings to C# strings should work.
If this doesn’t work for some reason, you can always “use” the ArrayList as it is. However every interaction with the Java class has to be done through the AndroidJavaObject instance of that ArrayList. For example you can call the size method of the ArrayList to get the element count. Then in a loop you can call the get method with a valid index to get one element at a time.
So assuming you have a reference to your ArrayList as AndroidJavaObject called “arrayList” in C#, you should be able to do something like:
int count = arrayList.Call<int>("size");
List<string> list = new List<string>(count);
for(int i = 0; i < count; i++)
{
list.Add(arrayList.Call<string>("get", i));
}
Though using the JNI bridge there are generally a lot things that can go wrong. So makes sure you wrap everything in a try catch block and also dispose the objects properly.
On the Java side, I’m using a USB SDK to enumerate USB devices attached to Android. I instantiate the library on the Unity side using AndroidJavaObject and then I do a call to a method that returns a java.util.ArrayList of device names. From that point in Unity code, I have an AndroidJavaObject representing a java.util.ArrayList that I don’t know how to get it’s elements from. It’s an ArrayList of java.lang.String, so in Unity that has to also be treated as a AndroidJavaObject. But then from that point I don’t know how to actually get the string data from it.