From be1c39d5d72f895812399be14ef44334a1798b00 Mon Sep 17 00:00:00 2001 From: Yandrik Date: Tue, 28 Apr 2026 21:29:26 +0200 Subject: [PATCH] feat: add shifter trainer scan service --- lib/service/shifter_service.dart | 46 ++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/lib/service/shifter_service.dart b/lib/service/shifter_service.dart index e1f8dc8..2eb8d7d 100644 --- a/lib/service/shifter_service.dart +++ b/lib/service/shifter_service.dart @@ -62,6 +62,42 @@ class ShifterService { } } + Future> writeConnectToTrainerAddress( + TrainerAddress trainerAddress, + ) async { + try { + final payload = encodeTrainerAddress(trainerAddress); + return _requireBluetooth.writeCharacteristic( + buttonDeviceId, + universalShifterControlServiceUuid, + universalShifterConnectToAddrCharacteristicUuid, + payload, + ); + } on FormatException catch (e) { + return bail('Could not encode trainer address: $e'); + } catch (e) { + return bail('Failed writing trainer address: $e'); + } + } + + Stream subscribeToTrainerScanResults() { + return _requireBluetooth + .subscribeToCharacteristic( + buttonDeviceId, + universalShifterControlServiceUuid, + universalShifterScanResultCharacteristicUuid, + ) + .map(TrainerScanEvent.fromBytes); + } + + Future> startTrainerScan() { + return writeCommand(UniversalShifterCommand.startScan); + } + + Future> stopTrainerScan() { + return writeCommand(UniversalShifterCommand.stopScan); + } + Future> writeCommand(UniversalShifterCommand command) { return _requireBluetooth.writeCharacteristic( buttonDeviceId, @@ -79,6 +115,16 @@ class ShifterService { return writeCommand(UniversalShifterCommand.connectToDevice); } + Future> connectButtonToTrainer( + TrainerAddress trainerAddress, + ) async { + final addrRes = await writeConnectToTrainerAddress(trainerAddress); + if (addrRes.isErr()) { + return addrRes; + } + return writeCommand(UniversalShifterCommand.connectToDevice); + } + Future> readGearRatios() async { final readRes = await _requireBluetooth.readCharacteristic( buttonDeviceId,