Flutter_blue: Connect taking too long

Created on 15 Sep 2020  路  2Comments  路  Source: pauldemarco/flutter_blue

After scanning some devices and calling the connect method, it takes more than 1 minute sometimes, and sometimes it never connects. Also, something that could be related, the state stream doesn't look correct: it's either "connected" or "disconnected". I'm never able to see the "connecting" or "disconnecting" state, which makes it harder to know if there is a connection establishment in progress, or something happened and it will not connect.

Sample repository: https://github.com/HugoSart/flutter_blue_hangs

Most helpful comment

Same situation for me.
Sometimes it connects in 5 secs and sometimes it connects in more than 5 attempts.
this is my connect method

  Future<void> connectTimeout(ScanResult result) async {
    await result.device
        .connect(autoConnect: true)
        .timeout(Duration(seconds: 5), onTimeout: () {
      log('${result.device.name} connecting timeout');
      result.device.disconnect();
        }).then((data) async {
          //TODO no lo puedo hacer ac谩 porque se repite mucho
      await for (BluetoothDeviceState r in result.device.state) {
        log('${result.device.name} state: ${r}');
        if (r == BluetoothDeviceState.connected) {
          checkConnectedImei(result.device);
        }
      }

    }).catchError((e) {
      log(' ${result.device.name}  connected state: ${result.device.state} $e');
    }).whenComplete(()  {});
  }

All 2 comments

Same situation for me.
Sometimes it connects in 5 secs and sometimes it connects in more than 5 attempts.
this is my connect method

  Future<void> connectTimeout(ScanResult result) async {
    await result.device
        .connect(autoConnect: true)
        .timeout(Duration(seconds: 5), onTimeout: () {
      log('${result.device.name} connecting timeout');
      result.device.disconnect();
        }).then((data) async {
          //TODO no lo puedo hacer ac谩 porque se repite mucho
      await for (BluetoothDeviceState r in result.device.state) {
        log('${result.device.name} state: ${r}');
        if (r == BluetoothDeviceState.connected) {
          checkConnectedImei(result.device);
        }
      }

    }).catchError((e) {
      log(' ${result.device.name}  connected state: ${result.device.state} $e');
    }).whenComplete(()  {});
  }

In some cases, you can solve it just by setting [autoConnect: false]. I don't know why it works, but it works pretty well.

https://github.com/pauldemarco/flutter_blue/issues/106#issuecomment-467697537

Was this page helpful?
0 / 5 - 0 ratings

Related issues

a-v-ebrahimi picture a-v-ebrahimi  路  4Comments

adamk22 picture adamk22  路  5Comments

ekuleshov picture ekuleshov  路  4Comments

pauldemarco picture pauldemarco  路  3Comments

RyanYANG52 picture RyanYANG52  路  6Comments