Reputation: 319
I want to make bluetooth work (connection to a device) in the background. but I get the following error:
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>
To reproduce the problem it's very simple:
Create a Flutter app
Add the following packages:
flutter pub add flutter_blue
flutter pub add flutter_background_service
Then start the background service and trigger it
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),
)
Thank you in advance for your help
Upvotes: 2
Views: 929
Reputation: 341
In my case, This package is working fine. I have modified this package locally, and it is functioning well on Android. Since this package is new, I think it is still a work in progress.
Upvotes: 1
Reputation: 1
Use the FlutterBluePlus plugin which is more up to date...
It's almost a drop-in replacement : all you have to do is replace FlutterBlue
by FlutterBluePlus
in your code...
Upvotes: 0