Work that was previously only in the local working tree: - Rename fuelCut -> fuelPump (SW2 bit2, active-low, live-confirmed) - ECT/IAT/BAT moved to live-confirmed offsets 33/34/36 - S300 template-frame detection with hold-last-good logic - Persist ECU type in SharedPreferences; drop per-connect protocol dialog - Official-protocol probe and DTC clear on the debug/DTC screens - Sanity clamp for multi-byte fields; scrollable vertical dashboard - docs/HANDOFF.md Includes temporary [DBG]/[KDBG] prints, removed in the next commit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
155 lines
5.6 KiB
Dart
155 lines
5.6 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_bluetooth_serial/flutter_bluetooth_serial.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
import '../../../core/providers/bt_provider.dart';
|
|
import '../../widgets/main_nav.dart';
|
|
|
|
class BtPickerScreen extends ConsumerStatefulWidget {
|
|
const BtPickerScreen({super.key});
|
|
|
|
@override
|
|
ConsumerState<BtPickerScreen> createState() => _BtPickerScreenState();
|
|
}
|
|
|
|
class _BtPickerScreenState extends ConsumerState<BtPickerScreen> {
|
|
String? _error;
|
|
bool _initDone = false;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
WidgetsBinding.instance.addPostFrameCallback((_) => _initBluetooth());
|
|
}
|
|
|
|
Future<void> _initBluetooth() async {
|
|
setState(() => _error = null);
|
|
|
|
final statuses = await [
|
|
Permission.bluetoothConnect,
|
|
Permission.bluetoothScan,
|
|
Permission.locationWhenInUse,
|
|
].request();
|
|
|
|
final denied = statuses.entries
|
|
.where((e) => e.value.isDenied || e.value.isPermanentlyDenied)
|
|
.map((e) => e.key.toString())
|
|
.toList();
|
|
|
|
if (denied.isNotEmpty) {
|
|
setState(() {
|
|
_error = 'Permissions denied: ${denied.join(", ")}\n\n'
|
|
'Go to Settings → Apps → HV BT Dashboard → Permissions.';
|
|
_initDone = true;
|
|
});
|
|
return;
|
|
}
|
|
|
|
final btState = await FlutterBluetoothSerial.instance.state;
|
|
if (btState != BluetoothState.STATE_ON) {
|
|
await FlutterBluetoothSerial.instance.requestEnable();
|
|
await Future<void>.delayed(const Duration(seconds: 1));
|
|
}
|
|
|
|
setState(() => _initDone = true);
|
|
ref.invalidate(pairedDevicesProvider);
|
|
}
|
|
|
|
// ECU protocol is chosen once via Settings (or defaults to S300) and
|
|
// persisted from there — no need to ask again on every connection. A
|
|
// dialog here used to re-prompt "S300 or KPro?" on every single tap,
|
|
// which was pure friction once the user had already told the app which
|
|
// ECU they have.
|
|
Future<void> _connect(BluetoothDevice device) async {
|
|
await ref.read(btProvider.notifier).connect(device);
|
|
if (!mounted) return;
|
|
if (ref.read(btProvider).isConnected) {
|
|
Navigator.of(context).pushReplacement(
|
|
MaterialPageRoute(builder: (_) => const MainNav()),
|
|
);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final btState = ref.watch(btProvider);
|
|
final devicesAsync = ref.watch(pairedDevicesProvider);
|
|
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('Connect to ECU'),
|
|
actions: [
|
|
IconButton(
|
|
icon: const Icon(Icons.refresh),
|
|
onPressed: _loading ? null : _initBluetooth,
|
|
),
|
|
],
|
|
),
|
|
body: Column(
|
|
children: [
|
|
if (btState.status == BtStatus.error)
|
|
_banner('Connection failed: ${btState.error}'),
|
|
if (_error != null) _banner(_error!),
|
|
Expanded(
|
|
child: !_initDone
|
|
? const Center(child: CircularProgressIndicator())
|
|
: devicesAsync.when(
|
|
loading: () => const Center(
|
|
child: CircularProgressIndicator()),
|
|
error: (e, _) =>
|
|
Center(child: _banner('Failed to load: $e')),
|
|
data: (devices) => devices.isEmpty
|
|
? const Center(
|
|
child: Padding(
|
|
padding: EdgeInsets.all(24),
|
|
child: Text(
|
|
'No paired BT devices.\nPair your ECU in Android Settings first.',
|
|
textAlign: TextAlign.center,
|
|
style: TextStyle(color: Colors.grey),
|
|
),
|
|
),
|
|
)
|
|
: ListView.separated(
|
|
itemCount: devices.length,
|
|
separatorBuilder: (_, __) =>
|
|
const Divider(height: 1),
|
|
itemBuilder: (_, i) {
|
|
final d = devices[i];
|
|
final connecting = btState.isConnecting &&
|
|
btState.deviceName ==
|
|
(d.name ?? d.address);
|
|
return ListTile(
|
|
leading: const Icon(Icons.bluetooth),
|
|
title: Text(d.name ?? 'Unknown'),
|
|
subtitle: Text(d.address),
|
|
trailing: connecting
|
|
? const SizedBox(
|
|
width: 20,
|
|
height: 20,
|
|
child: CircularProgressIndicator(
|
|
strokeWidth: 2))
|
|
: const Icon(
|
|
Icons.arrow_forward_ios,
|
|
size: 14),
|
|
onTap: connecting ? null : () => _connect(d),
|
|
);
|
|
},
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
bool get _loading => !_initDone || ref.read(btProvider).isConnecting;
|
|
|
|
Widget _banner(String msg) => Container(
|
|
width: double.infinity,
|
|
color: const Color(0xFF4A0000),
|
|
padding: const EdgeInsets.all(12),
|
|
child: Text(msg,
|
|
style: const TextStyle(color: Colors.redAccent, fontSize: 12)),
|
|
);
|
|
}
|