diff options
Diffstat (limited to 'lib/control/flutter_blue_plus_scanner_controller.dart')
-rw-r--r-- | lib/control/flutter_blue_plus_scanner_controller.dart | 45 |
1 files changed, 28 insertions, 17 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); - } - } - } } |