mindwave_mobile2 1.0.0+2 mindwave_mobile2: ^1.0.0+2 copied to clipboard
A Plugin that provides a Flutter interface for Neurosky Mindwave Mobile 2 headset.
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
import 'screens/bluetooth_off_screen.dart';
import 'screens/scan_screen.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
BluetoothAdapterState _adapterState = BluetoothAdapterState.unknown;
late StreamSubscription<BluetoothAdapterState> _adapterStateStateSubscription;
@override
void initState() {
super.initState();
_adapterStateStateSubscription =
FlutterBluePlus.adapterState.listen((state) {
_adapterState = state;
if (mounted) {
setState(() {});
}
});
}
@override
void dispose() {
_adapterStateStateSubscription.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
Widget screen = _adapterState == BluetoothAdapterState.on
? const ScanScreen()
: BluetoothOffScreen(adapterState: _adapterState);
return MaterialApp(
debugShowCheckedModeBanner: false,
color: Colors.lightBlue,
home: screen,
navigatorObservers: [BluetoothAdapterStateObserver()],
);
}
}
class BluetoothAdapterStateObserver extends NavigatorObserver {
StreamSubscription<BluetoothAdapterState>? _adapterStateSubscription;
@override
void didPush(Route route, Route? previousRoute) {
super.didPush(route, previousRoute);
if (route.settings.name == '/DeviceScreen') {
_adapterStateSubscription ??=
FlutterBluePlus.adapterState.listen((state) {
if (state != BluetoothAdapterState.on) {
navigator?.pop();
}
});
}
}
@override
void didPop(Route route, Route? previousRoute) {
super.didPop(route, previousRoute);
// Cancel the subscription when the route is popped
_adapterStateSubscription?.cancel();
_adapterStateSubscription = null;
}
}