bluetooth connectivity layer added
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
import '../../transport/bluetooth/bluetooth_classic_transport.dart';
|
||||
import '../../transport/bluetooth/bluetooth_device_info.dart';
|
||||
import '../../transport/link_transport.dart';
|
||||
import 'database_provider.dart';
|
||||
|
||||
/// App-lebenslanger Bluetooth-Classic-Transport (Doku Kommunikationsschicht
|
||||
/// Abschnitt 3), verdrahtet gegen die gemerkte Geraete-MAC in den
|
||||
/// Einstellungen. Bewusst nicht autoDispose: die Verbindung soll bestehen
|
||||
/// bleiben, waehrend zwischen Fly- und Plan-Modus gewechselt wird, und erst
|
||||
/// beim Verlassen des Fly-Modus explizit getrennt werden (Doku 3,
|
||||
/// Robustheit: "Verbindung beim Verlassen des Fly-Modus sauber schliessen").
|
||||
final bluetoothTransportProvider = Provider<BluetoothClassicTransport>((ref) {
|
||||
final settingsRepo = ref.watch(appSettingsRepositoryProvider);
|
||||
final transport = BluetoothClassicTransport(
|
||||
getRememberedAddress: settingsRepo.loadConnectionDeviceAddress,
|
||||
rememberAddress: settingsRepo.setConnectionDeviceAddress,
|
||||
);
|
||||
ref.onDispose(transport.dispose);
|
||||
return transport;
|
||||
});
|
||||
|
||||
/// Reaktiver [LinkState] des Bluetooth-Transports fuer die Oberflaeche
|
||||
/// (Verbindungsindikator, Abnahmekriterium: "Zustand jederzeit sichtbar").
|
||||
final linkStateProvider = StreamProvider<LinkState>((ref) {
|
||||
final transport = ref.watch(bluetoothTransportProvider);
|
||||
return transport.state;
|
||||
});
|
||||
|
||||
/// Grund des zuletzt gemeldeten [LinkState.error] (Doku 3, Robustheit: "Grund
|
||||
/// mitfuehren und in der Oberflaeche anzeigen"). Wird bei jedem neuen
|
||||
/// LinkState neu ausgelesen, da der Transport ihn synchron mitfuehrt.
|
||||
final linkErrorReasonProvider = Provider<LinkErrorReason?>((ref) {
|
||||
ref.watch(linkStateProvider);
|
||||
return ref.watch(bluetoothTransportProvider).lastErrorReason;
|
||||
});
|
||||
|
||||
/// Gekoppelte Geraete fuer die manuelle Auswahlliste (Doku 3.2c). Wird bei
|
||||
/// jedem [LinkState]-Wechsel neu geladen, damit eine frisch angeschlossene
|
||||
/// Kopplung (ueber den "Geraet koppeln"-Knopf) ohne expliziten Reload
|
||||
/// auftaucht.
|
||||
final bondedDevicesProvider = FutureProvider<List<BluetoothDeviceInfo>>((ref) {
|
||||
ref.watch(linkStateProvider);
|
||||
return ref.watch(bluetoothTransportProvider).listBondedDevices();
|
||||
});
|
||||
|
||||
/// Ein Eintrag im Verbindungsprotokoll (Doku 7: "einfache Textansicht der
|
||||
/// letzten Ereignisse ... spart im Feld viel Ratearbeit").
|
||||
class ConnectionLogEntry {
|
||||
const ConnectionLogEntry(this.timestamp, this.message);
|
||||
|
||||
final DateTime timestamp;
|
||||
final String message;
|
||||
}
|
||||
|
||||
/// Verbindungsprotokoll + Paketzaehler (Doku 7). Haengt sich an den
|
||||
/// LinkState- und den Rohdaten-Stream des Transports, damit auch ohne
|
||||
/// MSP-Decoder schon sichtbar ist, dass ueberhaupt Bytes ankommen.
|
||||
class ConnectionLogNotifier extends Notifier<List<ConnectionLogEntry>> {
|
||||
static const _maxEntries = 50;
|
||||
|
||||
StreamSubscription<LinkState>? _stateSub;
|
||||
StreamSubscription<void>? _dataSub;
|
||||
int _packetCount = 0;
|
||||
|
||||
int get packetCount => _packetCount;
|
||||
|
||||
@override
|
||||
List<ConnectionLogEntry> build() {
|
||||
final transport = ref.watch(bluetoothTransportProvider);
|
||||
_stateSub?.cancel();
|
||||
_dataSub?.cancel();
|
||||
_packetCount = 0;
|
||||
_stateSub = transport.state.listen(_onStateChanged);
|
||||
_dataSub = transport.incoming.listen((_) => _packetCount++);
|
||||
ref.onDispose(() {
|
||||
_stateSub?.cancel();
|
||||
_dataSub?.cancel();
|
||||
});
|
||||
return const [];
|
||||
}
|
||||
|
||||
void _onStateChanged(LinkState linkState) {
|
||||
final transport = ref.read(bluetoothTransportProvider);
|
||||
final message = switch (linkState) {
|
||||
LinkState.disconnected => 'Getrennt',
|
||||
LinkState.connecting => 'Verbinde ...',
|
||||
LinkState.connected =>
|
||||
'Verbunden${transport.connectedDevice != null ? ' (${transport.connectedDevice!.displayName})' : ''}',
|
||||
LinkState.error =>
|
||||
'Fehler: ${transport.lastErrorMessage ?? transport.lastErrorReason?.name ?? 'unbekannt'}',
|
||||
};
|
||||
_append(message);
|
||||
}
|
||||
|
||||
void _append(String message) {
|
||||
final entry = ConnectionLogEntry(DateTime.now(), message);
|
||||
final next = [...state, entry];
|
||||
state = next.length > _maxEntries
|
||||
? next.sublist(next.length - _maxEntries)
|
||||
: next;
|
||||
}
|
||||
}
|
||||
|
||||
final connectionLogProvider =
|
||||
NotifierProvider<ConnectionLogNotifier, List<ConnectionLogEntry>>(
|
||||
ConnectionLogNotifier.new);
|
||||
@@ -0,0 +1,12 @@
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
import 'database_provider.dart';
|
||||
|
||||
/// Verbindungseinstellungen (Doku Kommunikationsschicht Abschnitt 7):
|
||||
/// gemerkte Bluetooth-Geraete-MAC und "beim Wechsel in Fly automatisch
|
||||
/// verbinden". Eigener Provider statt Wiederverwendung von
|
||||
/// [appSettingsRepositoryProvider] direkt, damit die Einstellungs-UI nach
|
||||
/// einem Schreibzugriff gezielt neu laden kann (`ref.invalidate`).
|
||||
final connectionSettingsProvider = FutureProvider((ref) {
|
||||
return ref.watch(appSettingsRepositoryProvider).load();
|
||||
});
|
||||
@@ -1,15 +1,277 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
/// Drohnen-Profile: FC-Typ, Flugleistungsdaten, Standardprofil T1 Ranger
|
||||
/// (Architektur-Doku 3.5).
|
||||
import '../../../transport/bluetooth/bluetooth_device_info.dart';
|
||||
import '../../../transport/bluetooth/bluetooth_system_settings.dart';
|
||||
import '../../../transport/link_transport.dart';
|
||||
import '../../dmc_colors.dart';
|
||||
import '../../providers/bluetooth_connection_provider.dart';
|
||||
import '../../providers/connection_settings_provider.dart';
|
||||
import '../../providers/database_provider.dart';
|
||||
|
||||
/// Einstellungen: Drohnen-Profile (Doku 3.5/7.3, noch nicht hier
|
||||
/// angebunden - siehe DroneProfileEditor) und Verbindung (Doku
|
||||
/// Kommunikationsschicht Abschnitt 7).
|
||||
class SettingsScreen extends StatelessWidget {
|
||||
const SettingsScreen({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text('Drone Profiles')),
|
||||
body: const Center(child: Text('Settings')),
|
||||
appBar: AppBar(title: const Text('Settings')),
|
||||
body: ListView(
|
||||
padding: const EdgeInsets.all(16),
|
||||
children: const [_ConnectionSection()],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _ConnectionSection extends ConsumerWidget {
|
||||
const _ConnectionSection();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final linkState = ref.watch(linkStateProvider).value;
|
||||
final errorReason = ref.watch(linkErrorReasonProvider);
|
||||
final bondedDevices = ref.watch(bondedDevicesProvider);
|
||||
final connectionSettings = ref.watch(connectionSettingsProvider).value;
|
||||
final log = ref.watch(connectionLogProvider);
|
||||
final packetCount = ref.watch(connectionLogProvider.notifier).packetCount;
|
||||
|
||||
return Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Text('Verbindung', style: Theme.of(context).textTheme.titleMedium),
|
||||
const Spacer(),
|
||||
_StatePill(state: linkState),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
const Text('Art: Bluetooth'),
|
||||
const SizedBox(height: 4),
|
||||
const Text(
|
||||
'WLAN und USB folgen spaeter (Doku Kommunikationsschicht 6/7).',
|
||||
style: TextStyle(fontSize: 12, color: Colors.grey),
|
||||
),
|
||||
if (errorReason != null) ...[
|
||||
const SizedBox(height: 12),
|
||||
_ErrorHint(reason: errorReason),
|
||||
],
|
||||
const SizedBox(height: 16),
|
||||
Row(
|
||||
children: [
|
||||
ElevatedButton(
|
||||
onPressed: () => _connect(ref),
|
||||
child: const Text('Verbinden'),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
OutlinedButton(
|
||||
onPressed: () => ref.read(bluetoothTransportProvider).disconnect(),
|
||||
child: const Text('Trennen'),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
TextButton(
|
||||
onPressed: BluetoothSystemSettings.openBluetoothSettings,
|
||||
child: const Text('Geraet koppeln'),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
SwitchListTile(
|
||||
contentPadding: EdgeInsets.zero,
|
||||
title: const Text('Beim Wechsel in Fly automatisch verbinden'),
|
||||
value: connectionSettings?.connectionAutoConnect ?? true,
|
||||
onChanged: (value) async {
|
||||
await ref
|
||||
.read(appSettingsRepositoryProvider)
|
||||
.setConnectionAutoConnect(value);
|
||||
ref.invalidate(connectionSettingsProvider);
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Text('Gekoppelte Geraete', style: Theme.of(context).textTheme.titleSmall),
|
||||
const SizedBox(height: 4),
|
||||
bondedDevices.when(
|
||||
data: (devices) => devices.isEmpty
|
||||
? const Text(
|
||||
'Keine gekoppelten Geraete. Ueber "Geraet koppeln" '
|
||||
'zunaechst einmalig ueber den Systemdialog koppeln.',
|
||||
style: TextStyle(fontSize: 12, color: Colors.grey),
|
||||
)
|
||||
: RadioGroup<String>(
|
||||
groupValue: connectionSettings?.connectionDeviceAddress,
|
||||
onChanged: (address) =>
|
||||
_selectDevice(ref, address, devices),
|
||||
child: Column(
|
||||
children: [
|
||||
for (final device in devices)
|
||||
RadioListTile<String>(
|
||||
contentPadding: EdgeInsets.zero,
|
||||
dense: true,
|
||||
value: device.address,
|
||||
title: Text(device.displayName),
|
||||
subtitle: Text(
|
||||
device.address,
|
||||
style: const TextStyle(fontSize: 11),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
loading: () => const Padding(
|
||||
padding: EdgeInsets.symmetric(vertical: 8),
|
||||
child: LinearProgressIndicator(),
|
||||
),
|
||||
error: (e, _) => Text(
|
||||
'Gekoppelte Geraete konnten nicht gelesen werden: $e',
|
||||
style: const TextStyle(fontSize: 12, color: Colors.grey),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Text(
|
||||
'Verbindungsprotokoll ($packetCount Datenpakete empfangen)',
|
||||
style: Theme.of(context).textTheme.titleSmall,
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
SizedBox(
|
||||
height: 120,
|
||||
child: log.isEmpty
|
||||
? const Text(
|
||||
'Noch keine Ereignisse.',
|
||||
style: TextStyle(fontSize: 12, color: Colors.grey),
|
||||
)
|
||||
: ListView.builder(
|
||||
reverse: true,
|
||||
itemCount: log.length,
|
||||
itemBuilder: (context, index) {
|
||||
final entry = log[log.length - 1 - index];
|
||||
return Text(
|
||||
'${_formatTime(entry.timestamp)} ${entry.message}',
|
||||
style: const TextStyle(fontSize: 12, fontFamily: 'monospace'),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _connect(WidgetRef ref) async {
|
||||
try {
|
||||
await ref.read(bluetoothTransportProvider).connect();
|
||||
} on LinkConnectException {
|
||||
// Grund/Nachricht sind bereits ueber lastErrorReason/state gemeldet
|
||||
// (siehe LinkConnectException-Doku) - die Oberflaeche reagiert darauf
|
||||
// ueber linkErrorReasonProvider/_ErrorHint, hier ist nichts weiter zu tun.
|
||||
}
|
||||
}
|
||||
|
||||
String _formatTime(DateTime t) =>
|
||||
'${t.hour.toString().padLeft(2, '0')}:${t.minute.toString().padLeft(2, '0')}:${t.second.toString().padLeft(2, '0')}';
|
||||
|
||||
Future<void> _selectDevice(
|
||||
WidgetRef ref,
|
||||
String? address,
|
||||
List<BluetoothDeviceInfo> devices,
|
||||
) async {
|
||||
if (address == null) return;
|
||||
await ref
|
||||
.read(appSettingsRepositoryProvider)
|
||||
.setConnectionDeviceAddress(address);
|
||||
ref.invalidate(connectionSettingsProvider);
|
||||
try {
|
||||
await ref.read(bluetoothTransportProvider).connect();
|
||||
} on LinkConnectException {
|
||||
// siehe _connect() oben
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class _StatePill extends StatelessWidget {
|
||||
const _StatePill({required this.state});
|
||||
|
||||
final LinkState? state;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final (label, color) = switch (state) {
|
||||
LinkState.connected => ('Verbunden', DmcColors.liveGreen),
|
||||
LinkState.connecting => ('Verbinde ...', DmcColors.pafAmber),
|
||||
LinkState.error => ('Fehler', DmcColors.warnRed),
|
||||
LinkState.disconnected || null => ('Getrennt', Colors.grey),
|
||||
};
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 4),
|
||||
decoration: BoxDecoration(
|
||||
color: color.withValues(alpha: 0.15),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
border: Border.all(color: color),
|
||||
),
|
||||
child: Text(label, style: TextStyle(color: color, fontSize: 12)),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _ErrorHint extends StatelessWidget {
|
||||
const _ErrorHint({required this.reason});
|
||||
|
||||
final LinkErrorReason reason;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final (text, action) = switch (reason) {
|
||||
LinkErrorReason.bluetoothOff => (
|
||||
'Bluetooth ist aus. Bitte in den Systemeinstellungen einschalten.',
|
||||
_HintAction('Einstellungen', BluetoothSystemSettings.openBluetoothSettings),
|
||||
),
|
||||
LinkErrorReason.permissionDenied => (
|
||||
'"Geraete in der Naehe"-Berechtigung wird benoetigt. Bitte erneut '
|
||||
'auf "Verbinden" tippen.',
|
||||
null,
|
||||
),
|
||||
LinkErrorReason.permissionDeniedPermanently => (
|
||||
'"Geraete in der Naehe"-Berechtigung wurde dauerhaft abgelehnt. '
|
||||
'Bitte in den App-Einstellungen manuell erteilen.',
|
||||
_HintAction('App-Einstellungen', BluetoothSystemSettings.openAppSettings),
|
||||
),
|
||||
LinkErrorReason.deviceSelectionRequired => (
|
||||
'Kein gekoppeltes mLRS-Geraet gefunden. Bitte unten ein Geraet '
|
||||
'auswaehlen.',
|
||||
null,
|
||||
),
|
||||
LinkErrorReason.connectionFailed => (
|
||||
'Verbindung zum Geraet ist fehlgeschlagen.',
|
||||
null,
|
||||
),
|
||||
LinkErrorReason.unknown => ('Unbekannter Verbindungsfehler.', null),
|
||||
};
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(8),
|
||||
decoration: BoxDecoration(
|
||||
color: DmcColors.warnRed.withValues(alpha: 0.1),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(child: Text(text, style: const TextStyle(fontSize: 12))),
|
||||
if (action != null)
|
||||
TextButton(onPressed: action.onTap, child: Text(action.label)),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _HintAction {
|
||||
const _HintAction(this.label, this.onTap);
|
||||
final String label;
|
||||
final Future<void> Function() onTap;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user