Sunday, 31 January 2021

Trouble sending Uint8List from Flutter to Android[Java] using method channels

I have a method channel setup on Flutter to send Uint8List to Android[Java]. Android is receiving the data but it looks like the bytes are getting wrapped at 127.

Ex: if I send [254, 100, 32] from flutter, it shows up as [-2, 100, 32] in Android.

Basically, any value in the list over 127 is getting converted to a negative value.

Here is how I have the method channel setup on the Fluter side:

static const MethodChannel platform = MethodChannel(SEND_SERIAL_MESSAGE_TO_ANDROID);

Future<void> sendMessageToAndroid(List<int> message) async {

  final bool result = await platform.invokeMethod('parseMessage', message);
}

And this is how I am receiving it on the Android side

@Override
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
    super.configureFlutterEngine(flutterEngine);
    new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL)
            .setMethodCallHandler(
                    (call, result) -> {
                        if (call.method.equals("parseMessage")) {
                            byte[] message = call.arguments();
                            parseMessage(message);
                        } else {
                            result.notImplemented();
                        }
                    }

            );
}

private void parseMessage(byte[] message){
    Log.i(LOG_TAG, message.toString());
}

Question: how do I stop values over 127 in my Uint8List from getting converted to a negative value in Java?

Let me know if you need to see any more of my code.



from Trouble sending Uint8List from Flutter to Android[Java] using method channels

No comments:

Post a Comment