我想让蓝牙在后台工作(连接到设备)。但我收到以下错误:
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>
重现问题非常简单:
创建 Flutter 应用程序
添加以下包:
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),
)
提前感谢您的帮助