Can you explain how to communicate from an application A written in flutter to an application written in Android B. Is possible use EventChannels for this purpose? If it is possible this communication is bidirectional?
An example is here (FLUTTER APP A):
import 'dart:async';
import 'package:flutter/services.dart';
class MyEventChannel {
static const MethodChannel _channel =
const MethodChannel('my_event_channel');
static Future<String> get platformVersion async {
final String version = await _channel.invokeMethod('getPlatformVersion');
return version;
}
static Stream<String> get messageStream {
return _channel.receiveBroadcastStream('message');
}
}
void _sendMessage(String message) {
MyEventChannel.platformVersion.then((version) {
if (version == 'Android') {
MyEventChannel._channel.invokeMethod('sendMessage', message);
}
});
}
Android APP B:
import android.content.Context;
import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;
public class MyEventChannel implements MethodCallHandler {
private final Context context;
private MyEventChannel(Context context) {
this.context = context;
}
public static void registerWith(Registrar registrar) {
final MethodChannel channel = new MethodChannel(registrar.messenger(), "my_event_channel");
channel.setMethodCallHandler(new MyEventChannel(registrar.context()));
final EventChannel eventChannel = new EventChannel(registrar.messenger(), "message");
eventChannel.setStreamHandler(new EventChannel.StreamHandler() {
@Override
public void onListen(Object args, EventChannel.EventSink events) {
// Handle messages received from Flutter
}
@Override
public void onCancel(Object args) {
// Handle cancel event
}
});
}
@Override
public void onMethodCall(MethodCall call, Result result) {
if (call.method.equals("getPlatformVersion")) {
result.success("Android");
} else if (call.method.equals("sendMessage")) {
// Handle send message event
} else {
result.notImplemented();
}
}
}
We can have A send some message to B, and in other cases b send something to A? How it works this mechanism. There is poor documentation about this mechanism.