Skip to content

Commit

Permalink
[Fix] cancelWhenScanDone must handle error cases
Browse files Browse the repository at this point in the history
  • Loading branch information
chipweinberger committed Jan 6, 2024
1 parent 9816e11 commit 213d817
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/src/flutter_blue_plus.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<BmScanResponse?> outputStream = removeIfGone != null
Expand All @@ -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
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 213d817

Please sign in to comment.