Skip to content

Commit

Permalink
feat: example_advanced disconnect on app pause (#6)
Browse files Browse the repository at this point in the history
* feat: example_advanced disconnect on app pause
* fix: disconnect only if device is connected

---------

Co-authored-by: Joshua Wellbrock <[email protected]>
  • Loading branch information
JoshuaWell and JoshuaWellbrock authored Nov 20, 2024
1 parent aa19b69 commit 493a4b9
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion example/example_advanced/lib/advanced_workflow.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,47 @@ class AdvancedWorkflow extends StatefulWidget {
}
}

class _AdvancedWorkflowState extends State<AdvancedWorkflow> {
// The WidgetsBindingObserver is used to disconnect the reader
// if the app is moved to background
class _AdvancedWorkflowState extends State<AdvancedWorkflow>
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:
if(_bleStrategy.status == ConnectionStatus.connected) {
_bleStrategy.disconnectDevice();
}
break;
case AppLifecycleState.resumed:
break;
case AppLifecycleState.hidden:
break;
}
super.didChangeAppLifecycleState(state);
}

// Find and connect a IMP Reader
Future<void> findAndConnect() async {
await _bleStrategy.findAndConnectDevice(
Expand Down

0 comments on commit 493a4b9

Please sign in to comment.