diff options
Diffstat (limited to 'lib/control/universal_ble_scanner_controller.dart')
-rw-r--r-- | lib/control/universal_ble_scanner_controller.dart | 35 |
1 files changed, 21 insertions, 14 deletions
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; - } -} |