From 213d81709c1617992da04ee24d2170c5687e01e3 Mon Sep 17 00:00:00 2001 From: Chip Weinberger Date: Fri, 5 Jan 2024 20:19:06 -0600 Subject: [PATCH] [Fix] cancelWhenScanDone must handle error cases --- lib/src/flutter_blue_plus.dart | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/src/flutter_blue_plus.dart b/lib/src/flutter_blue_plus.dart index 5bcba638..60403d19 100644 --- a/lib/src/flutter_blue_plus.dart +++ b/lib/src/flutter_blue_plus.dart @@ -260,7 +260,7 @@ class FlutterBluePlus { _scanBuffer = _BufferStream.listen(responseStream); // invoke platform method - await _invokeMethod('startScan', settings.toMap()); + await _invokeMethod('startScan', settings.toMap()).onError((e,s) => _stopScan(invokePlatform: false)); // check every 250ms for gone devices? late Stream outputStream = removeIfGone != null @@ -282,8 +282,9 @@ class FlutterBluePlus { } else { // failure? if (response.success == false) { - _scanResults - .addError(FlutterBluePlusException(_nativeError, "scan", response.errorCode, response.errorString)); + var e = FlutterBluePlusException(_nativeError, "scan", response.errorCode, response.errorString); + _scanResults.addError(e); + _stopScan(invokePlatform: false); } // iterate through advertisements @@ -349,7 +350,7 @@ class FlutterBluePlus { /// This function simplifies cleanup, to prevent creating duplicate stream subscriptions. /// - this is an optional convenience function /// - prevents accidentally creating duplicate subscriptions on scan - void cancelWhenScanComplete(StreamSubscription subscription) { + static void cancelWhenScanComplete(StreamSubscription subscription) { FlutterBluePlus._scanSubscriptions.add(subscription); }