diff --git a/lib/connector/meshcore_connector.dart b/lib/connector/meshcore_connector.dart index c378bff..191f74e 100644 --- a/lib/connector/meshcore_connector.dart +++ b/lib/connector/meshcore_connector.dart @@ -963,7 +963,7 @@ class MeshCoreConnector extends ChangeNotifier { await sendFrame(buildGetBattAndStorageFrame()); } catch (e) { // Reset flag on error to allow retry - _handleDisconnection(); + // Don't disconnect on battery request failure - it may be transient _batteryRequested = false; } } diff --git a/lib/screens/scanner_screen.dart b/lib/screens/scanner_screen.dart index 642ce1f..0d38d98 100644 --- a/lib/screens/scanner_screen.dart +++ b/lib/screens/scanner_screen.dart @@ -16,25 +16,37 @@ class ScannerScreen extends StatefulWidget { } class _ScannerScreenState extends State { - bool changedNavgation = false; + bool _changedNavigation = false; + late final VoidCallback _connectionListener; @override void initState() { super.initState(); final connector = Provider.of(context, listen: false); - connector.addListener(() { + _connectionListener = () { if (connector.state == MeshCoreConnectionState.disconnected) { - changedNavgation = false; - }else if (connector.state == MeshCoreConnectionState.connected && !changedNavgation) { - changedNavgation = true; - Navigator.of(context).push( - MaterialPageRoute( - builder: (context) => const ContactsScreen(), - ), - ); + _changedNavigation = false; + } else if (connector.state == MeshCoreConnectionState.connected && !_changedNavigation) { + _changedNavigation = true; + if (mounted) { + Navigator.of(context).push( + MaterialPageRoute( + builder: (context) => const ContactsScreen(), + ), + ); + } } - }); + }; + + connector.addListener(_connectionListener); + } + + @override + void dispose() { + final connector = Provider.of(context, listen: false); + connector.removeListener(_connectionListener); + super.dispose(); } @override