Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: example_advanced disconnect on app pause #6

Merged
merged 2 commits into from
Nov 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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