Files
swiftcontrol/lib/bluetooth/peripheral_server.dart
Jonas Acker f7e4b710ea Don't add a second Device Information and Battery service on iOS
iOS serves its own Device Information and Battery services to every
central, so the controller roles' copies made the phone list each twice,
which Rouvy trips over.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 18:09:32 +01:00

128 lines
5.0 KiB
Dart

import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:prop/emulators/transporter/shared_peripheral.dart';
import 'package:universal_ble/universal_ble.dart';
/// Thin wrapper around [UniversalBlePeripheral]'s static API that lets
/// several PeripheralServer instances — and the trainer bridge — coexist
/// without clobbering each other's read/write handlers: universal_ble exposes
/// a single global read handler and write handler, so every role registers
/// with [SharedPeripheral]'s one router, which dispatches by characteristic
/// UUID.
class PeripheralServer {
final Map<String, OnPeripheralReadRequest> _readHandlers = {};
final Map<String, OnPeripheralWriteRequest> _writeHandlers = {};
StreamSubscription<BlePeripheralConnectionStateChanged>? _connectionSub;
StreamSubscription<BlePeripheralCharacteristicSubscriptionChanged>? _subscriptionSub;
StreamSubscription<BlePeripheralAdvertisingStateChanged>? _advertisingSub;
void onConnectionChanged(void Function(String deviceId, bool connected) callback) {
_connectionSub?.cancel();
_connectionSub = UniversalBlePeripheral.connectionStateStream.listen((e) {
callback(e.deviceId, e.connected);
});
}
void onSubscriptionChanged(void Function(String deviceId, String characteristicId, bool isSubscribed) callback) {
_subscriptionSub?.cancel();
_subscriptionSub = UniversalBlePeripheral.characteristicSubscriptionStream.listen((e) {
callback(e.deviceId, e.characteristicId, e.isSubscribed);
});
}
void onAdvertisingStateChanged(void Function(PeripheralAdvertisingState state, String? error) callback) {
_advertisingSub?.cancel();
_advertisingSub = UniversalBlePeripheral.advertisingStateStream.listen((e) {
callback(e.state, e.error);
});
}
void setReadHandler(String characteristicId, OnPeripheralReadRequest handler) {
_readHandlers[characteristicId.toLowerCase()] = handler;
SharedPeripheral.setReadHandler(characteristicId, handler);
}
void setWriteHandler(String characteristicId, OnPeripheralWriteRequest handler) {
_writeHandlers[characteristicId.toLowerCase()] = handler;
SharedPeripheral.setWriteHandler(characteristicId, handler);
}
/// iOS serves its own Device Information (Apple, iPhone model) and Battery
/// services to every central. Adding ours as well makes the phone list each
/// twice, which trainer apps (Rouvy) trip over — so there the system's copy
/// is the only one.
static bool get systemHostsStandardServices =>
debugSystemHostsStandardServices ?? (!kIsWeb && defaultTargetPlatform == TargetPlatform.iOS);
@visibleForTesting
static bool? debugSystemHostsStandardServices;
static const _standardServices = {
'0000180a-0000-1000-8000-00805f9b34fb', // Device Information
'0000180f-0000-1000-8000-00805f9b34fb', // Battery
};
static bool _hostedBySystem(String serviceId) =>
systemHostsStandardServices && _standardServices.contains(SharedPeripheral.normalize(serviceId));
Future<void> addService(BlePeripheralService service) async {
if (_hostedBySystem(service.uuid)) return;
await UniversalBlePeripheral.addService(service);
}
Future<void> removeService(String serviceId) async {
if (_hostedBySystem(serviceId)) return;
await UniversalBlePeripheral.removeService(serviceId);
}
Future<void> clearServices() => UniversalBlePeripheral.clearServices();
Future<List<String>> getServices() => UniversalBlePeripheral.getServices();
Future<void> startAdvertising({
required List<String> services,
String? localName,
// Put service UUIDs in the Android scan response instead of the primary
// advertisement. Needed when a 128-bit UUID + name would overflow the
// 31-byte primary packet (e.g. the Rouvy Zwift emulator advertisement).
bool servicesInScanResponse = false,
}) => UniversalBlePeripheral.startAdvertising(
services: services,
localName: localName,
platformConfig: PeripheralPlatformConfig(
android: PeripheralAndroidOptions(
addManufacturerDataInScanResponse: true,
addServicesInScanResponse: servicesInScanResponse,
),
),
);
Future<void> stopAdvertising() => UniversalBlePeripheral.stopAdvertising();
Future<void> notify({
required String characteristicId,
required Uint8List value,
String? deviceId,
}) => UniversalBlePeripheral.updateCharacteristicValue(
characteristicId: characteristicId,
value: value,
deviceId: deviceId,
);
Future<PeripheralReadinessState> getAvailabilityState() => UniversalBlePeripheral.getAvailabilityState();
Future<bool> get isReady async => (await getAvailabilityState()) == PeripheralReadinessState.ready;
Future<void> dispose() async {
await _connectionSub?.cancel();
await _subscriptionSub?.cancel();
await _advertisingSub?.cancel();
_readHandlers.forEach(SharedPeripheral.removeReadHandler);
_writeHandlers.forEach(SharedPeripheral.removeWriteHandler);
_readHandlers.clear();
_writeHandlers.clear();
}
}