diff options
author | Orangerot <purple@orangerot.dev> | 2025-01-06 19:21:36 +0100 |
---|---|---|
committer | Orangerot <purple@orangerot.dev> | 2025-01-06 19:21:36 +0100 |
commit | 36e859d2fe431a27b388ac9d8a13ade921a375a7 (patch) | |
tree | 8f9dcf90906c82ff60e5c8b6834fb44fa6a66bd0 /lib | |
parent | acebb2431c43108167a073e889a61147cc526edd (diff) |
feat: connect to ESense via AlertDialog
Diffstat (limited to 'lib')
-rw-r--r-- | lib/level_selection.dart | 122 |
1 files changed, 121 insertions, 1 deletions
diff --git a/lib/level_selection.dart b/lib/level_selection.dart index e2cdcbe..5ba7a21 100644 --- a/lib/level_selection.dart +++ b/lib/level_selection.dart @@ -1,7 +1,9 @@ import 'dart:io'; +import 'package:esense_flutter/esense.dart'; import 'package:file_picker/file_picker.dart'; import 'package:flutter/material.dart'; +import 'package:permission_handler/permission_handler.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'level.dart'; @@ -17,12 +19,85 @@ class _LevelSelectionState extends State<LevelSelection> { String? stepmaniaCoursesPath; List<FileSystemEntity> stepmaniaCoursesFolders = []; + ESenseManager? eSenseManager; + String _deviceStatus = ''; + String eSenseDeviceName = ''; + bool connected = false; + bool sampling = false; + @override void initState() { super.initState(); loadFolderPath(); } + Future<void> _askForPermissions() async { + if (!(await Permission.bluetoothScan.request().isGranted && + await Permission.bluetoothConnect.request().isGranted)) { + print( + 'WARNING - no permission to use Bluetooth granted. Cannot access eSense device.'); + } + // for some strange reason, Android requires permission to location for Bluetooth to work.....? + if (Platform.isAndroid) { + if (!(await Permission.locationWhenInUse.request().isGranted)) { + print( + 'WARNING - no permission to access location granted. Cannot access eSense device.'); + } + } + } + + Future<void> _listenToESense() async { + await _askForPermissions(); + + // if you want to get the connection events when connecting, + // set up the listener BEFORE connecting... + eSenseManager!.connectionEvents.listen((event) { + print('CONNECTION event: $event'); + + // when we're connected to the eSense device, we can start listening to events from it + // if (event.type == ConnectionType.connected) _listenToESenseEvents(); + + setState(() { + connected = false; + switch (event.type) { + case ConnectionType.connected: + _deviceStatus = 'connected'; + connected = true; + break; + case ConnectionType.unknown: + _deviceStatus = 'unknown'; + break; + case ConnectionType.disconnected: + _deviceStatus = 'disconnected'; + sampling = false; + break; + case ConnectionType.device_found: + _deviceStatus = 'device_found'; + break; + case ConnectionType.device_not_found: + _deviceStatus = 'device_not_found'; + break; + } + }); + }); + } + + Future<void> _connectToESense() async { + if (!connected) { + await _askForPermissions(); + print('Trying to connect to eSense device...'); + print(eSenseDeviceName); + eSenseManager = ESenseManager(eSenseDeviceName); + connected = await eSenseManager!.connect(); + print('success!'); + + setState(() { + _deviceStatus = connected ? 'connecting...' : 'connection failed'; + }); + _listenToESense(); + } + } + Future<void> loadFolderPath() async { SharedPreferences prefs = await SharedPreferences.getInstance(); final String? stepmaniaCoursesPathSetting = @@ -68,7 +143,52 @@ class _LevelSelectionState extends State<LevelSelection> { @override Widget build(BuildContext context) { return Scaffold( - appBar: AppBar(title: const Text('Sense the Rhythm')), + appBar: AppBar( + title: const Text('Sense the Rhythm'), + actions: [ + IconButton( + onPressed: () => showDialog<String>( + context: context, + builder: (BuildContext context) { + return AlertDialog( + title: const Text('Connect to ESense'), + content: StatefulBuilder(builder: + (BuildContext context, StateSetter setState) { + return Column( + mainAxisSize: MainAxisSize.min, + children: [ + TextField( + onChanged: (input) { + setState(() { + eSenseDeviceName = input; + }); + }, + decoration: InputDecoration( + border: OutlineInputBorder(), + hintText: 'eSense-xxxx', + labelText: 'Device name', + ), + ), + Text(eSenseDeviceName), + Text(_deviceStatus) + ]); + }), + actions: <Widget>[ + TextButton( + onPressed: () => Navigator.pop(context, 'Cancel'), + child: const Text('Discard'), + ), + TextButton( + onPressed: () => _connectToESense(), + child: const Text('Connect'), + ), + ], + ); + }, + ), + icon: const Icon(Icons.bluetooth)) + ], + ), body: Builder(builder: (context) { if (stepmaniaCoursesPath == null) { return Text('Add a Directory with Stepmania Songs on \'+\''); |