import 'dart:async'; import 'package:bike_control/bluetooth/ble.dart'; import 'package:bike_control/bluetooth/devices/base_device.dart'; import 'package:bike_control/bluetooth/devices/openbikecontrol/openbikecontrol_device.dart'; import 'package:bike_control/bluetooth/devices/proxy/proxy_device.dart'; import 'package:bike_control/bluetooth/devices/shimano/shimano_di2.dart'; import 'package:bike_control/bluetooth/devices/sram/sram_axs.dart'; import 'package:bike_control/bluetooth/devices/wahoo/wahoo_kickr_bike_pro.dart'; import 'package:bike_control/bluetooth/devices/wahoo/wahoo_kickr_bike_shift.dart'; import 'package:bike_control/bluetooth/devices/wahoo/wahoo_kickr_climb.dart'; import 'package:bike_control/bluetooth/devices/wahoo/wahoo_kickr_headwind.dart'; import 'package:bike_control/bluetooth/devices/zwift/constants.dart'; import 'package:bike_control/bluetooth/devices/zwift/zwift_click.dart'; import 'package:bike_control/bluetooth/devices/zwift/zwift_clickv2.dart'; import 'package:bike_control/bluetooth/devices/zwift/zwift_clickv2_left_side.dart'; import 'package:bike_control/bluetooth/devices/zwift/zwift_clickv2_right_side.dart'; import 'package:bike_control/bluetooth/devices/zwift/zwift_device.dart'; import 'package:bike_control/bluetooth/devices/zwift/zwift_play.dart'; import 'package:bike_control/bluetooth/devices/zwift/zwift_play_fw2.dart'; import 'package:bike_control/bluetooth/devices/zwift/zwift_ride.dart'; import 'package:bike_control/gen/l10n.dart'; import 'package:bike_control/utils/core.dart'; import 'package:bike_control/utils/i18n_extension.dart'; import 'package:bike_control/utils/iap/iap_manager.dart'; import 'package:bike_control/utils/keymap/buttons.dart'; import 'package:bike_control/widgets/ui/toast.dart'; import 'package:dartx/dartx.dart'; import 'package:flutter/foundation.dart'; // `SramAdvertisement` clashes with nothing here, but the prop package also // exports a `SramAxs` constants class that collides with this file's `SramAxs` // app device import above — pull in only what's needed to avoid the clash. import 'package:prop/prop.dart' show LogLevel, SramAdvertisement; import 'package:prop/utils/wahoo_climb.dart'; import 'package:shadcn_flutter/shadcn_flutter.dart'; import 'package:universal_ble/universal_ble.dart'; import 'package:url_launcher/url_launcher_string.dart'; import 'cycplus/cycplus_bc2.dart'; import 'elite/elite_rizer.dart'; import 'elite/elite_square.dart'; import 'elite/elite_sterzo.dart'; import 'thinkrider/thinkrider_vs200.dart'; import 'wheeltop/wheeltop_eds.dart'; abstract class BluetoothDevice extends BaseDevice { final BleDevice scanResult; BluetoothDevice( this.scanResult, { required List availableButtons, bool allowMultiple = false, bool isBeta = false, bool supportsLongPress = true, IconData icon = LucideIcons.gamepad, String? buttonPrefix, }) : super( scanResult.name, icon: icon, uniqueId: scanResult.deviceId, availableButtons: allowMultiple ? availableButtons.toList().map((b) => b.copyWith(sourceDeviceId: scanResult.deviceId)).toList() : availableButtons.toList(), isBeta: isBeta, supportsLongPress: supportsLongPress, buttonPrefix: buttonPrefix, ) { rssi = scanResult.rssi; } int? batteryLevel; String? firmwareVersion; String? hardwareRevision; String? manufacturerName; String? deviceName; int? rssi; static List servicesToScan = [ ZwiftConstants.ZWIFT_CUSTOM_SERVICE_UUID, ZwiftConstants.ZWIFT_CUSTOM_SERVICE_SHORT_UUID, ZwiftConstants.ZWIFT_RIDE_CUSTOM_SERVICE_UUID, SquareConstants.SERVICE_UUID, WahooKickrBikeShiftConstants.SERVICE_UUID, WahooKickrHeadwindConstants.SERVICE_UUID, wahooClimbServiceUuid, SterzoConstants.SERVICE_UUID, CycplusBc2Constants.SERVICE_UUID, ShimanoDi2Constants.SERVICE_UUID, ShimanoDi2Constants.SERVICE_UUID_ALTERNATIVE, OpenBikeControlConstants.SERVICE_UUID, ThinkRiderVs200Constants.SERVICE_UUID, ]; static final List _ignoredNames = ['ASSIOMA', 'QUARQ', 'POWERCRANK']; List? services; /// §2.1: BikeControl should only connect to the SRAM AXS rear derailleur — /// bare shifters/blips/pods also advertise service 0xFE51 but aren't /// connectable targets. Uses the advertised device-type record to tell them /// apart; when it's absent or unparsable, falls back to the previous /// behavior (connect). static bool _sramIsConnectable(BleDevice scanResult) { Uint8List? record; for (final e in scanResult.serviceData.entries) { if (e.key.toLowerCase().contains('fe51')) { record = e.value; break; } } if (record == null) return true; // no service data → can't tell; keep current behavior final info = SramAdvertisement.parse(record); if (info == null) return true; return info.isRearDerailleur; // connect only to the rear derailleur } static BluetoothDevice? fromScanResult(BleDevice scanResult) { // skip devices with ignored names if (scanResult.name != null && _ignoredNames.any((ignoredName) => scanResult.name!.toUpperCase().startsWith(ignoredName))) { return null; } // Use the name first as the "System Devices" and Web (android sometimes Windows) don't have manufacturer data BluetoothDevice? device; if (kIsWeb) { device = switch (scanResult.name) { 'Zwift Ride' => ZwiftRide(scanResult), 'Zwift Play' => ZwiftPlay(scanResult, deviceType: ZwiftDeviceType.playLeft), 'Zwift Click' => ZwiftClickV2(scanResult), 'SQUARE' => EliteSquare(scanResult), 'OpenBike' => OpenBikeControlDevice(scanResult), null => null, _ when scanResult.name!.toUpperCase().startsWith('KICKR CLIMB') && IAPManager.instance.isBetaTester => WahooKickrClimb(scanResult), _ when scanResult.name!.toUpperCase().startsWith('HEADWIND') => WahooKickrHeadwind(scanResult), _ when scanResult.name!.toUpperCase().startsWith('STERZO') => EliteSterzo(scanResult), _ when scanResult.name!.toUpperCase().startsWith('RIZER') && IAPManager.instance.isBetaTester => EliteRizer( scanResult, ), _ when scanResult.name!.toUpperCase().startsWith('KICKR BIKE SHIFT') => WahooKickrBikeShift(scanResult), _ when scanResult.name!.toUpperCase().startsWith('KICKR BIKE PRO') => WahooKickrBikePro(scanResult), _ when scanResult.name!.toUpperCase().startsWith('KICKR BIKE') => WahooKickrBikeShift(scanResult), _ when scanResult.name!.toUpperCase().startsWith('CYCPLUS') && scanResult.name!.toUpperCase().contains('BC2') => CycplusBc2(scanResult), _ when scanResult.name!.toUpperCase().startsWith('THINK VS') => ThinkRiderVs200(scanResult), _ when scanResult.name!.toUpperCase().startsWith('RDR') => ShimanoDi2(scanResult), _ when scanResult.name!.toUpperCase().startsWith('SRAM') && _sramIsConnectable(scanResult) => SramAxs( scanResult, ), _ => null, }; } else { device = switch (scanResult.name) { null => null, //'Zwift Ride' => ZwiftRide(scanResult), special case for Zwift Ride: we must only connect to the left controller // https://www.makinolo.com/blog/2024/07/26/zwift-ride-protocol/ //'Zwift Play' => ZwiftPlay(scanResult), //'Zwift Click' => ZwiftClick(scanResult), special case for Zwift Click v2: we must only connect to the left controller _ when scanResult.name!.toUpperCase().startsWith('KICKR CLIMB') && IAPManager.instance.isBetaTester => WahooKickrClimb(scanResult), _ when scanResult.name!.toUpperCase().startsWith('HEADWIND') => WahooKickrHeadwind(scanResult), _ when scanResult.name!.toUpperCase().startsWith('SQUARE') => EliteSquare(scanResult), _ when scanResult.name!.toUpperCase().startsWith('STERZO') => EliteSterzo(scanResult), _ when scanResult.name!.toUpperCase().startsWith('RIZER') && IAPManager.instance.isBetaTester => EliteRizer( scanResult, ), _ when scanResult.name!.toUpperCase().contains('KICKR BIKE SHIFT') => WahooKickrBikeShift(scanResult), _ when scanResult.name!.toUpperCase().startsWith('KICKR BIKE PRO') => WahooKickrBikePro(scanResult), _ when scanResult.name!.toUpperCase().startsWith('KICKR BIKE') => WahooKickrBikeShift(scanResult), _ when scanResult.name!.toUpperCase().startsWith('CYCPLUS') && scanResult.name!.toUpperCase().contains('BC2') => CycplusBc2(scanResult), _ when scanResult.name!.toUpperCase().startsWith('THINK VS') => ThinkRiderVs200(scanResult), //_ when scanResult.services.contains(CycplusBc2Constants.SERVICE_UUID.toLowerCase()) => CycplusBc2(scanResult), _ when scanResult.services.contains(ShimanoDi2Constants.SERVICE_UUID.toLowerCase()) => ShimanoDi2(scanResult), _ when scanResult.services.contains(ShimanoDi2Constants.SERVICE_UUID_ALTERNATIVE.toLowerCase()) => ShimanoDi2( scanResult, ), _ when scanResult.services.containsAny(ProxyDevice.proxyServiceUUIDs) => ProxyDevice(scanResult), _ when scanResult.services.contains(SramAxsConstants.SERVICE_UUID.toLowerCase()) && _sramIsConnectable(scanResult) => SramAxs(scanResult), _ when scanResult.services.contains(OpenBikeControlConstants.SERVICE_UUID.toLowerCase()) => OpenBikeControlDevice(scanResult), _ when scanResult.services.contains(WahooKickrHeadwindConstants.SERVICE_UUID.toLowerCase()) => WahooKickrHeadwind(scanResult), _ when scanResult.services.contains(wahooClimbServiceUuid.toLowerCase()) => WahooKickrClimb(scanResult), // otherwise the service UUIDs will be used _ => null, }; } if (device != null) { return device; } else if (scanResult.services.containsAny([ ZwiftConstants.ZWIFT_CUSTOM_SERVICE_UUID.toLowerCase(), ZwiftConstants.ZWIFT_CUSTOM_SERVICE_SHORT_UUID.toLowerCase(), ZwiftConstants.ZWIFT_RIDE_CUSTOM_SERVICE_UUID.toLowerCase(), ])) { // otherwise use the manufacturer data to identify the device final manufacturerData = scanResult.manufacturerDataList; final data = manufacturerData .firstOrNullWhere((e) => e.companyId == ZwiftConstants.ZWIFT_MANUFACTURER_ID) ?.payload; if (data == null || data.isEmpty) { } else { final type = ZwiftDeviceType.fromManufacturerData(data.first); // The split left/right controllers (with the new unlock handling) are // available to everyone now; a single toggle picks them over the // legacy unified [ZwiftClickV2]. Defaults on. final useNewUnlock = core.settings.getUseNewUnlockMethod(); device = switch (type) { ZwiftDeviceType.click => ZwiftClick(scanResult), ZwiftDeviceType.playRight => ZwiftPlay(scanResult, deviceType: type!), ZwiftDeviceType.playLeft => ZwiftPlay(scanResult, deviceType: type!), ZwiftDeviceType.rideLeft => ZwiftRide(scanResult), ZwiftDeviceType.playFw2 => ZwiftPlayFw2(scanResult), //DeviceType.rideRight => ZwiftRide(scanResult), // see comment above ZwiftDeviceType.clickV2Left => useNewUnlock ? ZwiftClickV2LeftSide(scanResult) : ZwiftClickV2(scanResult), ZwiftDeviceType.clickV2Right => useNewUnlock ? ZwiftClickV2RightSide(scanResult) : null, _ => null, }; } } // WHEELTOP EDS shifters advertise no usable name and no service UUIDs — // they are recognized by their manufacturer-data prefix alone. device ??= WheeltopEds.tryFrom(scanResult); if (scanResult.name == 'Zwift Ride' && device == null && core.connection.controllerDevices.none((d) => d is ZwiftRide)) { // Fallback for Zwift Ride if nothing else matched => old firmware. // Naming the Companion app isn't enough — riders don't know firmware // updates live there, so the toast's action opens it directly. buildToast( level: LogLevel.LOGLEVEL_WARNING, title: AppLocalizations.current.firmwareUpdateRequired(scanResult.name!), closeTitle: AppLocalizations.current.zwiftCompanionApp, onClose: () => launchUrlString(ZwiftConstants.ZWIFT_COMPANION_URL, mode: LaunchMode.externalApplication), duration: Duration(seconds: 6), ); } return device; } @override bool operator ==(Object other) => identical(this, other) || other is BluetoothDevice && runtimeType == other.runtimeType && scanResult.deviceId == other.scanResult.deviceId; @override int get hashCode => scanResult.deviceId.hashCode; BleDevice get device => scanResult; /// Consecutive failed auto-connect attempts before [Connection] stops /// retrying this device and surfaces [connectionGuidance]. 0 = retry forever. int get maxAutoConnectAttempts => 0; /// Actionable hint shown when auto-connect gives up (see /// [maxAutoConnectAttempts]); null when there is no device-specific guidance. String? get connectionGuidance => null; /// When true, [Connection] leaves this device in the reconnect loop even if /// it keeps dropping within seconds of connecting — i.e. it opts out of the /// quick-drop backoff. Default false. WheeltopEds returns true while its /// keepalive experiment runs, so each reconnect can try the next candidate. bool get keepsReconnectingWhileDropping => false; @override Future connect() async { try { await connectUpstream(); } catch (e) { isConnected = false; rethrow; } services = await discoverUpstreamServices(); final deviceInformationService = services!.firstOrNullWhere( (service) => service.uuid == BleUuid.DEVICE_INFORMATION_SERVICE_UUID.toLowerCase(), ); Future readStringChar(BleService? service, String charUuid) async { final characteristic = service?.characteristics.firstOrNullWhere( (c) => c.uuid == charUuid.toLowerCase(), ); if (characteristic == null) return null; try { final data = await readUpstream(service!.uuid, characteristic.uuid); final decoded = String.fromCharCodes(data).trim(); return decoded.isEmpty ? null : decoded; } catch (_) { return null; } } firmwareVersion = await readStringChar( deviceInformationService, BleUuid.DEVICE_INFORMATION_CHARACTERISTIC_FIRMWARE_REVISION, ); hardwareRevision = await readStringChar( deviceInformationService, BleUuid.DEVICE_INFORMATION_CHARACTERISTIC_HARDWARE_REVISION, ); manufacturerName = await readStringChar( deviceInformationService, BleUuid.DEVICE_INFORMATION_CHARACTERISTIC_MANUFACTURER_NAME, ); final genericAccessService = services!.firstOrNullWhere( (service) => service.uuid == BleUuid.GENERIC_ACCESS_SERVICE_UUID.toLowerCase(), ); deviceName = await readStringChar( genericAccessService, BleUuid.GENERIC_ACCESS_CHARACTERISTIC_DEVICE_NAME, ) ?? device.name; if (firmwareVersion != null || hardwareRevision != null || manufacturerName != null || deviceName != null) { core.connection.signalChange(this); } final batteryService = services!.firstOrNullWhere( (service) => service.uuid == BleUuid.DEVICE_BATTERY_SERVICE_UUID.toLowerCase(), ); final batteryCharacteristic = batteryService?.characteristics.firstOrNullWhere( (c) => c.uuid == BleUuid.DEVICE_INFORMATION_CHARACTERISTIC_BATTERY_LEVEL.toLowerCase(), ); if (batteryCharacteristic != null) { final batteryData = await readUpstream(batteryService!.uuid, batteryCharacteristic.uuid); if (batteryData.isNotEmpty) { batteryLevel = batteryData.first; core.connection.signalChange(this); } } await handleServices(services!); } Future handleServices(List services); Future processCharacteristic(String characteristic, Uint8List bytes); // ── Upstream I/O seams ──────────────────────────────────────────────── // ProxyDevice overrides these to route through its TrainerTransport so a // WiFi (DirCon) trainer reuses this whole connect flow. The defaults are // verbatim the previous inline UniversalBle calls. @protected Future connectUpstream() async { await UniversalBle.connect(device.deviceId); if (!kIsWeb) { try { await UniversalBle.requestMtu(device.deviceId, 517); } catch (e) { // not critical, just log it debugPrint('Failed to request MTU: $e'); } } } @protected Future> discoverUpstreamServices() => UniversalBle.discoverServices(device.deviceId); @protected Future readUpstream(String serviceUuid, String characteristicUuid) => UniversalBle.read(device.deviceId, serviceUuid, characteristicUuid); @protected Future disconnectUpstream() => UniversalBle.disconnect(device.deviceId); @override Future disconnect() async { services?.clear(); await disconnectUpstream(); super.disconnect(); } String? serviceUuidForCharacteristic(String characteristicUuid) { return services ?.firstOrNullWhere((service) => service.characteristics.any((c) => c.uuid == characteristicUuid.toLowerCase())) ?.uuid; } @override List showMetaInformation(BuildContext context, {required bool showFull}) { final foregroundColor = Theme.of(context).colorScheme.mutedForeground; const fontSize = 11.0; return [ // metaRow: battery + signal if (batteryLevel != null || rssi != null) ...[ if (batteryLevel != null) ...[ Icon( switch (batteryLevel!) { >= 80 => LucideIcons.batteryFull, >= 60 => LucideIcons.batteryFull, >= 50 => LucideIcons.batteryMedium, >= 25 => LucideIcons.batteryLow, >= 10 => LucideIcons.batteryLow, _ => LucideIcons.batteryWarning, }, size: 14, color: batteryLevel! < 20 ? Theme.of(context).colorScheme.destructive : foregroundColor, ), Text( '$batteryLevel%', style: TextStyle( fontSize: fontSize, color: foregroundColor, ), ), // SizedBox (not Gap) because this lives in a `Wrap`, and Gap looks // up a Flex/Scrollable ancestor for direction — which is absent // when the parent device-header Row flies through a Hero overlay. if (firmwareVersion != null || rssi != null) const SizedBox(width: 16), ], if (firmwareVersion != null && (showFull || (this is ZwiftDevice && (this as ZwiftDevice).hasNewerFirmwareVersion))) ...[ if (this is ZwiftDevice && (this as ZwiftDevice).hasNewerFirmwareVersion) Icon( Icons.warning, size: fontSize, ) else Text('FW', style: TextStyle(fontSize: 10, color: foregroundColor)).inlineCode, Text( firmwareVersion!, style: TextStyle( fontSize: fontSize, color: foregroundColor, ), ), if (this is ZwiftDevice && (this as ZwiftDevice).hasNewerFirmwareVersion) Text( ' (${context.i18n.latestVersion((this as ZwiftDevice).latestFirmwareVersion!)})', style: TextStyle(color: foregroundColor, fontSize: fontSize), ), if (rssi != null) const SizedBox(width: 16), ], if (rssi != null) StreamBuilder( stream: core.connection.rssiConnectionStream.where((device) => device == this).map((event) => event.rssi), builder: (context, rssiValue) { final currentRssi = rssiValue.data ?? rssi!; if (showFull || currentRssi < -70) { return Row( mainAxisSize: MainAxisSize.min, children: [ Icon(LucideIcons.signal, size: 14, color: foregroundColor), const Gap(4), Text( switch (currentRssi) { >= -50 => 'Strong', >= -70 => 'Good', >= -85 => 'Fair', _ => 'Weak', }, style: TextStyle(fontSize: fontSize, color: foregroundColor), ), ], ); } else { return const SizedBox.shrink(); } }, ), ], ]; } void debugSubscribeToAll(List services) { for (final service in services) { for (final characteristic in service.characteristics) { if (characteristic.properties.contains(CharacteristicProperty.indicate)) { debugPrint('Subscribing to indications for ${service.uuid} / ${characteristic.uuid}'); UniversalBle.subscribeIndications(device.deviceId, service.uuid, characteristic.uuid); } if (characteristic.properties.contains(CharacteristicProperty.notify)) { debugPrint('Subscribing to notifications for ${service.uuid} / ${characteristic.uuid}'); UniversalBle.subscribeNotifications(device.deviceId, service.uuid, characteristic.uuid); } } } } } /// Marker for devices that react to trainer/game state rather than acting as a /// controller (e.g. Headwind fan, KICKR Climb). Excluded from [controllerDevices]. mixin Accessory on BluetoothDevice {}