flutter_blue 和后台服务:MissingPluginException 在通道上找不到实现

问题描述 投票:0回答:2

我想让蓝牙在后台工作(连接到设备)。但我收到以下错误:

E/flutter (15225): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: MissingPluginException(No implementation found for method isAvailable on channel plugins.pauldemarco.com/flutter_blue/methods)
E/flutter (15225): #0      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:156:7)
E/flutter (15225): <asynchronous suspension>
E/flutter (15225): #1      FlutterBlue._setLogLevelIfAvailable (package:flutter_blue/src/flutter_blue.dart:77:9)
E/flutter (15225): <asynchronous suspension>

重现问题非常简单:

  1. 创建 Flutter 应用程序

  2. 添加以下包:

    flutter 酒吧添加 flutter_blue

    flutter pub 添加 flutter_background_service

然后启动后台服务并触发

    void main() {
      WidgetsFlutterBinding.ensureInitialized();
      FlutterBackgroundService.initialize(onStart);
      runApp(MyApp());
    }

    void onStart() {
      WidgetsFlutterBinding.ensureInitialized();
      final service = FlutterBackgroundService();
      service.onDataReceived.listen((event) {
        if (event!["bluetooth"] == "start") {
          FlutterBlue flutterBlue = FlutterBlue.instance;
          flutterBlue.startScan(timeout: Duration(seconds: 4));
    
          var subscription = flutterBlue.scanResults.listen((results) {
            for (ScanResult r in results) {
              print('${r.device.name} found! rssi: ${r.rssi}');
            }
          });
    
          flutterBlue.stopScan();
          return;
        }
    
      });
    
      service.setForegroundMode(false);
    }


    IconButton(
      onPressed: (){
        FlutterBackgroundService().sendData({"bluetooth": "start"},);
        },
      icon: Icon(Icons.bluetooth),
    )

提前感谢您的帮助

flutter bluetooth background-service
2个回答
0
投票

使用更新的 FlutterBluePlus 插件...

这几乎是一个直接替换:您所要做的就是在代码中将

FlutterBlue
替换为
FlutterBluePlus
...


0
投票

就我而言,这个包运行良好。我在本地修改了这个包,在Android上运行良好。由于这个包是新的,我认为它仍在进行中。

© www.soinside.com 2019 - 2024. All rights reserved.