From 34a6b5d895f46d5b6c02345beabdefd6cbb29bda Mon Sep 17 00:00:00 2001 From: Winston Lowe Date: Wed, 28 Jan 2026 19:55:08 -0800 Subject: [PATCH 1/3] Added error catching to requestBatteryStatus to call _handleDisconnection when it fails update. Updated ScannerScreen to manage navigation state logic on connection. --- lib/connector/meshcore_connector.dart | 8 +++++- lib/screens/scanner_screen.dart | 37 +++++++++++++++++++-------- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/lib/connector/meshcore_connector.dart b/lib/connector/meshcore_connector.dart index de39d7e..c378bff 100644 --- a/lib/connector/meshcore_connector.dart +++ b/lib/connector/meshcore_connector.dart @@ -959,7 +959,13 @@ class MeshCoreConnector extends ChangeNotifier { if (!isConnected) return; if (_batteryRequested && !force) return; _batteryRequested = true; - await sendFrame(buildGetBattAndStorageFrame()); + try { + await sendFrame(buildGetBattAndStorageFrame()); + } catch (e) { + // Reset flag on error to allow retry + _handleDisconnection(); + _batteryRequested = false; + } } void _startBatteryPolling() { diff --git a/lib/screens/scanner_screen.dart b/lib/screens/scanner_screen.dart index 63f4a3c..642ce1f 100644 --- a/lib/screens/scanner_screen.dart +++ b/lib/screens/scanner_screen.dart @@ -8,9 +8,35 @@ import '../widgets/device_tile.dart'; import 'contacts_screen.dart'; /// Screen for scanning and connecting to MeshCore devices -class ScannerScreen extends StatelessWidget { +class ScannerScreen extends StatefulWidget { const ScannerScreen({super.key}); + @override + State createState() => _ScannerScreenState(); +} + +class _ScannerScreenState extends State { + bool changedNavgation = false; + + @override + void initState() { + super.initState(); + final connector = Provider.of(context, listen: false); + + connector.addListener(() { + 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(), + ), + ); + } + }); + } + @override Widget build(BuildContext context) { return Scaffold( @@ -161,15 +187,6 @@ final l10n = context.l10n; ? result.device.platformName : result.advertisementData.advName; await connector.connect(result.device, displayName: name); - - if (context.mounted && connector.isConnected) { - Navigator.push( - context, - MaterialPageRoute( - builder: (context) => const ContactsScreen(), - ), - ); - } } catch (e) { if (context.mounted) { ScaffoldMessenger.of(context).showSnackBar( From 92d2b224e75ad261e4b6e1149c718c32ddac51cf Mon Sep 17 00:00:00 2001 From: Zach Date: Wed, 28 Jan 2026 21:28:28 -0700 Subject: [PATCH 2/3] fix: address PR review issues - Fix memory leak by adding dispose() to remove connection listener - Fix typo: changedNavgation -> _changedNavigation - Add mounted check before navigation to prevent errors - Remove overly aggressive _handleDisconnection() call on battery request failure - Only reset battery flag on error to allow retry without disconnecting --- lib/connector/meshcore_connector.dart | 2 +- lib/screens/scanner_screen.dart | 34 ++++++++++++++++++--------- 2 files changed, 24 insertions(+), 12 deletions(-) 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 From 998ff50495f2bb738588da6d760cd538bc3e2a3a Mon Sep 17 00:00:00 2001 From: Zach Date: Wed, 28 Jan 2026 21:34:13 -0700 Subject: [PATCH 3/3] fix: restore _handleDisconnection() on battery request failure This was the author's original intent - use battery request failure as a signal that the connection is lost. --- lib/connector/meshcore_connector.dart | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/connector/meshcore_connector.dart b/lib/connector/meshcore_connector.dart index 191f74e..6f22c5e 100644 --- a/lib/connector/meshcore_connector.dart +++ b/lib/connector/meshcore_connector.dart @@ -962,9 +962,8 @@ class MeshCoreConnector extends ChangeNotifier { try { await sendFrame(buildGetBattAndStorageFrame()); } catch (e) { - // Reset flag on error to allow retry - // Don't disconnect on battery request failure - it may be transient - _batteryRequested = false; + // Connection likely lost - trigger disconnection handling + _handleDisconnection(); } }