主要逻辑:
- Flutter端 EventChannel监听Native传来的消息
- Native通过创建EventChannel,给Flutter层发送消息
代码梳理
1、Flutter端创建EventChannel进行监听
import 'dart:async';
import 'package:flutter/services.dart';
class FlutterJlBluetoothPlugin {
// 工厂模式
factory FlutterJlBluetoothPlugin() =>_getInstance();
static FlutterJlBluetoothPlugin get instance => _getInstance();
static FlutterJlBluetoothPlugin? _instance;
MethodChannel _methodChannel;
EventChannel _eventChannel;
FlutterJlBluetoothPlugin._internal(this._methodChannel, this._eventChannel) {
_eventChannel.receiveBroadcastStream().listen(_onEvent, onError: _onError);
}
static FlutterJlBluetoothPlugin _getInstance() {
if (_instance == null) {
MethodChannel methodChannel = MethodChannel('flutter_jl_bluetooth_plugin');
EventChannel eventChannel = EventChannel('flutter_jl_bluetooth_plugi