Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scanning for devices #595

Open
ahoelzemann opened this issue May 6, 2021 · 1 comment
Open

Scanning for devices #595

ahoelzemann opened this issue May 6, 2021 · 1 comment

Comments

@ahoelzemann
Copy link

ahoelzemann commented May 6, 2021

Hello everyone,

I wrote the following method to scan for nearby bluetooth devices and connect to an earlier saved device.

  Future<dynamic> start_ble_scan() async {
    Completer completer = new Completer();

    if (_mydevice != null) {
      bool connected = await _mydevice.isConnected();
      print("DeviceState: " + connected.toString());
      if (connected) {
        try {
          await _responseSubscription?.cancel();
          await _characSubscription?.cancel();
          await _condeviceStateSubscription?.cancel();
          await _mydevice.disconnectOrCancelConnection();
          _mydevice = null;
          _currentDeviceConnected = false;
        } catch (e) {
          print("Disconnecting device before new scan process");
        }
      }
    }

    SharedPreferences prefs = await SharedPreferences.getInstance();
    String savedDevice = prefs.getString("Devicename");
    String savedIdentifier = prefs.getString("macnum");
    bool alreadyStoppedScanning = false;

    _bleonSubscription = _activateBleManager
        .startPeripheralScan()
        .timeout(Duration(seconds: 30), onTimeout: (timeout) async {
      if (!alreadyStoppedScanning) {
        await _activateBleManager.stopPeripheralScan();
      }
    }).listen(
      (data) async {
        if ((data.peripheral.name == savedDevice) ||
            (data.peripheral.identifier == savedIdentifier)) {
          if (!alreadyStoppedScanning) {
            alreadyStoppedScanning = true;
            _mydevice = data.peripheral;
            print("Device found: " + _mydevice.toString());
            await _activateBleManager.stopPeripheralScan();
            _bleonSubscription?.cancel();
          } 

        }
      },
      onError: (err) {
        print('Error!: $err');
      },
      cancelOnError: true,
      onDone: () async {
        print("Device found: " + _mydevice.toString());
        completer.complete(true);
      },
    );
    return completer.future;
  }

however, this method doesn't always returns the desired result. Sometimes I jump directly into the timeout which stops the scan immediately and does not connect my device.

Someone has an idea what I'm doing wrong?

thanks and best regards!

@bretmh
Copy link

bretmh commented May 8, 2021

Please create a test project and implement the breaking code. I would try to only show an example of this with FlutterBleLib and no other packages / noisy code.

You have two places where you stop the scan and one place where you disconnect/cancel connections.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants