Compare commits
4 Commits
76b7195e5e
...
eb26c759e8
| Author | SHA1 | Date | |
|---|---|---|---|
| eb26c759e8 | |||
| 5285c44173 | |||
| be1c39d5d7 | |||
| 7628947623 |
@ -8,6 +8,8 @@ const String universalShifterStatusCharacteristicUuid =
|
||||
'0993826f-0ee4-4b37-9614-d13ecba40000';
|
||||
const String universalShifterConnectToAddrCharacteristicUuid =
|
||||
'0993826f-0ee4-4b37-9614-d13ecba40001';
|
||||
const String universalShifterScanResultCharacteristicUuid =
|
||||
'0993826f-0ee4-4b37-9614-d13ecba40004';
|
||||
const String universalShifterCommandCharacteristicUuid =
|
||||
'0993826f-0ee4-4b37-9614-d13ecba40005';
|
||||
const String universalShifterGearRatiosCharacteristicUuid =
|
||||
@ -52,6 +54,13 @@ const int errorPairingAuth = 3;
|
||||
const int errorPairingEncrypt = 4;
|
||||
const int errorFtmsRequiredCharMissing = 5;
|
||||
|
||||
const int trainerScanProtocolVersion = 1;
|
||||
|
||||
const int trainerScanDeviceFlagFtmsDetected = 0x01;
|
||||
const int trainerScanDeviceFlagNameComplete = 0x02;
|
||||
const int trainerScanDeviceFlagScanResponseSeen = 0x04;
|
||||
const int trainerScanDeviceFlagConnectable = 0x08;
|
||||
|
||||
enum DfuUpdateState {
|
||||
idle,
|
||||
starting,
|
||||
@ -250,6 +259,144 @@ enum UniversalShifterCommand {
|
||||
final int value;
|
||||
}
|
||||
|
||||
enum TrainerScanEventKind {
|
||||
scanStarted(0),
|
||||
device(1),
|
||||
scanFinished(2),
|
||||
scanCancelled(3);
|
||||
|
||||
const TrainerScanEventKind(this.value);
|
||||
final int value;
|
||||
|
||||
static TrainerScanEventKind fromRaw(int value) {
|
||||
for (final kind in values) {
|
||||
if (kind.value == value) {
|
||||
return kind;
|
||||
}
|
||||
}
|
||||
throw FormatException('Unknown trainer scan event kind: $value');
|
||||
}
|
||||
}
|
||||
|
||||
class TrainerAddress {
|
||||
const TrainerAddress({
|
||||
required this.flags,
|
||||
required this.bytes,
|
||||
});
|
||||
|
||||
final int flags;
|
||||
final List<int> bytes;
|
||||
|
||||
String get key => '${flags.toRadixString(16).padLeft(2, '0')}:'
|
||||
'${bytes.map((b) => b.toRadixString(16).padLeft(2, '0')).join()}';
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
if (identical(this, other)) {
|
||||
return true;
|
||||
}
|
||||
if (other is! TrainerAddress ||
|
||||
other.flags != flags ||
|
||||
other.bytes.length != bytes.length) {
|
||||
return false;
|
||||
}
|
||||
for (var i = 0; i < bytes.length; i++) {
|
||||
if (other.bytes[i] != bytes[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode => Object.hash(flags, Object.hashAll(bytes));
|
||||
}
|
||||
|
||||
class TrainerScanResult {
|
||||
const TrainerScanResult({
|
||||
required this.sequence,
|
||||
required this.address,
|
||||
required this.rssi,
|
||||
required this.flags,
|
||||
required this.name,
|
||||
});
|
||||
|
||||
final int sequence;
|
||||
final TrainerAddress address;
|
||||
final int rssi;
|
||||
final int flags;
|
||||
final String name;
|
||||
|
||||
bool get ftmsDetected => (flags & trainerScanDeviceFlagFtmsDetected) != 0;
|
||||
bool get nameComplete => (flags & trainerScanDeviceFlagNameComplete) != 0;
|
||||
bool get scanResponseSeen =>
|
||||
(flags & trainerScanDeviceFlagScanResponseSeen) != 0;
|
||||
bool get connectable => (flags & trainerScanDeviceFlagConnectable) != 0;
|
||||
}
|
||||
|
||||
class TrainerScanEvent {
|
||||
const TrainerScanEvent({
|
||||
required this.kind,
|
||||
required this.sequence,
|
||||
this.result,
|
||||
});
|
||||
|
||||
final TrainerScanEventKind kind;
|
||||
final int sequence;
|
||||
final TrainerScanResult? result;
|
||||
|
||||
static TrainerScanEvent fromBytes(List<int> bytes) {
|
||||
if (bytes.length < 3) {
|
||||
throw FormatException(
|
||||
'Trainer scan event payload too short: ${bytes.length}',
|
||||
);
|
||||
}
|
||||
if (bytes[0] != trainerScanProtocolVersion) {
|
||||
throw FormatException(
|
||||
'Unsupported trainer scan protocol version: ${bytes[0]}',
|
||||
);
|
||||
}
|
||||
|
||||
final kind = TrainerScanEventKind.fromRaw(bytes[1]);
|
||||
final sequence = bytes[2];
|
||||
if (kind != TrainerScanEventKind.device) {
|
||||
return TrainerScanEvent(kind: kind, sequence: sequence);
|
||||
}
|
||||
|
||||
if (bytes.length < 13) {
|
||||
throw FormatException(
|
||||
'Trainer scan device payload too short: ${bytes.length}',
|
||||
);
|
||||
}
|
||||
final nameLength = bytes[12];
|
||||
if (bytes.length < 13 + nameLength) {
|
||||
throw FormatException(
|
||||
'Trainer scan device name length $nameLength exceeds payload length '
|
||||
'${bytes.length}',
|
||||
);
|
||||
}
|
||||
|
||||
final rssiRaw = bytes[10];
|
||||
final rssi = rssiRaw > 127 ? rssiRaw - 256 : rssiRaw;
|
||||
final result = TrainerScanResult(
|
||||
sequence: sequence,
|
||||
address: TrainerAddress(
|
||||
flags: bytes[3],
|
||||
bytes: bytes.sublist(4, 10).toList(growable: false),
|
||||
),
|
||||
rssi: rssi,
|
||||
flags: bytes[11],
|
||||
name: utf8.decode(bytes.sublist(13, 13 + nameLength)),
|
||||
);
|
||||
|
||||
return TrainerScanEvent(
|
||||
kind: kind,
|
||||
sequence: sequence,
|
||||
result: result,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class ShifterDeviceTelemetry {
|
||||
const ShifterDeviceTelemetry({
|
||||
this.batteryPercent,
|
||||
@ -510,16 +657,21 @@ class CentralStatus {
|
||||
}
|
||||
}
|
||||
|
||||
List<int> parseMacToLittleEndianBytes(String macAddress) {
|
||||
final compact = macAddress.replaceAll(':', '').replaceAll('-', '');
|
||||
if (compact.length != 12) {
|
||||
throw FormatException('Invalid MAC address format: $macAddress');
|
||||
List<int> encodeTrainerAddress(TrainerAddress address) {
|
||||
if (address.flags < 0 || address.flags > 0xff) {
|
||||
throw FormatException('Invalid trainer address flags: ${address.flags}');
|
||||
}
|
||||
final bytes = <int>[];
|
||||
for (int i = 0; i < compact.length; i += 2) {
|
||||
bytes.add(int.parse(compact.substring(i, i + 2), radix: 16));
|
||||
if (address.bytes.length != 6) {
|
||||
throw FormatException(
|
||||
'Invalid trainer address length: ${address.bytes.length}',
|
||||
);
|
||||
}
|
||||
return bytes.reversed.toList(growable: false);
|
||||
for (final byte in address.bytes) {
|
||||
if (byte < 0 || byte > 0xff) {
|
||||
throw FormatException('Invalid trainer address byte: $byte');
|
||||
}
|
||||
}
|
||||
return [address.flags, ...address.bytes];
|
||||
}
|
||||
|
||||
String formatMacAddressFromLittleEndian(List<int> bytes) {
|
||||
|
||||
@ -10,8 +10,6 @@ import 'package:abawo_bt_app/util/bluetooth_settings.dart';
|
||||
import 'package:abawo_bt_app/widgets/bike_scan_dialog.dart';
|
||||
import 'package:abawo_bt_app/widgets/gear_ratio_editor_card.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_reactive_ble/flutter_reactive_ble.dart'
|
||||
show DiscoveredDevice;
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:nb_utils/nb_utils.dart';
|
||||
@ -438,20 +436,6 @@ class _DeviceDetailsPageState extends ConsumerState<DeviceDetailsPage> {
|
||||
return;
|
||||
}
|
||||
|
||||
_isAssignTrainerDialogOpen = true;
|
||||
final DiscoveredDevice? selectedBike;
|
||||
try {
|
||||
selectedBike = await BikeScanDialog.show(
|
||||
context,
|
||||
excludedDeviceId: widget.deviceAddress,
|
||||
);
|
||||
} finally {
|
||||
_isAssignTrainerDialogOpen = false;
|
||||
}
|
||||
if (selectedBike == null || !mounted) {
|
||||
return;
|
||||
}
|
||||
|
||||
await _startStatusStreamingIfNeeded();
|
||||
final shifter = _shifterService;
|
||||
if (shifter == null) {
|
||||
@ -463,8 +447,26 @@ class _DeviceDetailsPageState extends ConsumerState<DeviceDetailsPage> {
|
||||
);
|
||||
return;
|
||||
}
|
||||
if (!mounted) {
|
||||
return;
|
||||
}
|
||||
|
||||
final result = await shifter.connectButtonToBike(selectedBike.id);
|
||||
_isAssignTrainerDialogOpen = true;
|
||||
final TrainerScanResult? selectedTrainer;
|
||||
try {
|
||||
selectedTrainer = await BikeScanDialog.show(
|
||||
context,
|
||||
shifter: shifter,
|
||||
);
|
||||
} finally {
|
||||
_isAssignTrainerDialogOpen = false;
|
||||
}
|
||||
if (selectedTrainer == null || !mounted) {
|
||||
return;
|
||||
}
|
||||
|
||||
final result =
|
||||
await shifter.connectButtonToTrainer(selectedTrainer.address);
|
||||
if (!mounted) {
|
||||
return;
|
||||
}
|
||||
@ -479,7 +481,13 @@ class _DeviceDetailsPageState extends ConsumerState<DeviceDetailsPage> {
|
||||
}
|
||||
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('Sent connect request for ${selectedBike.id}.')),
|
||||
SnackBar(
|
||||
content: Text(
|
||||
selectedTrainer.name.isEmpty
|
||||
? 'Sent connect request for trainer.'
|
||||
: 'Sent connect request for ${selectedTrainer.name}.',
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@ -46,9 +46,11 @@ class ShifterService {
|
||||
static const int _gearRatioPayloadBytes = _gearRatioSlots + 1;
|
||||
static const int _gearRatioWriteMtu = 64;
|
||||
|
||||
Future<Result<void>> writeConnectToAddress(String bikeDeviceId) async {
|
||||
Future<Result<void>> writeConnectToTrainerAddress(
|
||||
TrainerAddress trainerAddress,
|
||||
) async {
|
||||
try {
|
||||
final payload = parseMacToLittleEndianBytes(bikeDeviceId);
|
||||
final payload = encodeTrainerAddress(trainerAddress);
|
||||
return _requireBluetooth.writeCharacteristic(
|
||||
buttonDeviceId,
|
||||
universalShifterControlServiceUuid,
|
||||
@ -56,12 +58,30 @@ class ShifterService {
|
||||
payload,
|
||||
);
|
||||
} on FormatException catch (e) {
|
||||
return bail('Could not parse bike address "$bikeDeviceId": $e');
|
||||
return bail('Could not encode trainer address: $e');
|
||||
} catch (e) {
|
||||
return bail('Failed writing connect address: $e');
|
||||
return bail('Failed writing trainer address: $e');
|
||||
}
|
||||
}
|
||||
|
||||
Stream<TrainerScanEvent> subscribeToTrainerScanResults() {
|
||||
return _requireBluetooth
|
||||
.subscribeToCharacteristic(
|
||||
buttonDeviceId,
|
||||
universalShifterControlServiceUuid,
|
||||
universalShifterScanResultCharacteristicUuid,
|
||||
)
|
||||
.map(TrainerScanEvent.fromBytes);
|
||||
}
|
||||
|
||||
Future<Result<void>> startTrainerScan() {
|
||||
return writeCommand(UniversalShifterCommand.startScan);
|
||||
}
|
||||
|
||||
Future<Result<void>> stopTrainerScan() {
|
||||
return writeCommand(UniversalShifterCommand.stopScan);
|
||||
}
|
||||
|
||||
Future<Result<void>> writeCommand(UniversalShifterCommand command) {
|
||||
return _requireBluetooth.writeCharacteristic(
|
||||
buttonDeviceId,
|
||||
@ -71,8 +91,10 @@ class ShifterService {
|
||||
);
|
||||
}
|
||||
|
||||
Future<Result<void>> connectButtonToBike(String bikeDeviceId) async {
|
||||
final addrRes = await writeConnectToAddress(bikeDeviceId);
|
||||
Future<Result<void>> connectButtonToTrainer(
|
||||
TrainerAddress trainerAddress,
|
||||
) async {
|
||||
final addrRes = await writeConnectToTrainerAddress(trainerAddress);
|
||||
if (addrRes.isErr()) {
|
||||
return addrRes;
|
||||
}
|
||||
|
||||
@ -1,39 +1,39 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:abawo_bt_app/controller/bluetooth.dart';
|
||||
import 'package:abawo_bt_app/model/shifter_types.dart';
|
||||
import 'package:abawo_bt_app/service/shifter_service.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_reactive_ble/flutter_reactive_ble.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
class BikeScanDialog extends ConsumerStatefulWidget {
|
||||
class BikeScanDialog extends StatefulWidget {
|
||||
const BikeScanDialog({
|
||||
required this.excludedDeviceId,
|
||||
required this.shifter,
|
||||
super.key,
|
||||
});
|
||||
|
||||
final String excludedDeviceId;
|
||||
final ShifterService shifter;
|
||||
|
||||
static Future<DiscoveredDevice?> show(
|
||||
static Future<TrainerScanResult?> show(
|
||||
BuildContext context, {
|
||||
required String excludedDeviceId,
|
||||
required ShifterService shifter,
|
||||
}) {
|
||||
return showDialog<DiscoveredDevice>(
|
||||
return showDialog<TrainerScanResult>(
|
||||
context: context,
|
||||
barrierDismissible: true,
|
||||
builder: (_) => BikeScanDialog(excludedDeviceId: excludedDeviceId),
|
||||
builder: (_) => BikeScanDialog(shifter: shifter),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
ConsumerState<BikeScanDialog> createState() => _BikeScanDialogState();
|
||||
State<BikeScanDialog> createState() => _BikeScanDialogState();
|
||||
}
|
||||
|
||||
class _BikeScanDialogState extends ConsumerState<BikeScanDialog> {
|
||||
bool _showAll = false;
|
||||
class _BikeScanDialogState extends State<BikeScanDialog> {
|
||||
bool _showOnlyFtms = true;
|
||||
bool _isStartingScan = true;
|
||||
bool _isScanning = false;
|
||||
String? _scanError;
|
||||
BluetoothController? _controller;
|
||||
final Map<String, TrainerScanResult> _resultsByAddress = {};
|
||||
StreamSubscription<TrainerScanEvent>? _scanSubscription;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
@ -42,16 +42,39 @@ class _BikeScanDialogState extends ConsumerState<BikeScanDialog> {
|
||||
}
|
||||
|
||||
Future<void> _startScan() async {
|
||||
await _scanSubscription?.cancel();
|
||||
if (_isScanning) {
|
||||
await widget.shifter.stopTrainerScan();
|
||||
}
|
||||
|
||||
setState(() {
|
||||
_isStartingScan = true;
|
||||
_isScanning = false;
|
||||
_scanError = null;
|
||||
_resultsByAddress.clear();
|
||||
});
|
||||
|
||||
try {
|
||||
final controller = await ref.read(bluetoothProvider.future);
|
||||
_controller = controller;
|
||||
await controller.stopScan();
|
||||
await controller.startScan();
|
||||
_scanSubscription = widget.shifter.subscribeToTrainerScanResults().listen(
|
||||
_handleScanEvent,
|
||||
onError: (Object error) {
|
||||
if (!mounted) {
|
||||
return;
|
||||
}
|
||||
setState(() {
|
||||
_scanError = error.toString();
|
||||
_isStartingScan = false;
|
||||
_isScanning = false;
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
final startResult = await widget.shifter.startTrainerScan();
|
||||
if (startResult.isErr()) {
|
||||
_scanError = startResult.unwrapErr().toString();
|
||||
} else {
|
||||
_isScanning = true;
|
||||
}
|
||||
} catch (error) {
|
||||
_scanError = error.toString();
|
||||
} finally {
|
||||
@ -63,15 +86,43 @@ class _BikeScanDialogState extends ConsumerState<BikeScanDialog> {
|
||||
}
|
||||
}
|
||||
|
||||
void _handleScanEvent(TrainerScanEvent event) {
|
||||
if (!mounted) {
|
||||
return;
|
||||
}
|
||||
|
||||
setState(() {
|
||||
_isStartingScan = false;
|
||||
switch (event.kind) {
|
||||
case TrainerScanEventKind.scanStarted:
|
||||
_isScanning = true;
|
||||
_scanError = null;
|
||||
break;
|
||||
case TrainerScanEventKind.device:
|
||||
final result = event.result;
|
||||
if (result != null) {
|
||||
_resultsByAddress[result.address.key] = result;
|
||||
}
|
||||
break;
|
||||
case TrainerScanEventKind.scanFinished:
|
||||
case TrainerScanEventKind.scanCancelled:
|
||||
_isScanning = false;
|
||||
break;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_controller?.stopScan();
|
||||
_scanSubscription?.cancel();
|
||||
if (_isScanning) {
|
||||
unawaited(widget.shifter.stopTrainerScan());
|
||||
}
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final btAsync = ref.watch(bluetoothProvider);
|
||||
final size = MediaQuery.of(context).size;
|
||||
final dialogWidth = size.width < 640 ? size.width - 24 : 560.0;
|
||||
final dialogHeight = size.height < 780 ? size.height * 0.78 : 560.0;
|
||||
@ -83,216 +134,85 @@ class _BikeScanDialogState extends ConsumerState<BikeScanDialog> {
|
||||
child: SizedBox(
|
||||
width: dialogWidth,
|
||||
height: dialogHeight,
|
||||
child: btAsync.when(
|
||||
loading: () => const Center(child: CircularProgressIndicator()),
|
||||
error: (err, _) => Center(child: Text('Bluetooth error: $err')),
|
||||
data: (controller) {
|
||||
_controller ??= controller;
|
||||
return Column(
|
||||
children: [
|
||||
_DialogHeader(
|
||||
showAll: _showAll,
|
||||
isScanning: _isStartingScan,
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
_showAll = value;
|
||||
});
|
||||
},
|
||||
onRescan: _startScan,
|
||||
),
|
||||
Expanded(
|
||||
child: _scanError != null
|
||||
? _ScanMessage(
|
||||
message: 'Could not start trainer scan: $_scanError',
|
||||
action: TextButton.icon(
|
||||
onPressed: _startScan,
|
||||
icon: const Icon(Icons.refresh),
|
||||
label: const Text('Retry'),
|
||||
),
|
||||
)
|
||||
: StreamBuilder<List<DiscoveredDevice>>(
|
||||
stream: controller.scanResultsStream,
|
||||
initialData: controller.scanResults,
|
||||
builder: (context, snapshot) {
|
||||
if (_isStartingScan &&
|
||||
(snapshot.data == null ||
|
||||
snapshot.data!.isEmpty)) {
|
||||
return const Center(
|
||||
child: CircularProgressIndicator());
|
||||
}
|
||||
|
||||
final devices =
|
||||
_filteredDevices(snapshot.data ?? const []);
|
||||
if (devices.isEmpty) {
|
||||
return const _ScanMessage(
|
||||
message:
|
||||
'No matching trainers nearby. Keep scanning or enable Show All to inspect every device in range.',
|
||||
);
|
||||
}
|
||||
|
||||
return ListView.separated(
|
||||
padding: const EdgeInsets.fromLTRB(20, 0, 20, 20),
|
||||
itemCount: devices.length,
|
||||
separatorBuilder: (_, __) =>
|
||||
const SizedBox(height: 12),
|
||||
itemBuilder: (context, index) {
|
||||
final device = devices[index];
|
||||
final isFtms = _advertisesFtms(device);
|
||||
return Material(
|
||||
color: Theme.of(context).colorScheme.surface,
|
||||
borderRadius: BorderRadius.circular(22),
|
||||
child: InkWell(
|
||||
borderRadius: BorderRadius.circular(22),
|
||||
onTap: () =>
|
||||
Navigator.of(context).pop(device),
|
||||
child: Container(
|
||||
padding: const EdgeInsets.all(16),
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(22),
|
||||
border: Border.all(
|
||||
color: Theme.of(context)
|
||||
.colorScheme
|
||||
.outlineVariant
|
||||
.withValues(alpha: 0.55),
|
||||
),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
Container(
|
||||
width: 50,
|
||||
height: 50,
|
||||
decoration: BoxDecoration(
|
||||
shape: BoxShape.circle,
|
||||
color: Theme.of(context)
|
||||
.colorScheme
|
||||
.primary
|
||||
.withValues(alpha: 0.12),
|
||||
),
|
||||
child: Icon(
|
||||
Icons.pedal_bike_rounded,
|
||||
color: Theme.of(context)
|
||||
.colorScheme
|
||||
.primary,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 14),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment:
|
||||
CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
device.name.isEmpty
|
||||
? 'Unknown Device'
|
||||
: device.name,
|
||||
maxLines: 1,
|
||||
overflow:
|
||||
TextOverflow.ellipsis,
|
||||
style: Theme.of(context)
|
||||
.textTheme
|
||||
.titleMedium
|
||||
?.copyWith(
|
||||
fontWeight:
|
||||
FontWeight.w700,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
isFtms
|
||||
? 'FTMS'
|
||||
: 'Nearby trainer',
|
||||
style: Theme.of(context)
|
||||
.textTheme
|
||||
.bodyMedium
|
||||
?.copyWith(
|
||||
color: Theme.of(context)
|
||||
.colorScheme
|
||||
.primary,
|
||||
fontWeight:
|
||||
FontWeight.w600,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
device.id,
|
||||
maxLines: 1,
|
||||
overflow:
|
||||
TextOverflow.ellipsis,
|
||||
style: Theme.of(context)
|
||||
.textTheme
|
||||
.bodySmall
|
||||
?.copyWith(
|
||||
color: Theme.of(context)
|
||||
.colorScheme
|
||||
.onSurface
|
||||
.withValues(
|
||||
alpha: 0.62),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Column(
|
||||
crossAxisAlignment:
|
||||
CrossAxisAlignment.end,
|
||||
children: [
|
||||
_RssiBadge(rssi: device.rssi),
|
||||
const SizedBox(height: 12),
|
||||
Icon(
|
||||
Icons.chevron_right_rounded,
|
||||
color: Theme.of(context)
|
||||
.colorScheme
|
||||
.onSurface
|
||||
.withValues(alpha: 0.55),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
child: Column(
|
||||
children: [
|
||||
_DialogHeader(
|
||||
showOnlyFtms: _showOnlyFtms,
|
||||
isScanning: _isStartingScan || _isScanning,
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
_showOnlyFtms = value;
|
||||
});
|
||||
},
|
||||
onRescan: _startScan,
|
||||
),
|
||||
Expanded(child: _buildBody(context)),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
List<DiscoveredDevice> _filteredDevices(List<DiscoveredDevice> devices) {
|
||||
return devices.where((device) {
|
||||
if (device.id == widget.excludedDeviceId) {
|
||||
return false;
|
||||
}
|
||||
if (_showAll) {
|
||||
return true;
|
||||
}
|
||||
return _advertisesFtms(device);
|
||||
}).toList(growable: false);
|
||||
Widget _buildBody(BuildContext context) {
|
||||
if (_scanError != null) {
|
||||
return _ScanMessage(
|
||||
message: 'Could not start shifter trainer scan: $_scanError',
|
||||
action: TextButton.icon(
|
||||
onPressed: _startScan,
|
||||
icon: const Icon(Icons.refresh),
|
||||
label: const Text('Retry'),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
if (_isStartingScan && _resultsByAddress.isEmpty) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
|
||||
final devices = _filteredDevices();
|
||||
if (devices.isEmpty) {
|
||||
return _ScanMessage(
|
||||
message: _isScanning
|
||||
? 'The shifter is scanning. Nearby trainers will appear here as soon as the shifter reports them.'
|
||||
: 'No matching trainers were reported by the shifter. Rescan with the trainer nearby and awake.',
|
||||
);
|
||||
}
|
||||
|
||||
return ListView.separated(
|
||||
padding: const EdgeInsets.fromLTRB(20, 0, 20, 20),
|
||||
itemCount: devices.length,
|
||||
separatorBuilder: (_, __) => const SizedBox(height: 12),
|
||||
itemBuilder: (context, index) => _TrainerScanResultTile(
|
||||
result: devices[index],
|
||||
onTap: () => Navigator.of(context).pop(devices[index]),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
bool _advertisesFtms(DiscoveredDevice device) {
|
||||
return device.serviceUuids.any(isFtmsUuid) ||
|
||||
device.serviceData.keys.any(isFtmsUuid);
|
||||
List<TrainerScanResult> _filteredDevices() {
|
||||
final devices = _resultsByAddress.values.where((device) {
|
||||
return !_showOnlyFtms || device.ftmsDetected;
|
||||
}).toList(growable: false);
|
||||
devices.sort((a, b) {
|
||||
final ftmsCompare = (b.ftmsDetected ? 1 : 0) - (a.ftmsDetected ? 1 : 0);
|
||||
if (ftmsCompare != 0) {
|
||||
return ftmsCompare;
|
||||
}
|
||||
return b.rssi.compareTo(a.rssi);
|
||||
});
|
||||
return devices;
|
||||
}
|
||||
}
|
||||
|
||||
class _DialogHeader extends StatelessWidget {
|
||||
const _DialogHeader({
|
||||
required this.showAll,
|
||||
required this.showOnlyFtms,
|
||||
required this.isScanning,
|
||||
required this.onChanged,
|
||||
required this.onRescan,
|
||||
});
|
||||
|
||||
final bool showAll;
|
||||
final bool showOnlyFtms;
|
||||
final bool isScanning;
|
||||
final ValueChanged<bool> onChanged;
|
||||
final VoidCallback onRescan;
|
||||
@ -319,7 +239,7 @@ class _DialogHeader extends StatelessWidget {
|
||||
),
|
||||
const SizedBox(height: 6),
|
||||
Text(
|
||||
'Tap a nearby trainer to assign it to the connected shifter.',
|
||||
'The shifter scans nearby trainers. Tap one to assign it.',
|
||||
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
|
||||
color: Theme.of(context)
|
||||
.colorScheme
|
||||
@ -344,13 +264,13 @@ class _DialogHeader extends StatelessWidget {
|
||||
child: Row(
|
||||
children: [
|
||||
Text(
|
||||
'Show All',
|
||||
'FTMS only',
|
||||
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Switch(value: showAll, onChanged: onChanged),
|
||||
Switch(value: showOnlyFtms, onChanged: onChanged),
|
||||
],
|
||||
),
|
||||
),
|
||||
@ -379,6 +299,109 @@ class _DialogHeader extends StatelessWidget {
|
||||
}
|
||||
}
|
||||
|
||||
class _TrainerScanResultTile extends StatelessWidget {
|
||||
const _TrainerScanResultTile({
|
||||
required this.result,
|
||||
required this.onTap,
|
||||
});
|
||||
|
||||
final TrainerScanResult result;
|
||||
final VoidCallback onTap;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final colorScheme = Theme.of(context).colorScheme;
|
||||
final name = result.name.isEmpty ? 'Unknown Trainer' : result.name;
|
||||
final typeLabel = result.ftmsDetected ? 'FTMS trainer' : 'Nearby device';
|
||||
final addressText = _formatTrainerAddress(result.address);
|
||||
|
||||
return Material(
|
||||
color: colorScheme.surface,
|
||||
borderRadius: BorderRadius.circular(22),
|
||||
child: InkWell(
|
||||
borderRadius: BorderRadius.circular(22),
|
||||
onTap: onTap,
|
||||
child: Container(
|
||||
padding: const EdgeInsets.all(16),
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(22),
|
||||
border: Border.all(
|
||||
color: colorScheme.outlineVariant.withValues(alpha: 0.55),
|
||||
),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
Container(
|
||||
width: 50,
|
||||
height: 50,
|
||||
decoration: BoxDecoration(
|
||||
shape: BoxShape.circle,
|
||||
color: colorScheme.primary.withValues(alpha: 0.12),
|
||||
),
|
||||
child: Icon(
|
||||
Icons.pedal_bike_rounded,
|
||||
color: colorScheme.primary,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 14),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
name,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: Theme.of(context).textTheme.titleMedium?.copyWith(
|
||||
fontWeight: FontWeight.w700,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
typeLabel,
|
||||
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
|
||||
color: colorScheme.primary,
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
addressText,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: Theme.of(context).textTheme.bodySmall?.copyWith(
|
||||
color:
|
||||
colorScheme.onSurface.withValues(alpha: 0.62),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.end,
|
||||
children: [
|
||||
_RssiBadge(rssi: result.rssi),
|
||||
const SizedBox(height: 12),
|
||||
Icon(
|
||||
Icons.chevron_right_rounded,
|
||||
color: colorScheme.onSurface.withValues(alpha: 0.55),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
String _formatTrainerAddress(TrainerAddress address) {
|
||||
final flags = address.flags.toRadixString(16).padLeft(2, '0');
|
||||
return '${formatMacAddressFromLittleEndian(address.bytes)} · flags 0x$flags';
|
||||
}
|
||||
}
|
||||
|
||||
class _ScanMessage extends StatelessWidget {
|
||||
const _ScanMessage({
|
||||
required this.message,
|
||||
|
||||
@ -108,6 +108,126 @@ void main() {
|
||||
});
|
||||
});
|
||||
|
||||
group('TrainerScanEvent.fromBytes', () {
|
||||
test('parses scan lifecycle events', () {
|
||||
expect(
|
||||
TrainerScanEvent.fromBytes(const [1, 0, 7]).kind,
|
||||
TrainerScanEventKind.scanStarted,
|
||||
);
|
||||
expect(
|
||||
TrainerScanEvent.fromBytes(const [1, 2, 8]).kind,
|
||||
TrainerScanEventKind.scanFinished,
|
||||
);
|
||||
expect(
|
||||
TrainerScanEvent.fromBytes(const [1, 3, 9]).kind,
|
||||
TrainerScanEventKind.scanCancelled,
|
||||
);
|
||||
});
|
||||
|
||||
test('parses device event with signed RSSI and flags', () {
|
||||
final event = TrainerScanEvent.fromBytes([
|
||||
1,
|
||||
1,
|
||||
42,
|
||||
0xc1,
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
4,
|
||||
5,
|
||||
6,
|
||||
0xd6,
|
||||
trainerScanDeviceFlagFtmsDetected |
|
||||
trainerScanDeviceFlagNameComplete |
|
||||
trainerScanDeviceFlagConnectable,
|
||||
5,
|
||||
...'Kickr'.codeUnits,
|
||||
]);
|
||||
|
||||
expect(event.kind, TrainerScanEventKind.device);
|
||||
expect(event.sequence, 42);
|
||||
expect(event.result, isNotNull);
|
||||
expect(event.result!.address.flags, 0xc1);
|
||||
expect(event.result!.address.bytes, [1, 2, 3, 4, 5, 6]);
|
||||
expect(event.result!.rssi, -42);
|
||||
expect(event.result!.name, 'Kickr');
|
||||
expect(event.result!.ftmsDetected, isTrue);
|
||||
expect(event.result!.nameComplete, isTrue);
|
||||
expect(event.result!.scanResponseSeen, isFalse);
|
||||
expect(event.result!.connectable, isTrue);
|
||||
});
|
||||
|
||||
test('rejects invalid scan payloads', () {
|
||||
expect(
|
||||
() => TrainerScanEvent.fromBytes(const []),
|
||||
throwsFormatException,
|
||||
);
|
||||
expect(
|
||||
() => TrainerScanEvent.fromBytes(const [2, 0, 1]),
|
||||
throwsFormatException,
|
||||
);
|
||||
expect(
|
||||
() => TrainerScanEvent.fromBytes(const [1, 9, 1]),
|
||||
throwsFormatException,
|
||||
);
|
||||
expect(
|
||||
() => TrainerScanEvent.fromBytes(const [1, 1, 1]),
|
||||
throwsFormatException,
|
||||
);
|
||||
expect(
|
||||
() => TrainerScanEvent.fromBytes(const [
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
0,
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
4,
|
||||
5,
|
||||
6,
|
||||
0,
|
||||
0,
|
||||
4,
|
||||
65,
|
||||
]),
|
||||
throwsFormatException,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
group('encodeTrainerAddress', () {
|
||||
test('encodes flags and address bytes', () {
|
||||
expect(
|
||||
encodeTrainerAddress(
|
||||
const TrainerAddress(flags: 0xc1, bytes: [1, 2, 3, 4, 5, 6]),
|
||||
),
|
||||
[0xc1, 1, 2, 3, 4, 5, 6],
|
||||
);
|
||||
});
|
||||
|
||||
test('rejects invalid address values', () {
|
||||
expect(
|
||||
() => encodeTrainerAddress(
|
||||
const TrainerAddress(flags: 0, bytes: [1, 2, 3, 4, 5]),
|
||||
),
|
||||
throwsFormatException,
|
||||
);
|
||||
expect(
|
||||
() => encodeTrainerAddress(
|
||||
const TrainerAddress(flags: 256, bytes: [1, 2, 3, 4, 5, 6]),
|
||||
),
|
||||
throwsFormatException,
|
||||
);
|
||||
expect(
|
||||
() => encodeTrainerAddress(
|
||||
const TrainerAddress(flags: 0, bytes: [1, 2, 3, 4, 5, 256]),
|
||||
),
|
||||
throwsFormatException,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
group('standard GATT telemetry parsing', () {
|
||||
test('decodes battery level percentage', () {
|
||||
expect(parseBatteryLevelPercent([0]), 0);
|
||||
|
||||
Reference in New Issue
Block a user