| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- 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<BmConnectionStateResponse>.broadcast();
- final _services = StreamController<BmDiscoverServicesResult>.broadcast();
- final _received = StreamController<BmCharacteristicData>.broadcast();
- final _written = StreamController<BmCharacteristicData>.broadcast();
- final _mtus = StreamController<BmMtuChangedResponse>.broadcast();
- final _descriptors = StreamController<BmDescriptorData>.broadcast();
- final _scans = StreamController<BmScanResponse>.broadcast();
- final writes = <List<int>>[];
- final connected = <DeviceIdentifier>{};
- 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<void>? discoveryGate;
- Completer<void>? 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<BmConnectionStateResponse> get onConnectionStateChanged =>
- _connections.stream;
- @override
- Stream<BmDiscoverServicesResult> get onDiscoveredServices => _services.stream;
- @override
- Stream<BmCharacteristicData> get onCharacteristicReceived => _received.stream;
- @override
- Stream<BmCharacteristicData> get onCharacteristicWritten => _written.stream;
- @override
- Stream<BmMtuChangedResponse> get onMtuChanged => _mtus.stream;
- @override
- Stream<BmDescriptorData> get onDescriptorWritten => _descriptors.stream;
- @override
- Stream<BmScanResponse> get onScanResponse => _scans.stream;
- @override
- Future<bool> startScan(BmScanSettings request) async {
- scanStarts++;
- return true;
- }
- @override
- Future<bool> stopScan(BmStopScanRequest request) async {
- scanStops++;
- return true;
- }
- @override
- Future<BmBluetoothAdapterState> 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<bool> connect(BmConnectRequest request) async {
- connectionAttempts++;
- connected.add(request.remoteId);
- _connection(request.remoteId, BmConnectionStateEnum.connected);
- return true;
- }
- @override
- Future<bool> disconnect(BmDisconnectRequest request) async {
- final changed = connected.remove(request.remoteId);
- if (changed) {
- _connection(request.remoteId, BmConnectionStateEnum.disconnected);
- }
- return changed;
- }
- @override
- Future<bool> requestMtu(BmMtuChangeRequest request) async {
- _mtus.add(
- BmMtuChangedResponse(
- remoteId: request.remoteId,
- mtu: mtu,
- success: true,
- errorCode: 0,
- errorString: '',
- ),
- );
- return true;
- }
- @override
- Future<bool> 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<bool> setNotifyValue(BmSetNotifyValueRequest request) async {
- if (failNotifications) throw StateError('Notification setup failed');
- return false;
- }
- @override
- Future<bool> 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<int>.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: '',
- ),
- );
- }
- }
|