import 'dart:async'; import 'package:flutter_blue_plus/flutter_blue_plus.dart'; import 'package:flutter_blue_plus_platform_interface/flutter_blue_plus_platform_interface.dart'; final class FakeBluetoothPlatform extends FlutterBluePlusPlatform { final _connections = StreamController.broadcast(); final _services = StreamController.broadcast(); final _received = StreamController.broadcast(); final _written = StreamController.broadcast(); final _mtus = StreamController.broadcast(); final _descriptors = StreamController.broadcast(); final _scans = StreamController.broadcast(); final writes = >[]; final connected = {}; final serviceUuid = Guid('0000ffe0-0000-1000-8000-00805f9b34fb'); final characteristicUuid = Guid('0000ffe1-0000-1000-8000-00805f9b34fb'); bool failDiscovery = false; bool failNotifications = false; bool failWrites = false; int? failWriteAt; bool hasCharacteristic = true; bool writable = true; bool writableWithoutResponse = false; bool notifiable = true; bool respondToIdentity = true; Completer? discoveryGate; Completer? writeGate; int mtu = 23; int _nextId = 0; int scanStarts = 0; int scanStops = 0; int connectionAttempts = 0; BluetoothDevice newDevice() => BluetoothDevice.fromId('review-${_nextId++}'); void reset() { writes.clear(); failDiscovery = false; failNotifications = false; failWrites = false; failWriteAt = null; hasCharacteristic = true; writable = true; writableWithoutResponse = false; notifiable = true; respondToIdentity = true; discoveryGate = null; writeGate = null; mtu = 23; scanStarts = 0; scanStops = 0; connectionAttempts = 0; } @override Stream get onConnectionStateChanged => _connections.stream; @override Stream get onDiscoveredServices => _services.stream; @override Stream get onCharacteristicReceived => _received.stream; @override Stream get onCharacteristicWritten => _written.stream; @override Stream get onMtuChanged => _mtus.stream; @override Stream get onDescriptorWritten => _descriptors.stream; @override Stream get onScanResponse => _scans.stream; @override Future startScan(BmScanSettings request) async { scanStarts++; return true; } @override Future stopScan(BmStopScanRequest request) async { scanStops++; return true; } @override Future getAdapterState( BmBluetoothAdapterStateRequest request, ) async => BmBluetoothAdapterState(adapterState: BmAdapterStateEnum.on); void _connection(DeviceIdentifier id, BmConnectionStateEnum state) { _connections.add( BmConnectionStateResponse( remoteId: id, connectionState: state, disconnectReasonCode: null, disconnectReasonString: null, ), ); } @override Future connect(BmConnectRequest request) async { connectionAttempts++; connected.add(request.remoteId); _connection(request.remoteId, BmConnectionStateEnum.connected); return true; } @override Future disconnect(BmDisconnectRequest request) async { final changed = connected.remove(request.remoteId); if (changed) { _connection(request.remoteId, BmConnectionStateEnum.disconnected); } return changed; } @override Future requestMtu(BmMtuChangeRequest request) async { _mtus.add( BmMtuChangedResponse( remoteId: request.remoteId, mtu: mtu, success: true, errorCode: 0, errorString: '', ), ); return true; } @override Future discoverServices(BmDiscoverServicesRequest request) async { if (failDiscovery) throw StateError('Discovery failed'); final result = BmDiscoverServicesResult( remoteId: request.remoteId, services: [ BmBluetoothService( remoteId: request.remoteId, primaryServiceUuid: null, serviceUuid: serviceUuid, characteristics: [ if (hasCharacteristic) BmBluetoothCharacteristic( remoteId: request.remoteId, primaryServiceUuid: null, serviceUuid: serviceUuid, characteristicUuid: characteristicUuid, instanceId: 0, descriptors: [], properties: BmCharacteristicProperties( broadcast: false, read: false, writeWithoutResponse: writableWithoutResponse, write: writable, notify: notifiable, indicate: false, authenticatedSignedWrites: false, extendedProperties: false, notifyEncryptionRequired: false, indicateEncryptionRequired: false, ), ), ], ), ], success: true, errorCode: 0, errorString: '', ); final gate = discoveryGate; if (gate == null) { _services.add(result); } else { unawaited(gate.future.then((_) => _services.add(result))); } return true; } @override Future setNotifyValue(BmSetNotifyValueRequest request) async { if (failNotifications) throw StateError('Notification setup failed'); return false; } @override Future writeCharacteristic(BmWriteCharacteristicRequest request) async { await writeGate?.future; if (failWrites || writes.length == failWriteAt) { throw StateError('Write failed'); } if (request.value.length > mtu - 3) { throw StateError('Write exceeds MTU payload'); } writes.add(List.from(request.value)); if (respondToIdentity && String.fromCharCodes(request.value).contains('cmd uname')) { notify( BluetoothDevice.fromId(request.remoteId.str), 'cmd uname meCoffee V10 OK\n', ); } _written.add( BmCharacteristicData( remoteId: request.remoteId, primaryServiceUuid: request.primaryServiceUuid, serviceUuid: request.serviceUuid, characteristicUuid: request.characteristicUuid, instanceId: request.instanceId, value: request.value, success: true, errorCode: 0, errorString: '', ), ); return true; } void notify(BluetoothDevice device, String message) { _received.add( BmCharacteristicData( remoteId: device.remoteId, primaryServiceUuid: null, serviceUuid: serviceUuid, characteristicUuid: characteristicUuid, instanceId: 0, value: message.codeUnits, success: true, errorCode: 0, errorString: '', ), ); } }