import 'dart:typed_data'; import 'package:universal_ble/universal_ble.dart'; import 'emulated_ble_platform.dart'; /// Splits universal_ble traffic between the real platform backend and the /// in-memory emulation backend. Devices registered as emulated peripherals /// route to the fake; everything else passes through untouched. Both /// children's events are re-emitted through this instance — the one /// Connection registers its callbacks on. class RoutingBlePlatform extends UniversalBlePlatform { RoutingBlePlatform({required this.real, required this.fake}) { _forwardEvents(real); _forwardEvents(fake); } final UniversalBlePlatform real; final FakeUniversalBlePlatform fake; void _forwardEvents(UniversalBlePlatform child) { child.onScanResultUpdate = updateScanResult; child.onConnectionChange = updateConnection; child.onValueChange = updateCharacteristicValue; child.onAvailabilityChange = updateAvailability; child.onPairingStateChange = updatePairingState; } // A forgotten (unregistered) emulated peripheral no longer has a fake // entry, but the app can still address it briefly afterwards (Sterzo's // 1s-delayed activation write, Connection's disconnect for an unknown // device) — those stray calls must still land on the fake, not leak to the // real OS BLE stack, so fall back on the id prefix once the lookup misses. UniversalBlePlatform _forDevice(String deviceId) => fake.peripherals.containsKey(deviceId) || deviceId.startsWith('emulated:') ? fake : real; @override Future getBluetoothAvailabilityState() => real.getBluetoothAvailabilityState(); @override Future enableBluetooth() => real.enableBluetooth(); @override Future disableBluetooth() => real.disableBluetooth(); @override Future hasPermissions({bool withAndroidFineLocation = false}) => real.hasPermissions(withAndroidFineLocation: withAndroidFineLocation); @override Future requestPermissions({bool withAndroidFineLocation = false}) => real.requestPermissions(withAndroidFineLocation: withAndroidFineLocation); @override Future startScan({ScanFilter? scanFilter, PlatformConfig? platformConfig}) async { await fake.startScan(scanFilter: scanFilter, platformConfig: platformConfig); try { await real.startScan(scanFilter: scanFilter, platformConfig: platformConfig); } catch (_) { // A dead real radio (Bluetooth off on the dev machine) must not break // an emulation session; without emulated peripherals keep the error. if (fake.peripherals.isEmpty) rethrow; } } @override Future stopScan() async { await fake.stopScan(); try { await real.stopScan(); } catch (_) { if (fake.peripherals.isEmpty) rethrow; } } @override Future isScanning() async => (await fake.isScanning()) || (await real.isScanning()); @override Future connect(String deviceId, {Duration? connectionTimeout, bool autoConnect = false}) => _forDevice(deviceId).connect(deviceId, connectionTimeout: connectionTimeout, autoConnect: autoConnect); @override Future disconnect(String deviceId) => _forDevice(deviceId).disconnect(deviceId); @override Future> discoverServices(String deviceId, bool withDescriptors) => _forDevice(deviceId).discoverServices(deviceId, withDescriptors); @override Future setNotifiable( String deviceId, String service, String characteristic, BleInputProperty bleInputProperty, ) => _forDevice(deviceId).setNotifiable(deviceId, service, characteristic, bleInputProperty); @override Future readValue(String deviceId, String service, String characteristic, {Duration? timeout}) => _forDevice(deviceId).readValue(deviceId, service, characteristic, timeout: timeout); @override Future writeValue( String deviceId, String service, String characteristic, Uint8List value, BleOutputProperty bleOutputProperty, ) => _forDevice(deviceId).writeValue(deviceId, service, characteristic, value, bleOutputProperty); @override Future requestMtu(String deviceId, int expectedMtu) => _forDevice(deviceId).requestMtu(deviceId, expectedMtu); @override Future readRssi(String deviceId) => _forDevice(deviceId).readRssi(deviceId); @override Future requestConnectionPriority(String deviceId, BleConnectionPriority priority) => _forDevice(deviceId).requestConnectionPriority(deviceId, priority); @override Future isPaired(String deviceId) => _forDevice(deviceId).isPaired(deviceId); @override Future pair(String deviceId) => _forDevice(deviceId).pair(deviceId); @override Future unpair(String deviceId) => _forDevice(deviceId).unpair(deviceId); @override Future getConnectionState(String deviceId) => _forDevice(deviceId).getConnectionState(deviceId); @override Future> getSystemDevices(List? withServices) => real.getSystemDevices(withServices); }