Skip to content

Commit

Permalink
feat: example_advanced disconnect on app pause
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaWellbrock committed Nov 20, 2024
1 parent aa19b69 commit 3acc99f
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 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,45 @@ 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:
_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 3acc99f

Please sign in to comment.