mirror of
https://github.com/jonasbark/swiftcontrol.git
synced 2026-11-29 17:21:45 +01:00
The Rouvy Zwift-BLE advertisement (Zwift fc82 + OBC 128-bit UUID + name "BikeControl" = ~38 bytes) overflows Android's 31-byte primary packet, so startAdvertising fails with "Data too large" — Rouvy users on 6.1 can't connect (the bluetooth_low_energy -> universal_ble migration stopped splitting service UUIDs into the scan response). Bump universal_ble to the fork commit that adds PeripheralAndroidOptions.addServicesInScanResponse, expose it through PeripheralServer.startAdvertising, and enable it for the Rouvy branch of the Zwift emulator. Primary packet becomes flags+name (~16B), scan response carries the UUIDs (~22B); active scanners still see everything. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
116 lines
4.3 KiB
Dart
116 lines
4.3 KiB
Dart
import 'dart:async';
|
|
import 'dart:typed_data';
|
|
|
|
import 'package:universal_ble/universal_ble.dart';
|
|
|
|
/// Thin wrapper around [UniversalBlePeripheral]'s static API that lets
|
|
/// several PeripheralServer instances coexist without clobbering each
|
|
/// other's read/write handlers — universal_ble exposes a single global
|
|
/// read handler and write handler, so we install one router per process
|
|
/// and dispatch by characteristic UUID.
|
|
class PeripheralServer {
|
|
static bool _routerInstalled = false;
|
|
static final Map<String, OnPeripheralReadRequest> _readHandlers = {};
|
|
static final Map<String, OnPeripheralWriteRequest> _writeHandlers = {};
|
|
|
|
static void _installRouter() {
|
|
if (_routerInstalled) return;
|
|
_routerInstalled = true;
|
|
UniversalBlePeripheral.setReadRequestHandlers((deviceId, charId, offset, value) {
|
|
return _readHandlers[charId.toLowerCase()]?.call(deviceId, charId, offset, value);
|
|
});
|
|
UniversalBlePeripheral.setWriteRequestHandlers((deviceId, charId, offset, value) {
|
|
return _writeHandlers[charId.toLowerCase()]?.call(deviceId, charId, offset, value);
|
|
});
|
|
}
|
|
|
|
final List<String> _ownedCharacteristics = [];
|
|
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) {
|
|
_installRouter();
|
|
final key = characteristicId.toLowerCase();
|
|
_readHandlers[key] = handler;
|
|
_ownedCharacteristics.add(key);
|
|
}
|
|
|
|
void setWriteHandler(String characteristicId, OnPeripheralWriteRequest handler) {
|
|
_installRouter();
|
|
final key = characteristicId.toLowerCase();
|
|
_writeHandlers[key] = handler;
|
|
_ownedCharacteristics.add(key);
|
|
}
|
|
|
|
Future<void> addService(BlePeripheralService service) => UniversalBlePeripheral.addService(service);
|
|
|
|
Future<void> clearServices() => UniversalBlePeripheral.clearServices();
|
|
|
|
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();
|
|
for (final key in _ownedCharacteristics) {
|
|
_readHandlers.remove(key);
|
|
_writeHandlers.remove(key);
|
|
}
|
|
_ownedCharacteristics.clear();
|
|
}
|
|
}
|