diff options
31 files changed, 938 insertions, 93 deletions
diff --git a/lib/badge_exception.dart b/lib/badge_exception.dart new file mode 100644 index 0000000..75f019d --- /dev/null +++ b/lib/badge_exception.dart @@ -0,0 +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/>. + +class BadgeException implements Exception { + String message; + + BadgeException(this.message); + + @override + String toString() { + return message; + } +} 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..7785088 100644 --- a/lib/control/universal_ble_scanner_controller.dart +++ b/lib/control/universal_ble_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:logger/logger.dart'; @@ -12,11 +27,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); } diff --git a/lib/main.dart b/lib/main.dart index 40a7641..90eba85 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -13,8 +13,10 @@ // 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 'package:uvok_epaper_badge/control/flutter_blue_plus_scanner_controller.dart'; import 'package:uvok_epaper_badge/control/mock_scanner_controller.dart'; import 'package:uvok_epaper_badge/control/scanner_controller.dart'; +import 'package:uvok_epaper_badge/control/universal_ble_scanner_controller.dart'; import 'package:uvok_epaper_badge/widgets/badge_app.dart'; import 'package:flutter/material.dart'; @@ -23,6 +25,22 @@ import 'package:logger/logger.dart'; var logger = Logger(); void main() { - final ScannerController scanner = MockScannerController(); + // needed for UniversalBlue, as I initialize the controller early... + WidgetsFlutterBinding.ensureInitialized(); + final ScannerController scanner; + final int arg = 1; + + switch (arg) { + case 1: + scanner = UniversalBleScannerController(rssiLimit: -70); + break; + case 2: + scanner = FlutterBluePlusScannerController(); + break; + default: + scanner = MockScannerController(); + break; + } + runApp(BadgeApp(selectedScanner: scanner)); } diff --git a/lib/model/badge_motive.dart b/lib/model/badge_motive.dart new file mode 100644 index 0000000..0fd2039 --- /dev/null +++ b/lib/model/badge_motive.dart @@ -0,0 +1,24 @@ +// 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/>. + +class BadgeMotive { + final int id; + final String description; + + const BadgeMotive(this.id, this.description); + + @override + String toString() => "$id - $description"; +} diff --git a/lib/model/badge_motive_selection_factory.dart b/lib/model/badge_motive_selection_factory.dart new file mode 100644 index 0000000..45851c2 --- /dev/null +++ b/lib/model/badge_motive_selection_factory.dart @@ -0,0 +1,33 @@ +// 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 'package:uvok_epaper_badge/model/device/device.dart'; +import 'package:uvok_epaper_badge/model/device/flutter_blue_plus_device.dart'; +import 'package:uvok_epaper_badge/model/device/universal_ble_device.dart'; +import 'package:uvok_epaper_badge/model/motive_selection/badge_motive_selection.dart'; +import 'package:uvok_epaper_badge/model/motive_selection/flutter_blue_plus_motive_selection.dart'; +import 'package:uvok_epaper_badge/model/motive_selection/mock_badge_motive_selection.dart'; +import 'package:uvok_epaper_badge/model/motive_selection/universal_blue_motive_selection.dart'; + +class BadgeMotiveSelectionFactory { + static BadgeMotiveSelection createBadgeMotiveSelection(Device device) { + if (device is UniversalBleDevice) { + return UniversalBlueMotiveSelection(device: device); + } else if (device is FlutterBluePlusDevice) { + return FlutterBluePlusMotiveSelection(device: device); + } + return MockBadgeMotiveSelection(); + } +} diff --git a/lib/model/connection/device_connection.dart b/lib/model/connection/device_connection.dart index 5c810f0..f50bd2c 100644 --- a/lib/model/connection/device_connection.dart +++ b/lib/model/connection/device_connection.dart @@ -13,14 +13,12 @@ // 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 'package:flutter/foundation.dart'; + enum ConnectionStatus { disconnected, connected, error } -abstract class DeviceConnection { +abstract interface class DeviceConnection { Future<void> connect(); Future<void> disconnect(); - ConnectionStatus get status; - - // Future<Uint8List> read(String endpoint); - // Future<void> write(String endpoint, Uint8List data); - // Stream<Uint8List> subscribe(String endpoint); + ValueNotifier<ConnectionStatus> get status; } diff --git a/lib/model/connection/flutter_blue_plus_device_connection.dart b/lib/model/connection/flutter_blue_plus_device_connection.dart index 3f860a6..abfc061 100644 --- a/lib/model/connection/flutter_blue_plus_device_connection.dart +++ b/lib/model/connection/flutter_blue_plus_device_connection.dart @@ -15,6 +15,7 @@ import 'dart:async'; +import 'package:flutter/foundation.dart'; import 'package:flutter_blue_plus/flutter_blue_plus.dart'; import 'package:logger/logger.dart'; import 'package:uvok_epaper_badge/first_where_ext.dart'; @@ -24,7 +25,9 @@ import 'package:uvok_epaper_badge/model/device/flutter_blue_plus_device.dart'; var logger = Logger(); class FlutterBluePlusDeviceConnection implements DeviceConnection { - ConnectionStatus _status = ConnectionStatus.disconnected; + final ValueNotifier<ConnectionStatus> _status = ValueNotifier( + ConnectionStatus.disconnected, + ); // Just to have a resonable default subscription? StreamSubscription<BluetoothConnectionState> subs = Stream<BluetoothConnectionState>.empty().listen((e) => ()); @@ -43,36 +46,38 @@ class FlutterBluePlusDeviceConnection implements DeviceConnection { // connect timeout doesn't work under Linux await dev.connect().timeout(Duration(seconds: 2)); // // ???? WTF ???? - List<BluetoothService> svcs = await dev.discoverServices(); - dev.onServicesReset.listen((_) async { - logger.i("Services Reset"); - // try { - // List<BluetoothService> svcs = dev.servicesList; - // findCharac(svcs); - // } catch (e) { - // logger.e(e); - // } - }); - - logger.i("services discovered"); - - findCharac(svcs); - - _status = ConnectionStatus.connected; - - await Future.delayed(Duration(seconds: 5)); - logger.i("Try re-discover"); - svcs = await dev.discoverServices(); + // List<BluetoothService> svcs = await dev.discoverServices(); + // dev.onServicesReset.listen((_) async { + // logger.i("Services Reset"); + // // try { + // // List<BluetoothService> svcs = dev.servicesList; + // // findCharac(svcs); + // // } catch (e) { + // // logger.e(e); + // // } + // }); + + // logger.i("services discovered"); + + // findCharac(svcs); + + // _status.value = ConnectionStatus.connected; + + // await Future.delayed(Duration(seconds: 5)); + // logger.i("Try re-discover"); + // svcs = await dev.discoverServices(); } @override Future<void> disconnect() async { + final dev = device.scanResult.device; subs.cancel().ignore(); - _status = ConnectionStatus.disconnected; + await dev.disconnect(); + _status.value = ConnectionStatus.disconnected; } @override - ConnectionStatus get status => _status; + ValueNotifier<ConnectionStatus> get status => _status; void _onConnStateChange(BluetoothConnectionState event) { logger.i("New conn state: ${event.toString()}"); diff --git a/lib/model/connection/mock_device_connection.dart b/lib/model/connection/mock_device_connection.dart index 8b8750a..9dc3726 100644 --- a/lib/model/connection/mock_device_connection.dart +++ b/lib/model/connection/mock_device_connection.dart @@ -13,21 +13,24 @@ // 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 'package:flutter/foundation.dart'; import 'package:uvok_epaper_badge/model/connection/device_connection.dart'; class MockDeviceConnection implements DeviceConnection { - ConnectionStatus _status = ConnectionStatus.disconnected; + final ValueNotifier<ConnectionStatus> _status = ValueNotifier( + ConnectionStatus.disconnected, + ); @override Future<void> connect() async { - _status = ConnectionStatus.connected; + _status.value = ConnectionStatus.connected; } @override Future<void> disconnect() async { - _status = ConnectionStatus.disconnected; + _status.value = ConnectionStatus.disconnected; } @override - ConnectionStatus get status => _status; + ValueNotifier<ConnectionStatus> get status => _status; } diff --git a/lib/model/connection/universal_ble_connection.dart b/lib/model/connection/universal_ble_connection.dart index 6aa3c87..fa986a0 100644 --- a/lib/model/connection/universal_ble_connection.dart +++ b/lib/model/connection/universal_ble_connection.dart @@ -1,10 +1,28 @@ +// 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 'package:flutter/foundation.dart'; import 'package:universal_ble/universal_ble.dart'; import 'package:uvok_epaper_badge/model/connection/device_connection.dart'; import 'package:uvok_epaper_badge/model/device/universal_ble_device.dart'; class UniversalBleConnection implements DeviceConnection { final UniversalBleDevice _device; - ConnectionStatus _status = ConnectionStatus.disconnected; + final ValueNotifier<ConnectionStatus> _status = ValueNotifier( + ConnectionStatus.disconnected, + ); UniversalBleConnection(this._device); @@ -12,17 +30,22 @@ class UniversalBleConnection implements DeviceConnection { Future<void> connect() async { await _device.device.connect(); - _status = await _device.device.isConnected + _status.value = await _device.device.isConnected ? ConnectionStatus.connected : ConnectionStatus.disconnected; + + if (_status.value == ConnectionStatus.connected) { + /* Ignore return value for now */ + await _device.device.discoverServices(); + } } @override Future<void> disconnect() async { await _device.device.disconnect(); - _status = ConnectionStatus.disconnected; + _status.value = ConnectionStatus.disconnected; } @override - ConnectionStatus get status => _status; + ValueNotifier<ConnectionStatus> get status => _status; } diff --git a/lib/model/device/device.dart b/lib/model/device/device.dart index 98445fa..577ac9e 100644 --- a/lib/model/device/device.dart +++ b/lib/model/device/device.dart @@ -1,5 +1,20 @@ +// 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/>. + /// Represents a (badge) device to be connected to. -abstract class Device { +abstract interface class Device { String? get name; String? get address; int? get rssi; diff --git a/lib/model/device/flutter_blue_plus_device.dart b/lib/model/device/flutter_blue_plus_device.dart index e56223b..3f6102a 100644 --- a/lib/model/device/flutter_blue_plus_device.dart +++ b/lib/model/device/flutter_blue_plus_device.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 'package:uvok_epaper_badge/model/device/device.dart'; import 'package:flutter_blue_plus/flutter_blue_plus.dart'; import 'package:uvok_epaper_badge/utility.dart'; diff --git a/lib/model/device/mock_device.dart b/lib/model/device/mock_device.dart index 78770e1..94a6a9c 100644 --- a/lib/model/device/mock_device.dart +++ b/lib/model/device/mock_device.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 'package:uvok_epaper_badge/model/device/device.dart'; class MockDevice implements Device { diff --git a/lib/model/device/universal_ble_device.dart b/lib/model/device/universal_ble_device.dart index cbd6f93..0b6953f 100644 --- a/lib/model/device/universal_ble_device.dart +++ b/lib/model/device/universal_ble_device.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 'package:universal_ble/universal_ble.dart'; import 'package:uvok_epaper_badge/model/device/device.dart'; diff --git a/lib/model/motive_selection/badge_motive_selection.dart b/lib/model/motive_selection/badge_motive_selection.dart new file mode 100644 index 0000000..b6f2b9a --- /dev/null +++ b/lib/model/motive_selection/badge_motive_selection.dart @@ -0,0 +1,22 @@ +// 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 'package:uvok_epaper_badge/model/badge_motive.dart'; + +abstract interface class BadgeMotiveSelection { + Future<BadgeMotive> getCurrentMotive(); + Future<void> setCurrentMotive(BadgeMotive motive); + Future<List<BadgeMotive>> getMotives(); +} diff --git a/lib/model/motive_selection/badge_parser.dart b/lib/model/motive_selection/badge_parser.dart new file mode 100644 index 0000000..e1a7b5c --- /dev/null +++ b/lib/model/motive_selection/badge_parser.dart @@ -0,0 +1,37 @@ +// 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:convert'; + +import 'package:uvok_epaper_badge/model/badge_motive.dart'; +import 'package:uvok_epaper_badge/string_ext.dart'; + +mixin BadgeParser { + List<BadgeMotive> parseBadgeMotives(List<int> val) { + var templates = ascii.decode(val); + var x = templates + .split(";") + .where((s) => s.isNotEmpty) + .map((String s) { + List<String> parts = s.splitFirst("-"); + if (parts.length != 2) { + return BadgeMotive(-1, "Invalid value"); + } + return BadgeMotive(int.tryParse(parts[0]) ?? -1, parts[1]); + }) + .toList(growable: false); + return x; + } +} diff --git a/lib/model/motive_selection/flutter_blue_plus_motive_selection.dart b/lib/model/motive_selection/flutter_blue_plus_motive_selection.dart new file mode 100644 index 0000000..4046d83 --- /dev/null +++ b/lib/model/motive_selection/flutter_blue_plus_motive_selection.dart @@ -0,0 +1,127 @@ +// 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:convert'; + +import 'package:flutter_blue_plus/flutter_blue_plus.dart'; +import 'package:uvok_epaper_badge/model/device/flutter_blue_plus_device.dart'; +import 'package:uvok_epaper_badge/model/motive_selection/badge_parser.dart'; +import 'package:uvok_epaper_badge/badge_exception.dart'; +import 'package:uvok_epaper_badge/model/badge_motive.dart'; +import 'package:uvok_epaper_badge/model/motive_selection/badge_motive_selection.dart'; + +class FlutterBluePlusMotiveSelection + with BadgeParser + implements BadgeMotiveSelection { + final FlutterBluePlusDevice _device; + final String _badgeService = "ca260000-b4bb-46b2-bd06-b7b7a61ea990"; + final String _currentMotiveCharacteristic = + "ca260001-b4bb-46b2-bd06-b7b7a61ea990"; + final String _availableMotivesCharacteristic = + "ca260002-b4bb-46b2-bd06-b7b7a61ea990"; + bool _loadedServices = false; + + List<BadgeMotive> _cachedMotives = []; + + late final BluetoothDevice _fbpDevice; + + FlutterBluePlusMotiveSelection({required FlutterBluePlusDevice device}) + : _device = device { + _fbpDevice = _device.scanResult.device; + } + + @override + Future<BadgeMotive> getCurrentMotive() async { + await _ensureConnected(); + + if (_cachedMotives.isEmpty) { + await getMotives(); + } + if (_cachedMotives.isEmpty) { + throw BadgeException( + "No motives available, so there's no current motive", + ); + } + + try { + var serv = _fbpDevice.servicesList.firstWhere( + (s) => s.uuid.str == _badgeService, + ); + var c = serv.characteristics.firstWhere( + (c) => c.characteristicUuid.str == _currentMotiveCharacteristic, + ); + + var val = await c.read(); + int? currentMotive = int.tryParse(ascii.decode(val)); + if (currentMotive == null) { + throw BadgeException("Error reading current motive."); + } + return _cachedMotives.singleWhere( + (bm) => bm.id == currentMotive, + orElse: () => throw BadgeException("Selected motive not in templates"), + ); + } on StateError { + throw BadgeException("Characeristic/Service not found."); + } + } + + @override + Future<List<BadgeMotive>> getMotives() async { + await _ensureConnected(); + + try { + var serv = _fbpDevice.servicesList.firstWhere( + (s) => s.uuid.str == _badgeService, + ); + var c = serv.characteristics.firstWhere( + (c) => c.characteristicUuid.str == _availableMotivesCharacteristic, + ); + var val = await c.read(); + List<BadgeMotive> x = parseBadgeMotives(val); + _cachedMotives = x; + } on StateError { + throw BadgeException("Characeristic/Service not found."); + } + + return _cachedMotives; + } + + Future<void> _ensureConnected() async { + if (_fbpDevice.isDisconnected) { + throw BadgeException("Not connected"); + } + if (!_loadedServices) { + await _fbpDevice.discoverServices(); + _loadedServices = true; + } + } + + @override + Future<void> setCurrentMotive(BadgeMotive motive) async { + await _ensureConnected(); + try { + var serv = _fbpDevice.servicesList.firstWhere( + (s) => s.uuid.str == _badgeService, + ); + var c = serv.characteristics.firstWhere( + (c) => c.characteristicUuid.str == _currentMotiveCharacteristic, + ); + + await c.write(ascii.encode(motive.id.toString())); + } on StateError { + throw BadgeException("Characeristic/Service not found."); + } + } +} diff --git a/lib/model/motive_selection/mock_badge_motive_selection.dart b/lib/model/motive_selection/mock_badge_motive_selection.dart new file mode 100644 index 0000000..572da2a --- /dev/null +++ b/lib/model/motive_selection/mock_badge_motive_selection.dart @@ -0,0 +1,44 @@ +// 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 'package:uvok_epaper_badge/model/badge_motive.dart'; +import 'package:uvok_epaper_badge/model/motive_selection/badge_motive_selection.dart'; + +class MockBadgeMotiveSelection implements BadgeMotiveSelection { + static const List<BadgeMotive> templates = [ + BadgeMotive(0, "Foo"), + BadgeMotive(1, "Bar"), + BadgeMotive(2, "Baz"), + ]; + + BadgeMotive _currentMotive = templates[0]; + + @override + Future<BadgeMotive> getCurrentMotive() async { + return _currentMotive; + } + + @override + Future<List<BadgeMotive>> getMotives() { + return Future.delayed(Duration(milliseconds: 100), () => templates); + } + + @override + Future<void> setCurrentMotive(BadgeMotive motive) async { + return await Future.delayed(Duration(milliseconds: 300), () { + _currentMotive = motive; + }); + } +} diff --git a/lib/model/motive_selection/universal_blue_motive_selection.dart b/lib/model/motive_selection/universal_blue_motive_selection.dart new file mode 100644 index 0000000..43edd79 --- /dev/null +++ b/lib/model/motive_selection/universal_blue_motive_selection.dart @@ -0,0 +1,112 @@ +// 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:convert'; + +import 'package:universal_ble/universal_ble.dart'; +import 'package:uvok_epaper_badge/model/motive_selection/badge_parser.dart'; +import 'package:uvok_epaper_badge/string_ext.dart'; +import 'package:uvok_epaper_badge/badge_exception.dart'; +import 'package:uvok_epaper_badge/model/badge_motive.dart'; +import 'package:uvok_epaper_badge/model/device/universal_ble_device.dart'; +import 'package:uvok_epaper_badge/model/motive_selection/badge_motive_selection.dart'; + +class UniversalBlueMotiveSelection + with BadgeParser + implements BadgeMotiveSelection { + final UniversalBleDevice _device; + final String _badgeService = "ca260000-b4bb-46b2-bd06-b7b7a61ea990"; + final String _currentMotiveCharacteristic = + "ca260001-b4bb-46b2-bd06-b7b7a61ea990"; + final String _availableMotivesCharacteristic = + "ca260002-b4bb-46b2-bd06-b7b7a61ea990"; + + List<BadgeMotive> _cachedMotives = []; + + UniversalBlueMotiveSelection({required UniversalBleDevice device}) + : _device = device; + + @override + Future<BadgeMotive> getCurrentMotive() async { + await _ensureConnected(); + + if (_cachedMotives.isEmpty) { + await getMotives(); + } + if (_cachedMotives.isEmpty) { + throw BadgeException( + "No motives available, so there's no current motive", + ); + } + + try { + var c = await _device.device.getCharacteristic( + _currentMotiveCharacteristic, + service: _badgeService, + ); + + var val = await c.read(); + int? currentMotive = int.tryParse(ascii.decode(val)); + if (currentMotive == null) { + throw BadgeException("Error reading current motive."); + } + return _cachedMotives.singleWhere( + (bm) => bm.id == currentMotive, + orElse: () => throw BadgeException("Selected motive not in templates"), + ); + } on NotFoundException { + throw BadgeException("Characeristic/Service not found."); + } + } + + @override + Future<List<BadgeMotive>> getMotives() async { + await _ensureConnected(); + + try { + var c = await _device.device.getCharacteristic( + _availableMotivesCharacteristic, + service: _badgeService, + ); + var val = await c.read(); + _cachedMotives = parseBadgeMotives(val); + } on NotFoundException { + throw BadgeException("Characeristic/Service not found."); + } + + return _cachedMotives; + } + + Future<void> _ensureConnected() async { + if (await _device.device.connectionState != BleConnectionState.connected) { + throw BadgeException("Not connected"); + } + } + + @override + Future<void> setCurrentMotive(BadgeMotive motive) async { + await _ensureConnected(); + try { + var c = await _device.device.getCharacteristic( + _currentMotiveCharacteristic, + service: _badgeService, + ); + + await c.write(ascii.encode(motive.id.toString()), withResponse: true); + } on NotFoundException { + throw BadgeException("Characeristic/Service not found."); + } + } +} diff --git a/lib/string_ext.dart b/lib/string_ext.dart new file mode 100644 index 0000000..6119c5b --- /dev/null +++ b/lib/string_ext.dart @@ -0,0 +1,24 @@ +// 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/>. + +extension StringExt on String { + /// split string on first occurrence. + /// if s is not inside string, the original value is returned. + List<String> splitFirst(String s) { + int idx = indexOf(s); + if (idx == -1) return [this]; + return [substring(0, idx).trim(), substring(idx + s.length).trim()]; + } +} diff --git a/lib/view_model/badge_motive_view_model.dart b/lib/view_model/badge_motive_view_model.dart new file mode 100644 index 0000000..64ba387 --- /dev/null +++ b/lib/view_model/badge_motive_view_model.dart @@ -0,0 +1,71 @@ +// 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 'package:flutter/foundation.dart'; +import 'package:logger/logger.dart'; +import 'package:uvok_epaper_badge/model/badge_motive.dart'; +import 'package:uvok_epaper_badge/model/motive_selection/badge_motive_selection.dart'; + +Logger logger = Logger(); + +class BadgeMotiveViewModel extends ChangeNotifier { + final BadgeMotiveSelection _motivSelect; + List<BadgeMotive> _motives = []; + bool _busy = false; + String? errorMessage; + + BadgeMotiveViewModel({required BadgeMotiveSelection motivSelect}) + : _motivSelect = motivSelect; + + bool get allowSelection => !_busy; + List<BadgeMotive> get motives => _motives; + BadgeMotive? currentMotive; + + Future<void> updateMotives() async { + await safeAction(() async { + _motives = await _motivSelect.getMotives(); + }); + } + + Future<void> setMotive(BadgeMotive motive) async { + await safeAction(() async { + logger.t(">Set motive to ${motive.id}"); + await _motivSelect.setCurrentMotive(motive); + logger.t("<Set motive to ${motive.id}"); + currentMotive = motive; + }); + } + + Future<void> getCurrentMotive() async { + await safeAction(() async { + currentMotive = await _motivSelect.getCurrentMotive(); + }); + } + + Future<void> safeAction(Future<void> Function() action) async { + if (_busy) return; + _busy = true; + notifyListeners(); + + try { + await action(); + } on Exception catch (e) { + errorMessage = "${e.runtimeType}: ${e.toString()}"; + } finally { + _busy = false; + notifyListeners(); + } + } +} diff --git a/lib/widgets/badge_app.dart b/lib/widgets/badge_app.dart index f4d1fcd..23c63aa 100644 --- a/lib/widgets/badge_app.dart +++ b/lib/widgets/badge_app.dart @@ -13,6 +13,8 @@ // 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:ui' show AppExitResponse; + import 'package:uvok_epaper_badge/control/scanner_controller.dart'; import 'package:uvok_epaper_badge/widgets/scan_page.dart'; import 'package:flutter/material.dart'; @@ -20,7 +22,15 @@ import 'package:flutter/material.dart'; class BadgeApp extends StatelessWidget { final ScannerController selectedScanner; - const BadgeApp({super.key, required this.selectedScanner}); + BadgeApp({super.key, required this.selectedScanner}) { + AppLifecycleListener( + onExitRequested: () async { + logger.i("Exit requested"); + dispose(); + return AppExitResponse.exit; + }, + ); + } @override Widget build(BuildContext context) { @@ -47,4 +57,8 @@ class BadgeApp extends StatelessWidget { home: ScanPage(title: 'Badge Scanner', deviceScanner: selectedScanner), ); } + + void dispose() { + selectedScanner.dispose(); + } } diff --git a/lib/widgets/badge_motive_list.dart b/lib/widgets/badge_motive_list.dart new file mode 100644 index 0000000..989b155 --- /dev/null +++ b/lib/widgets/badge_motive_list.dart @@ -0,0 +1,74 @@ +// 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 'package:flutter/material.dart'; +import 'package:uvok_epaper_badge/model/badge_motive.dart'; +import 'package:uvok_epaper_badge/view_model/badge_motive_view_model.dart'; +import 'package:uvok_epaper_badge/widgets/notifying_list_widget.dart'; + +class BadgeMotiveList extends NotifyingListWidget<BadgeMotive> { + final BadgeMotiveViewModel _motiveVM; + + const BadgeMotiveList({super.key, required BadgeMotiveViewModel motiveVM}) + : _motiveVM = motiveVM, + super(items: const []); + + @override + State<StatefulWidget> createState() => _BadgeMotiveListState(); +} + +class _BadgeMotiveListState extends State<BadgeMotiveList> { + @override + void initState() { + // I have no idea about the connection state here... + //widget._motiveVM.getCurrentMotive().ignore(); + super.initState(); + } + + @override + Widget build(BuildContext context) { + var mytheme = Theme.of(context); + + return Expanded( + child: ListenableBuilder( + listenable: widget._motiveVM, + builder: (context, child) { + return ListView.separated( + itemBuilder: (context, index) { + final item = widget._motiveVM.motives[index]; + final selected = widget._motiveVM.currentMotive?.id == item.id; + return ListTile( + title: Text(item.description), + selectedTileColor: mytheme.primaryColorLight, + selectedColor: Colors.black, + onTap: !widget._motiveVM.allowSelection + ? null + : () async { + widget.onItemSelected(item); + await widget._motiveVM.setMotive(item); + }, + selected: selected, + ); + }, + separatorBuilder: (context, index) { + return Divider(); + }, + itemCount: widget._motiveVM.motives.length, + ); + }, + ), + ); + } +} diff --git a/lib/widgets/device_details.dart b/lib/widgets/device_details.dart index 79f76cf..0463522 100644 --- a/lib/widgets/device_details.dart +++ b/lib/widgets/device_details.dart @@ -13,10 +13,16 @@ // 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:ui'; + +import 'package:uvok_epaper_badge/model/badge_motive_selection_factory.dart'; import 'package:uvok_epaper_badge/model/device/device.dart'; import 'package:flutter/material.dart'; import 'package:logger/logger.dart'; import 'package:uvok_epaper_badge/model/connection/device_connection.dart'; +import 'package:uvok_epaper_badge/model/motive_selection/badge_motive_selection.dart'; +import 'package:uvok_epaper_badge/view_model/badge_motive_view_model.dart'; +import 'package:uvok_epaper_badge/widgets/badge_motive_list.dart'; var logger = Logger(); @@ -24,11 +30,19 @@ class DeviceDetailsScreen extends StatefulWidget { final Device device; final DeviceConnection deviceConnection; - const DeviceDetailsScreen({ + late final BadgeMotiveSelection _motiveSelection; + late final BadgeMotiveViewModel _motiveVM; + + DeviceDetailsScreen({ super.key, required this.device, required this.deviceConnection, - }); + }) { + _motiveSelection = BadgeMotiveSelectionFactory.createBadgeMotiveSelection( + device, + ); + _motiveVM = BadgeMotiveViewModel(motivSelect: _motiveSelection); + } @override State<StatefulWidget> createState() { @@ -37,11 +51,21 @@ class DeviceDetailsScreen extends StatefulWidget { } class DeviceDetailsState extends State<DeviceDetailsScreen> { - String connectStatus = "<Status>"; + late final AppLifecycleListener appLL; /// Whether the back button should be active. bool backActive = false; + DeviceDetailsState() { + appLL = AppLifecycleListener( + onExitRequested: () async { + logger.i("Exit requested"); + dispose(); + return AppExitResponse.exit; + }, + ); + } + @override Widget build(BuildContext context) { return Scaffold( @@ -50,15 +74,41 @@ class DeviceDetailsState extends State<DeviceDetailsScreen> { title: Text("Device details"), ), body: Center( - child: Column( - spacing: 20, - children: [ - Text(connectStatus), - ElevatedButton( - onPressed: backActive ? backClick : null, - child: Text("Back"), - ), - ], + child: ValueListenableBuilder( + valueListenable: widget.deviceConnection.status, + builder: (connCtx, ConnectionStatus value, child) { + return Column( + spacing: 20, + children: [ + SizedBox(height: 20), + Text("Connection state: ${value.toString()}"), + ElevatedButton( + child: Text("Refresh"), + onPressed: () async { + await widget._motiveVM.updateMotives(); + await widget._motiveVM.getCurrentMotive(); + }, + ), + ListenableBuilder( + listenable: widget._motiveVM, + builder: (errorCtx, child) { + if (widget._motiveVM.errorMessage != null) { + var theme = Theme.of(errorCtx); + return Text( + widget._motiveVM.errorMessage!, + style: TextStyle( + color: theme.colorScheme.error, + fontWeight: FontWeight.bold, + ), + ); + } + return Container(); + }, + ), + BadgeMotiveList(motiveVM: widget._motiveVM), + ], + ); + }, ), ), ); @@ -70,33 +120,29 @@ class DeviceDetailsState extends State<DeviceDetailsScreen> { _doConnect(); } - void backClick() { - Navigator.pop(context); - } - @override void deactivate() { - widget.deviceConnection.disconnect().ignore(); - logger.i("Closing state"); + logger.i("(widget deactivate)"); // widget.device.disconnect().ignore(); super.deactivate(); } - void _doConnect() async { - final dev = widget.device; + @override + void dispose() { + logger.i("(widget dispose)"); + widget.deviceConnection.disconnect().ignore(); + super.dispose(); + } + void _doConnect() async { try { logger.i("Try to connect..."); await widget.deviceConnection.connect(); } catch (e) { logger.e(e); await widget.deviceConnection.disconnect(); - connectStatus = e.toString(); } finally { backActive = true; - if (mounted) { - setState(() {}); - } } } } diff --git a/lib/widgets/device_scan_select.dart b/lib/widgets/device_scan_select.dart index cd65eac..a2e7fc5 100644 --- a/lib/widgets/device_scan_select.dart +++ b/lib/widgets/device_scan_select.dart @@ -33,6 +33,8 @@ class _DeviceScanSelectionState extends State<DeviceScanSelection> { @override Widget build(BuildContext context) { + var mytheme = Theme.of(context); + return Expanded( child: ListView.separated( itemCount: widget.items.length, @@ -45,7 +47,7 @@ class _DeviceScanSelectionState extends State<DeviceScanSelection> { title: Text(name), subtitle: Text(result.address ?? "???"), trailing: Text('RSSI: ${result.rssi}'), - selectedTileColor: Colors.amber, + selectedTileColor: mytheme.primaryColorLight, selectedColor: Colors.black, onTap: () { setState(() { diff --git a/lib/widgets/notifying_list_widget.dart b/lib/widgets/notifying_list_widget.dart index 7fdc2b1..b53cb2f 100644 --- a/lib/widgets/notifying_list_widget.dart +++ b/lib/widgets/notifying_list_widget.dart @@ -22,6 +22,8 @@ abstract class NotifyingListWidget<T> extends StatefulWidget { const NotifyingListWidget({ super.key, required this.items, - required this.onItemSelected, - }); + ValueChanged<T?>? onItemSelected, + }) : onItemSelected = onItemSelected ?? (_noOp); + + static _noOp(_) {} } diff --git a/lib/widgets/scan_page.dart b/lib/widgets/scan_page.dart index bcb3525..f6695fe 100644 --- a/lib/widgets/scan_page.dart +++ b/lib/widgets/scan_page.dart @@ -13,6 +13,8 @@ // 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:io'; + import 'package:uvok_epaper_badge/control/scanner_controller.dart'; import 'package:uvok_epaper_badge/widgets/device_details.dart'; import 'package:uvok_epaper_badge/widgets/device_scan_select.dart'; @@ -61,13 +63,21 @@ class _ScanPageState extends State<ScanPage> { selectedDevice = null; }); - // ... + await getPermissions(); await widget.deviceScanner.startScan(); } Future getPermissions() async { + // avoid spamming log with "unsupported" messages. + if (Platform.isLinux) return; + try { - await Permission.bluetooth.request(); + var status = await Permission.bluetooth.request(); + logger.i("New BLE permission status: $status"); + status = await Permission.bluetoothScan.request(); + logger.i("New BLE scan permission status: $status"); + status = await Permission.bluetoothConnect.request(); + logger.i("New BLE connect permission status: $status"); } catch (e) { logger.e(e.toString()); } @@ -76,8 +86,6 @@ class _ScanPageState extends State<ScanPage> { @override void initState() { super.initState(); - // ehhh... - getPermissions().ignore(); } @override diff --git a/test/widget_test.dart b/test/widget_test.dart index 5fe0b7f..7d13645 100644 --- a/test/widget_test.dart +++ b/test/widget_test.dart @@ -7,7 +7,6 @@ import 'package:uvok_epaper_badge/control/mock_scanner_controller.dart'; import 'package:uvok_epaper_badge/widgets/badge_app.dart'; -import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; void main() { |