Callback from native Java to Unity's C# script high latency

Viewed 173

I'm developing a Unity Android project that use Usb OTG to communicate with a midi device. It working pretty well except there is ~1 sec of latency.

CallStack example :

Unity Script -> Java code -> Cpp code

I found this solution on the android github (here) I just adapt it to work with unity :

I changed this function :

 private void onNativeMessageReceive(final byte[] message) {
        // Messages are received on some other thread, so switch to the UI thread
        // before attempting to access the UI
        runOnUiThread(new Runnable() {
            public void run() {
                showReceivedMessage(message);
            }
        });
    }

To :

    private void onNativeMessageReceive(final byte[] message) {
        Log.i(LOGTAG, "MessageReceived");
        midiCallback.MidiMessage(message);
    }

The midiCallback is an object of type MidiCallBack which is an interface :

public interface MidiCallback {
    void MidiMessage(byte[] data);
}

On Unity side I have an class who inherits from MidiCallBack :

    public class UnityMidiCallBack : AndroidJavaProxy
    {
        public UnityMidiAndroidCallBack() : base("com.test.unitymidianrdoid.MidiCallback")
        {
            
        }
        
        public void MidiMessage(byte[] message)
        {
            Debug.Log("newMessage);
        }
    }

I do not undersand why there is latency.

0 Answers
Related