From 3acc99fabf45d400c11b42a21d12312eb1e53023 Mon Sep 17 00:00:00 2001 From: Joshua Wellbrock Date: Wed, 20 Nov 2024 13:49:36 +0100 Subject: [PATCH 1/2] feat: example_advanced disconnect on app pause --- .../lib/advanced_workflow.dart | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/example/example_advanced/lib/advanced_workflow.dart b/example/example_advanced/lib/advanced_workflow.dart index beb2b57..95cc2b7 100644 --- a/example/example_advanced/lib/advanced_workflow.dart +++ b/example/example_advanced/lib/advanced_workflow.dart @@ -12,11 +12,45 @@ class AdvancedWorkflow extends StatefulWidget { } } -class _AdvancedWorkflowState extends State { +// The WidgetsBindingObserver is used to disconnect the reader +// if the app is moved to background +class _AdvancedWorkflowState extends State + with WidgetsBindingObserver { final UrpBleStrategy _bleStrategy = UrpBleStrategy(); late final ImpReader _impReader = ImpReader(connectionStrategy: _bleStrategy); + @override + void initState() { + super.initState(); + WidgetsBinding.instance.addObserver(this); + } + + @override + void dispose() { + WidgetsBinding.instance.removeObserver(this); + super.dispose(); + } + + // Disconnect the device if the app is paused + @override + void didChangeAppLifecycleState(AppLifecycleState state) { + switch (state) { + case AppLifecycleState.inactive: + break; + case AppLifecycleState.detached: + break; + case AppLifecycleState.paused: + _bleStrategy.disconnectDevice(); + break; + case AppLifecycleState.resumed: + break; + case AppLifecycleState.hidden: + break; + } + super.didChangeAppLifecycleState(state); + } + // Find and connect a IMP Reader Future findAndConnect() async { await _bleStrategy.findAndConnectDevice( From 04424a172a9020a047d885b63a6732e141015f24 Mon Sep 17 00:00:00 2001 From: Joshua Wellbrock Date: Wed, 20 Nov 2024 13:56:20 +0100 Subject: [PATCH 2/2] fix: disconnect only if device is connected --- example/example_advanced/lib/advanced_workflow.dart | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/example/example_advanced/lib/advanced_workflow.dart b/example/example_advanced/lib/advanced_workflow.dart index 95cc2b7..e99caf9 100644 --- a/example/example_advanced/lib/advanced_workflow.dart +++ b/example/example_advanced/lib/advanced_workflow.dart @@ -41,7 +41,9 @@ class _AdvancedWorkflowState extends State case AppLifecycleState.detached: break; case AppLifecycleState.paused: - _bleStrategy.disconnectDevice(); + if(_bleStrategy.status == ConnectionStatus.connected) { + _bleStrategy.disconnectDevice(); + } break; case AppLifecycleState.resumed: break;