Flutter_blue: "Another scan is already in progress" exception (v.0.6.0+1)

Created on 21 Jun 2019  路  5Comments  路  Source: pauldemarco/flutter_blue

Hi,
I'm using:

_scanSubscription = _flutterBlue.scan(timeout: SCAN_TIMEOUT, withServices: withServices)
.listen((oneResult) {
        Utils.printDebug('BTDeviceConnection startScan: ScanResult received oneResult: device.id=' + oneResult.device.name);
        if (oneResult.device.id.id == deviceId) {
          // do stuffs with this device 
      _flutterBlue.stopScan();
      _scanSubscription.cancel();
        }
      }, onDone: () {
        Utils.printDebug('BTDeviceConnection startScan: onDone invoked');
      });

It seems to me tha the scan is never stopped before the timeout.
If I try to repeat this action before SCAN_TIMEOUT (20 seconds) it always causes:

Unhandled Exception: Exception: Another scan is already in progress.
FlutterBlue.scan (package:flutter_blue/src/flutter_blue.dart:80:7)

I tried both stopScan() and/or _scanSubscription.cancel(), but no effect.

Could you please help me?

Thanks

Most helpful comment

Fix below.

Don't throw an exception for now on scan(). Clear scan results and then continue as normal.

Example output of fix:

I/flutter ( 4692): You called scan again while a scan was already in progress.
I/flutter ( 4692):  Clearing scan result list and starting a new scan.
D/BluetoothAdapter( 4692): STATE_ON
I/flutter ( 4692): Found Device
D/BluetoothManager( 4692): getConnectedDevices
D/BluetoothGatt( 4692): connect() - device: 0C:2A:69:16:75:55, auto: false
D/BluetoothGatt( 4692): registerApp()
D/BluetoothGatt( 4692): registerApp() - UUID=ec62285f-22b3-404f-b386-a269117c2a11
D/BluetoothGatt( 4692): onClientRegistered() - status=0 clientIf=8
D/BluetoothManager( 4692): getConnectionState()
D/BluetoothManager( 4692): getConnectedDevices
D/BluetoothGatt( 4692): onClientConnectionState() - status=0 clientIf=8 device=0C:2A:69:16:75:55
D/FlutterBluePlugin( 4692): [onConnectionStateChange] status: 0 newState: 2
I/flutter ( 4692): Connected!
I/flutter ( 4692): Getting services/characteristics.
D/BluetoothGatt( 4692): discoverServices() - device: 0C:2A:69:16:75:55

Code to change... make sure to comment out the throw and then clear your list before you get new scan results to avoid duplicates.

   if (_isScanning.value == true) {      
      print("You called scan again while a scan was already in progress.\n Clearing scan result list and starting a new scan.");
      _scanResults.add(<ScanResult>[]);
      //throw Exception('Another scan is already in progress.');
    }

All 5 comments

I cannot let the scan to stop programmatically... Simply canceling the scan subscriptions seems not to be enough. Rolling back to 0.5 works fine.

My app shows known devices in a tab card widget and at each card-change I try to connect making a "scan_for_the_device_and_then_connect" process. Users can change cards quite quicly, so I can't wait the scan timeout before restarting the process, I need to force the scan to stop and then restart it.

Thank you

stopScan() seems to work for me in (0.6.0 +1)
Have you tried calling the stopScan in the dispose of the card to ensure the scan get's stopped when a user switches card?

Neither _flutterBlue.stopScan() nor _scanSubscription.cancel() are preventing this error for me on 0.6.0 +1.

_Exception (Exception: Another scan is already in progress.)

And according to this issue .stopScan() shouldn't be used directly anyway.

Fix below.

Don't throw an exception for now on scan(). Clear scan results and then continue as normal.

Example output of fix:

I/flutter ( 4692): You called scan again while a scan was already in progress.
I/flutter ( 4692):  Clearing scan result list and starting a new scan.
D/BluetoothAdapter( 4692): STATE_ON
I/flutter ( 4692): Found Device
D/BluetoothManager( 4692): getConnectedDevices
D/BluetoothGatt( 4692): connect() - device: 0C:2A:69:16:75:55, auto: false
D/BluetoothGatt( 4692): registerApp()
D/BluetoothGatt( 4692): registerApp() - UUID=ec62285f-22b3-404f-b386-a269117c2a11
D/BluetoothGatt( 4692): onClientRegistered() - status=0 clientIf=8
D/BluetoothManager( 4692): getConnectionState()
D/BluetoothManager( 4692): getConnectedDevices
D/BluetoothGatt( 4692): onClientConnectionState() - status=0 clientIf=8 device=0C:2A:69:16:75:55
D/FlutterBluePlugin( 4692): [onConnectionStateChange] status: 0 newState: 2
I/flutter ( 4692): Connected!
I/flutter ( 4692): Getting services/characteristics.
D/BluetoothGatt( 4692): discoverServices() - device: 0C:2A:69:16:75:55

Code to change... make sure to comment out the throw and then clear your list before you get new scan results to avoid duplicates.

   if (_isScanning.value == true) {      
      print("You called scan again while a scan was already in progress.\n Clearing scan result list and starting a new scan.");
      _scanResults.add(<ScanResult>[]);
      //throw Exception('Another scan is already in progress.');
    }

Same issue. +1 for fix proposed by @bretie

Was this page helpful?
0 / 5 - 0 ratings

Related issues

adamk22 picture adamk22  路  5Comments

FWeissenb picture FWeissenb  路  5Comments

RyanYANG52 picture RyanYANG52  路  6Comments

coduy96 picture coduy96  路  5Comments

ssaylanc picture ssaylanc  路  4Comments