chipweinberger / flutter_blue_plus

Flutter plugin for connecting and communicationg with Bluetooth Low Energy devices, on Android, iOS, macOS
Other
791 stars 479 forks source link

[Bug]: scan errors are not passed to scanResults stream #707

Closed timalenustmf closed 1 year ago

timalenustmf commented 1 year ago

Requirements

Have you checked this problem on the example app?

No

FlutterBluePlus Version

1.29.6

Flutter Version

2.19.6

What OS?

Android

OS Version

Android 13

Bluetooth Module

/

What is your problem?

We're noticing in our crash and error reporting (Sentry) a lot of FlutterBluePlusExceptions thrown with the error code SCAN_FAILED_APPLICATION_REGISTRATION_FAILED. From the breadcrumbs it seems to have to do with a bluetooth scan still being initiated after the Android smartphone screen is turned off and our app is put into a dreaming state by the OS.

The exception seems to be thrown inside the listener on the output stream. In the file https://github.com/boskokg/flutter_blue_plus/blob/master/lib/src/flutter_blue_plus.dart on line 276.

// listen & push to `scanResults` stream
    _scanSubscription = outputStream.listen((BmScanResponse? response) {
      if (response == null) {
        // if null, this is just a periodic update to remove old results
        if (output._removeWhere((elm) => DateTime.now().difference(elm.timeStamp) > removeIfGone!)) {
          _scanResults.add(List.from(output)); // push to stream
        }
      } else {
        // failure?
        if (response.success == false) {
          throw FlutterBluePlusException(_nativeError, "scan", response.errorCode, response.errorString);
        }

Because the exception gets thrown inside the listener there is no way for our code to catch it and handle it. There should be no exception thrown here.

Logs

//logs are not relevant
chipweinberger commented 1 year ago

fixed 1.29.7

timalenustmf commented 1 year ago

fixed 1.29.7

thanks for the really quick fix