| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- 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<ConnectionScreen> createState() => _ConnectionScreenState();
- }
- class _ConnectionScreenState extends ConsumerState<ConnectionScreen> {
- late Future<List<ClassicBluetoothDevice>> _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<List<ClassicBluetoothDevice>>(
- 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();
- },
- ),
- ],
- );
- },
- ),
- ],
- ),
- );
- }
- }
|