connection_screen.dart 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_riverpod/flutter_riverpod.dart';
  3. import '../services/bluetooth_service.dart';
  4. import '../services/classic_bluetooth_transport.dart';
  5. class ConnectionScreen extends ConsumerStatefulWidget {
  6. const ConnectionScreen({super.key});
  7. @override
  8. ConsumerState<ConnectionScreen> createState() => _ConnectionScreenState();
  9. }
  10. class _ConnectionScreenState extends ConsumerState<ConnectionScreen> {
  11. late Future<List<ClassicBluetoothDevice>> _devices;
  12. @override
  13. void initState() {
  14. super.initState();
  15. _devices = ref.read(bluetoothServiceProvider.notifier).listClassicDevices();
  16. }
  17. @override
  18. Widget build(BuildContext context) {
  19. final service = ref.read(bluetoothServiceProvider.notifier);
  20. final state = ref.watch(bluetoothServiceProvider);
  21. final busy =
  22. state.connectionStatus == 'Connecting...' ||
  23. state.connectionStatus == 'Connected' ||
  24. state.firmwareUpdating;
  25. return Scaffold(
  26. appBar: AppBar(title: const Text('Connect to meCoffee')),
  27. body: ListView(
  28. padding: const EdgeInsets.all(16),
  29. children: [
  30. ListTile(
  31. leading: const Icon(Icons.bluetooth_searching),
  32. title: const Text('Scan for Bluetooth LE'),
  33. subtitle: const Text('HM-10 / FFE0 controllers'),
  34. enabled: !busy,
  35. onTap: () {
  36. service.startScan();
  37. Navigator.of(context).pop();
  38. },
  39. ),
  40. const Divider(),
  41. const Text(
  42. 'Bluetooth Classic / SPP (Android)\n'
  43. 'Pair your controller in Android Bluetooth settings first, then '
  44. 'select it below. Select only a meCoffee controller.',
  45. ),
  46. TextButton.icon(
  47. icon: const Icon(Icons.refresh),
  48. label: const Text('Refresh paired devices'),
  49. onPressed: busy
  50. ? null
  51. : () {
  52. setState(() {
  53. _devices = service.listClassicDevices();
  54. });
  55. },
  56. ),
  57. FutureBuilder<List<ClassicBluetoothDevice>>(
  58. future: _devices,
  59. builder: (context, snapshot) {
  60. if (snapshot.hasError) {
  61. return Text('Cannot list paired devices: ${snapshot.error}');
  62. }
  63. if (!snapshot.hasData) {
  64. return const Center(child: CircularProgressIndicator());
  65. }
  66. if (snapshot.data!.isEmpty) {
  67. return const Text('No paired devices found.');
  68. }
  69. return Column(
  70. children: [
  71. for (final device in snapshot.data!)
  72. ListTile(
  73. leading: const Icon(Icons.bluetooth),
  74. title: Text(
  75. device.name.isEmpty ? device.address : device.name,
  76. ),
  77. subtitle: Text(device.address),
  78. enabled: !busy,
  79. onTap: () {
  80. service.connectToClassicDevice(device);
  81. Navigator.of(context).pop();
  82. },
  83. ),
  84. ],
  85. );
  86. },
  87. ),
  88. ],
  89. ),
  90. );
  91. }
  92. }