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

本文介绍了如何使用Flutter与Android原生代码通过BasicMessageChannel进行通信,以此实现在Flutter应用中控制蓝牙通讯。详细展示了创建工具类、FlutterActivity、dart工具类以及dart widget的步骤,并给出了成功交互的日志输出。
摘要由CSDN通过智能技术生成

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 met

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值