PhilipsHue / flutter_reactive_ble

Flutter library that handles BLE operations for multiple devices.
https://developers.meethue.com/
Other
661 stars 321 forks source link

Cant connect after bluetooth is off/on #822

Closed AdemYavuzCelik closed 3 months ago

AdemYavuzCelik commented 9 months ago

BluetoothReactiveDeviceCubit({required this.deviceId}) : super(BluetoothReactiveDeviceState.initial(deviceId)) { log.fine('$this Created'); Future.delayed(Duration.zero, () async { _flutterReactiveBle = FlutterReactiveBle(); await _flutterReactiveBle.initialize(); _bluetoothOnOffStream = _flutterReactiveBle.statusStream.listen((status) async { log.fine('bt status: $status'); if (status == BleStatus.poweredOff) { await _blueDeviceStateStream?.cancel(); emit(const BluetoothReactiveDeviceState.disconnected()); } else if (status == BleStatus.ready) { log.fine('connect state: connect called'); await _blueDeviceStateStream?.cancel(); _blueDeviceStateStream = _flutterReactiveBle .connectToDevice( id: deviceId, connectionTimeout: const Duration(seconds: 5), ) .listen((connectionState) async { if (connectionState.connectionState == DeviceConnectionState.connected) { discoverServices(); } else if (connectionState.connectionState == DeviceConnectionState.disconnected) { emit(const BluetoothReactiveDeviceState.disconnected()); } log.fine('connect state: $connectionState'); }, onError: (Object error) { log.warning('connect state error: $error'); emit(const BluetoothReactiveDeviceState.disconnected()); }, onDone: () { log.fine('connect state done'); }); } }); }); }

"This is my code. In the cases of turning Bluetooth on and off, after the 'connect' call, I sequentially receive the 'connecting' state, then the 'disconnected (failure: GenericFailure(code: ConnectionError.failedToConnect, message: "Unknown error"))' state, and finally the 'onDone' state. What is the reason? Am I missing something?"

I/flutter (13530): FINE | BluetoothReactiveDeviceCubit | connect state: connect called I/flutter (13530): FINE | BluetoothReactiveDeviceCubit | connect state: ConnectionStateUpdate(deviceId: C3:7B:AC:A5:BB:C3, connectionState: DeviceConnectionState.connecting, failure: null) I/flutter (13530): FINE | BluetoothReactiveDeviceCubit | connect state: ConnectionStateUpdate(deviceId: C3:7B:AC:A5:BB:C3, connectionState: DeviceConnectionState.connecting, failure: null) I/flutter (13530): FINE | BluetoothReactiveDeviceCubit | connect state: ConnectionStateUpdate(deviceId: C3:7B:AC:A5:BB:C3, connectionState: DeviceConnectionState.disconnected, failure: GenericFailure(code: ConnectionError.failedToConnect, message: "Unknown error")) I/flutter (13530): FINE | BluetoothReactiveDeviceCubit | connect state done

otopba commented 8 months ago

Same here

Taym95 commented 3 months ago

This is not library issue but device issues, please make sure you call disconnect from app and try to scan and connect again.