import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import '../services/bluetooth_service.dart'; import '../services/classic_bluetooth_transport.dart'; class ConnectionScreen extends ConsumerStatefulWidget { const ConnectionScreen({super.key}); @override ConsumerState createState() => _ConnectionScreenState(); } class _ConnectionScreenState extends ConsumerState { late Future> _devices; @override void initState() { super.initState(); _devices = ref.read(bluetoothServiceProvider.notifier).listClassicDevices(); } @override Widget build(BuildContext context) { final service = ref.read(bluetoothServiceProvider.notifier); final state = ref.watch(bluetoothServiceProvider); final busy = state.connectionStatus == 'Connecting...' || state.connectionStatus == 'Connected' || state.firmwareUpdating; return Scaffold( appBar: AppBar(title: const Text('Connect to meCoffee')), body: ListView( padding: const EdgeInsets.all(16), children: [ ListTile( leading: const Icon(Icons.bluetooth_searching), title: const Text('Scan for Bluetooth LE'), subtitle: const Text('HM-10 / FFE0 controllers'), enabled: !busy, onTap: () { service.startScan(); Navigator.of(context).pop(); }, ), const Divider(), const Text( 'Bluetooth Classic / SPP (Android)\n' 'Pair your controller in Android Bluetooth settings first, then ' 'select it below. Select only a meCoffee controller.', ), TextButton.icon( icon: const Icon(Icons.refresh), label: const Text('Refresh paired devices'), onPressed: busy ? null : () { setState(() { _devices = service.listClassicDevices(); }); }, ), FutureBuilder>( future: _devices, builder: (context, snapshot) { if (snapshot.hasError) { return Text('Cannot list paired devices: ${snapshot.error}'); } if (!snapshot.hasData) { return const Center(child: CircularProgressIndicator()); } if (snapshot.data!.isEmpty) { return const Text('No paired devices found.'); } return Column( children: [ for (final device in snapshot.data!) ListTile( leading: const Icon(Icons.bluetooth), title: Text( device.name.isEmpty ? device.address : device.name, ), subtitle: Text(device.address), enabled: !busy, onTap: () { service.connectToClassicDevice(device); Navigator.of(context).pop(); }, ), ], ); }, ), ], ), ); } }