diff options
Diffstat (limited to 'lib/control')
-rw-r--r-- | lib/control/flutter_blue_plus_scanner_controller.dart | 45 | ||||
-rw-r--r-- | lib/control/mock_scanner_controller.dart | 15 | ||||
-rw-r--r-- | lib/control/scanner_controller.dart | 17 | ||||
-rw-r--r-- | lib/control/scanner_controller_impl.dart | 17 | ||||
-rw-r--r-- | lib/control/universal_ble_scanner_controller.dart | 35 |
5 files changed, 96 insertions, 33 deletions
diff --git a/lib/control/flutter_blue_plus_scanner_controller.dart b/lib/control/flutter_blue_plus_scanner_controller.dart index d0a8fee..1c93cf6 100644 --- a/lib/control/flutter_blue_plus_scanner_controller.dart +++ b/lib/control/flutter_blue_plus_scanner_controller.dart @@ -1,3 +1,18 @@ +// Copyright (C) 2025, uvok cheetah +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see <https://www.gnu.org/licenses/>. + import 'dart:async'; import 'package:uvok_epaper_badge/control/scanner_controller_impl.dart'; import 'package:uvok_epaper_badge/model/device/flutter_blue_plus_device.dart'; @@ -9,6 +24,18 @@ import 'package:logger/logger.dart'; var logger = Logger(); class FlutterBluePlusScannerController extends ScannerControllerImpl { + late final StreamSubscription<BluetoothAdapterState> _availSubs; + + FlutterBluePlusScannerController() { + _availSubs = FlutterBluePlus.adapterState.listen( + (d) => super.setAvailability( + d == BluetoothAdapterState.on + ? ScanAvailability.available + : ScanAvailability.unavailable, + ), + ); + } + @override Future<void> startScan({ Duration timeout = const Duration(seconds: 5), @@ -51,23 +78,7 @@ class FlutterBluePlusScannerController extends ScannerControllerImpl { @override void dispose() { stopScan().ignore(); + _availSubs.cancel().ignore(); super.dispose(); } - - final List<ScanResult> _scanResults = []; - bool _deviceInResults(ScanResult incomingDev) => _scanResults.any( - (existingDev) => existingDev.device.remoteId == incomingDev.device.remoteId, - ); - void _onScanResult(List<ScanResult> results) { - if (results.isNotEmpty) { - for (var r in results.where( - (d) => d.rssi > -90 && !_deviceInResults(d), - )) { - logger.i( - '${r.device.remoteId}: "${r.device.platformName}" / "${r.device.advName}" / "${r.advertisementData.advName}" found!', - ); - _scanResults.add(r); - } - } - } } diff --git a/lib/control/mock_scanner_controller.dart b/lib/control/mock_scanner_controller.dart index 8e95e29..61babea 100644 --- a/lib/control/mock_scanner_controller.dart +++ b/lib/control/mock_scanner_controller.dart @@ -1,3 +1,18 @@ +// Copyright (C) 2025, uvok cheetah +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see <https://www.gnu.org/licenses/>. + import 'dart:async'; import 'package:uvok_epaper_badge/control/scanner_controller.dart'; diff --git a/lib/control/scanner_controller.dart b/lib/control/scanner_controller.dart index fd44307..5e330b3 100644 --- a/lib/control/scanner_controller.dart +++ b/lib/control/scanner_controller.dart @@ -1,3 +1,18 @@ +// Copyright (C) 2025, uvok cheetah +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see <https://www.gnu.org/licenses/>. + import 'dart:async'; import 'package:uvok_epaper_badge/model/device/device.dart'; @@ -9,7 +24,7 @@ enum ScanAvailability { unavailable, available } /// Represents a scanner controller. /// It emits devices which may be connected to later. -abstract class ScannerController { +abstract interface class ScannerController { Stream<List<Device>> get scanResultsStream; Stream<ScanStatus> get statusStream; Stream<ScanAvailability> get availabilityStream; diff --git a/lib/control/scanner_controller_impl.dart b/lib/control/scanner_controller_impl.dart index 4c2fa81..e24cabf 100644 --- a/lib/control/scanner_controller_impl.dart +++ b/lib/control/scanner_controller_impl.dart @@ -1,3 +1,18 @@ +// Copyright (C) 2025, uvok cheetah +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see <https://www.gnu.org/licenses/>. + import 'dart:async'; import 'package:uvok_epaper_badge/control/scanner_controller.dart'; import 'package:meta/meta.dart'; @@ -6,7 +21,7 @@ import 'package:uvok_epaper_badge/model/device/device.dart'; import 'package:rxdart/rxdart.dart'; /// Helper class which provides the setStatus method. -abstract class ScannerControllerImpl extends ScannerController { +abstract class ScannerControllerImpl implements ScannerController { final BehaviorSubject<ScanStatus> _scanStatusController = BehaviorSubject<ScanStatus>(); final BehaviorSubject<List<Device>> _deviceContoller = diff --git a/lib/control/universal_ble_scanner_controller.dart b/lib/control/universal_ble_scanner_controller.dart index b91337f..07608fa 100644 --- a/lib/control/universal_ble_scanner_controller.dart +++ b/lib/control/universal_ble_scanner_controller.dart @@ -1,9 +1,25 @@ +// Copyright (C) 2025, uvok cheetah +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see <https://www.gnu.org/licenses/>. + import 'dart:async'; import 'package:logger/logger.dart'; import 'package:universal_ble/universal_ble.dart'; import 'package:uvok_epaper_badge/control/scanner_controller.dart'; import 'package:uvok_epaper_badge/control/scanner_controller_impl.dart'; +import 'package:uvok_epaper_badge/extensions/list_ext.dart'; import 'package:uvok_epaper_badge/model/device/universal_ble_device.dart'; Logger logger = Logger(); @@ -12,11 +28,14 @@ class UniversalBleScannerController extends ScannerControllerImpl { StreamSubscription<BleDevice>? _subScan; StreamSubscription<AvailabilityState>? _subAvail; final List<BleDevice> _devices = []; + final int? rssiLimit; - UniversalBleScannerController() { + UniversalBleScannerController({this.rssiLimit}) { // fuck this limitation, I want an instance method to be called, which doesn't // work in an initializer. - _subScan = UniversalBle.scanStream.listen(_newDeviceAction); + _subScan = UniversalBle.scanStream + .where((d) => rssiLimit == null || (d.rssi ?? 0) > (rssiLimit!)) + .listen(_newDeviceAction); _subAvail = UniversalBle.availabilityStream.listen(_newAvailabilityAction); } @@ -73,15 +92,3 @@ class UniversalBleScannerController extends ScannerControllerImpl { _subAvail?.cancel().ignore(); } } - -extension ListAddExt<T> on List<T> { - /// An element if the predicate is true only for all elements. - /// predicate is passed all existing elements. - bool addIf(T dev, bool Function(T exDev) predicate) { - if (every(predicate)) { - add(dev); - return true; - } - return false; - } -} |