"Converting Byte array is obsolete, use SByte array instead" even when using SByte array

Viewed 1355

I am calling this Java method from Unity C#. Even though I am sendings byte[] instead of byte[], I am getting an error that says otherwise. Any ideas?

Java method:

public void sendBytes(byte[] bytes) {
        if (_connectionThread != null) {
            Log.i(LOG_TAG, "Sending " + bytes.length + " byte(s).");
            _connectionThread.sendBytes(bytes);
        } else {
            Log.i(LOG_TAG, "Unable to send bytes because the connection thread is not initialized.");
        }
    }

C#:

public void SendBytes(byte[] bytes)
                    {
                        sbyte[] signedBytes = (sbyte[])(Array)bytes;
                        _bluetoothClient.Call("sendBytes", signedBytes);
                    }
2021-04-18 19:33:14.024 4733-4796/com.company.product W/Unity: AndroidJNIHelper: converting Byte array is obsolete, use SByte array instead
    UnityEngine.StackTraceUtility:ExtractStackTrace () (at /Users/bokken/buildslave/unity/build/Runtime/Export/Scripting/StackTrace.cs:37)
    UnityEngine.DebugLogHandler:LogFormat (UnityEngine.LogType,UnityEngine.Object,string,object[])
    UnityEngine.Logger:Log (UnityEngine.LogType,object)
    UnityEngine.Debug:LogWarning (object)
    UnityEngine._AndroidJNIHelper:ConvertToJNIArray (System.Array) (at /Users/bokken/buildslave/unity/build/Modules/AndroidJNI/AndroidJava.cs:1208)
    UnityEngine._AndroidJNIHelper:CreateJNIArgArray (object[]) (at /Users/bokken/buildslave/unity/build/Modules/AndroidJNI/AndroidJava.cs:1027)
    UnityEngine.AndroidJNIHelper:CreateJNIArgArray (object[]) (at /Users/bokken/buildslave/unity/build/Modules/AndroidJNI/AndroidJNI.bindings.cs:112)
    UnityEngine.AndroidJavaObject:_Call (string,object[]) (at /Users/bokken/buildslave/unity/build/Modules/AndroidJNI/AndroidJava.cs:410)
    UnityEngine.AndroidJavaObject:Call<sbyte> (string,sbyte[]) (at
1 Answers

It is probably using reflection to check the array type via .GetType() and Type.GetElementType(). Your coercion (which honesty: I'm surprised even works, but: array variance is complex) doesn't change the array type.

You could try allocating a new array (actually a sbyte[]), copy the data over, and see if that works. But this is obviously less efficient. If the API allows you to specify an array range (array+offset+count), you could lease an oversized array from the array pool and use that, to avoid the allocation step.

Related