import 'dart:convert'; import 'dart:typed_data'; import 'package:universal_ble/universal_ble.dart'; /// Mock implementation of [UniversalBlePlatform] for testing class MockUniversalBle extends UniversalBlePlatform { final _mockBleDevice = BleDevice( name: 'MockDevice', deviceId: 'MockDeviceId', rssi: 50, manufacturerDataList: [], ); Uint8List _serviceValue = utf8.encode('Result'); bool _isScanning = false; final BleService _mockService = BleService('180', [ BleCharacteristic('180A', [ CharacteristicProperty.read, CharacteristicProperty.write, CharacteristicProperty.notify, ], []), ]); @override Future startScan({ ScanFilter? scanFilter, PlatformConfig? platformConfig, }) async { _isScanning = true; updateScanResult(_mockBleDevice); } @override Future stopScan() async { _isScanning = false; } @override Future isScanning() async { return _isScanning; } @override Future connect(String deviceId, {Duration? connectionTimeout, bool autoConnect = false}) async { updateConnection(deviceId, true); } @override Future disconnect(String deviceId) async { updateConnection(deviceId, false); } @override Future> discoverServices(String deviceId, bool withDescriptors) async { return [_mockService]; } @override Future enableBluetooth() async { await Future.delayed(const Duration(milliseconds: 500)); return true; } @override Future getBluetoothAvailabilityState() async { return AvailabilityState.poweredOn; } @override Future> getSystemDevices(List? withServices) async { return []; } @override Future readValue( String deviceId, String service, String characteristic, { final Duration? timeout, }) async { await Future.delayed(const Duration(milliseconds: 500)); return _serviceValue; } @override Future writeValue(String deviceId, String service, String characteristic, Uint8List value, BleOutputProperty bleOutputProperty) async { await Future.delayed(const Duration(milliseconds: 500)); _serviceValue = value; } @override Future requestMtu(String deviceId, int expectedMtu) async { await Future.delayed(const Duration(seconds: 1)); return 512; } @override Future requestConnectionPriority( String deviceId, BleConnectionPriority priority, ) async {} @override Future setNotifiable(String deviceId, String service, String characteristic, BleInputProperty bleInputProperty) async {} @override Future isPaired(String deviceId) async { await Future.delayed(const Duration(milliseconds: 500)); return true; } @override Future pair(String deviceId) async { updatePairingState(deviceId, true); return true; } @override Future unpair(String deviceId) async { updatePairingState(deviceId, false); } @override Future getConnectionState(String deviceId) { throw UnimplementedError(); } @override Future disableBluetooth() { throw UnimplementedError(); } @override Future requestPermissions({ bool withAndroidFineLocation = false, bool withAndroidBluetoothAdvertise = false, }) { throw UnimplementedError(); } @override Future readRssi(String deviceId) { throw UnimplementedError(); } }