feat: add shifter trainer scan service
This commit is contained in:
@ -62,6 +62,42 @@ class ShifterService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<Result<void>> 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<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) {
|
Future<Result<void>> writeCommand(UniversalShifterCommand command) {
|
||||||
return _requireBluetooth.writeCharacteristic(
|
return _requireBluetooth.writeCharacteristic(
|
||||||
buttonDeviceId,
|
buttonDeviceId,
|
||||||
@ -79,6 +115,16 @@ class ShifterService {
|
|||||||
return writeCommand(UniversalShifterCommand.connectToDevice);
|
return writeCommand(UniversalShifterCommand.connectToDevice);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<Result<void>> connectButtonToTrainer(
|
||||||
|
TrainerAddress trainerAddress,
|
||||||
|
) async {
|
||||||
|
final addrRes = await writeConnectToTrainerAddress(trainerAddress);
|
||||||
|
if (addrRes.isErr()) {
|
||||||
|
return addrRes;
|
||||||
|
}
|
||||||
|
return writeCommand(UniversalShifterCommand.connectToDevice);
|
||||||
|
}
|
||||||
|
|
||||||
Future<Result<GearRatiosData>> readGearRatios() async {
|
Future<Result<GearRatiosData>> readGearRatios() async {
|
||||||
final readRes = await _requireBluetooth.readCharacteristic(
|
final readRes = await _requireBluetooth.readCharacteristic(
|
||||||
buttonDeviceId,
|
buttonDeviceId,
|
||||||
|
|||||||
Reference in New Issue
Block a user