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
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
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