使用flutter控制蓝牙通讯_Android与flutter 的消息通讯

Flutter 与 Android iOS 原生的通信有以下三种方式

BasicMessageChannel:

用于传递字符串和半结构化的信息,Flutter和平台端进行消息数据交换时候可以使用。

MethodChannel:

用于传递方法调用(method invocation),Flutter和平台端进行直接方法调用时候可以使用。

EventChannel:

用于数据流(event streams)的通信,Flutter和平台端进行事件监听、取消等可以使用。

以下以BasicMessageChannel为例,介绍以下用法:

先创建一个工具类

public class PlatformUtil {

private static String Default_Message_Key = "key_platform_default";

public PlatformUtil(BinaryMessenger binaryMessenger) {

this.binaryMessenger = binaryMessenger;

}

private BasicMessageChannel messageChannel;

private MethodChannel methodChannel;

private EventChannel eventChannel;

private BinaryMessenger binaryMessenger;

public BasicMessageChannel createBasicMessageChannel(String key) {

if (messageChannel == null)

messageChannel = new BasicMessageChannel<>(binaryMessenger, key, StandardMessageCodec.INSTANCE);

return messageChannel;

}

public BasicMessageChannel createBasicMessageChannel() {

return createBasicMessageChannel(Default_Message_Key);

}

public MethodChannel createMethodChannel(String key) {

if (methodChannel == null)

methodChannel = new MethodChannel(binaryMessenger, key, StandardMethodCodec.INSTANCE);

return methodChannel;

}

public MethodChannel createMethodChannel() {

return createMethodChannel(Default_Message_Key);

}

public EventChannel createEventChannel(String key) {

if (eventChannel == null)

eventChannel = new EventChannel(binaryMessenger, key, StandardMethodCodec.INSTANCE);

return eventChannel;

}

public EventChannel createEventChannel() {

return createEventChannel(Default_Message_Key);

}

}

创建一个FlutterActicity

public class TestAty extends FlutterActivity {

@Override

protected void onCreate(@Nullable Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

addFlutterView();

requestData();

}

private PlatformUtil platformUtil;

private void addFlutterView(){

FlutterView flutterView = Flutter.createView(this,getLifecycle(),"test1");

FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(

FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);

platformUtil = new PlatformUtil(flutterView);

addContentView(flutterView,layoutParams);

}

protected void requestData() {

//test send msg

JSONObject object = new JSONObject();

try {

object.put("code","200");

object.put("message","send");

object.put("data","i want to send message to you");

}catch (Exception e){

e.toString();

}

platformUtil.createBasicMessageChannel().send(object.toString(), new BasicMessageChannel.Reply() {

@Override

public void reply(@Nullable Object reply) {

L.e("receive--"+reply.toString());

}

});

platformUtil.createBasicMessageChannel().setMessageHandler(new BasicMessageChannel.MessageHandler() {

@Override

public void onMessage(@Nullable Object message, @NonNull BasicMessageChannel.Reply reply) {

L.e("receive--"+message.toString());

try {

object.put("code","200");

object.put("message","receive");

object.put("data","i know");

}catch (Exception e){

e.toString();

}

reply.reply(object.toString());

}

});

//test call method

//test register event

}

}

创建一个dart工具类

import 'package:flutter/services.dart';

import 'dart:convert';

const String default_key_code = "key_platform_default";

var messageChannel = const BasicMessageChannel(default_key_code, StandardMessageCodec());

void sendMessage() async{

Map sourceMap = new Map();

sourceMap.putIfAbsent("code", ()=>("200"));

sourceMap.putIfAbsent("message", ()=>("test"));

sourceMap.putIfAbsent("data", ()=>("testData"));

String jsonString = jsonEncode(sourceMap);

print(jsonString);

try{

String reply = await messageChannel.send(jsonString);

print("dart:receive--"+reply.toString());

}on PlatformException catch(e){

print(e);

}

}

void receiveMessage(){

messageChannel.setMessageHandler((message) async{

print("dart:receive--+$message");

TestMode testMode = new TestMode("200", "receive", "222");

return jsonEncode(testMode.toJson());

});

}

class TestMode{

String code;

String message;

String data;

TestMode(this.code, this.message, this.data);

TestMode.fromJson(Map json){

code = json['code'];

message = json['message'];

data = json['data'];

}

Map toJson() {

final Map data = new Map();

data['code'] = this.code;

data['message'] = this.message;

data['data'] = this.data;

return data;

}

}

创建一个dart widget

import 'package:flutter/material.dart';

import 'platform_util.dart';

class MyTestApp extends StatelessWidget{

@override

Widget build(BuildContext context) {

receiveMessage();

sendMessage();

return new MaterialApp(

title: 'welcome use flutter',

home: new Scaffold(

appBar: new AppBar(

title: new Text('welcome title'),

),

body: new Center(

child: new Text('welcome body'),

),

),

);

}

}

运行得到如下结果:

2020-02-06 10:43:15.808 14590-14929/com.aa.bb I/flutter: dart:receive--+{"code":"200","message":"send","data":"i want to send message to you"}

2020-02-06 10:43:15.813 14590-14929/com.aa.bb I/flutter: {"code":"200","message":"test","data":"testData"}

2020-02-06 10:43:15.827 14590-14590/com.aa.bb E/=====Leen=====: receive--{"code":"200","message":"test","data":"testData"}

2020-02-06 10:43:16.247 14590-14590/com.aa.bb E/=====Leen=====: receive--{"code":"200","message":"receive","data":"222"}

2020-02-06 10:43:16.418 14590-14929/com.aa.bb I/flutter: dart:receive--{"code":"200","message":"receive","data":"i know"}

你可能感兴趣的:(使用flutter控制蓝牙通讯)