diff --git a/app/lib/features/bluetooth/backend/bluetooth_device.dart b/app/lib/features/bluetooth/backend/bluetooth_device.dart index 9c941be2..5d772df7 100644 --- a/app/lib/features/bluetooth/backend/bluetooth_device.dart +++ b/app/lib/features/bluetooth/backend/bluetooth_device.dart @@ -164,7 +164,13 @@ abstract class BluetoothDevice< } }, onError: onError); - await backendConnect(); + try { + await backendConnect(); + } catch (e) { + logger.severe('Failed to connect to device', e); + if (!completer.isCompleted) completer.complete(false); + } + return completer.future.then((res) { logger.finer('connect: completer.resolved($res)'); return res; @@ -317,17 +323,18 @@ mixin CharacteristicValueListener< disconnectCallbacks.add(disconnectCallback); - final listener = characteristicValueStream.listen((value) { - if (value == null) { - // ignore null values - return; - } + final listener = characteristicValueStream.listen( + (value) { + if (value == null) { + // ignore null values + return; + } - logger.finer('listenCharacteristicValue[${value.length}] $value'); + logger.finer('listenCharacteristicValue[${value.length}] $value'); - receivedSomeData = true; - onValue(value, completer.complete); - }, + receivedSomeData = true; + onValue(value, completer.complete); + }, cancelOnError: true, onDone: () { logger.finer('listenCharacteristicValue: onDone called'); @@ -345,9 +352,13 @@ mixin CharacteristicValueListener< _readCharacteristicCompleters.add(completer); _readCharacteristicListeners.add(listener); - final bool triggerSuccess = await triggerCharacteristicValue(characteristic); - if (!triggerSuccess) { - logger.warning('listenCharacteristicValue: triggerCharacteristicValue returned $triggerSuccess'); + try { + final bool triggerSuccess = await triggerCharacteristicValue(characteristic); + if (!triggerSuccess) { + logger.warning('listenCharacteristicValue: triggerCharacteristicValue returned $triggerSuccess'); + } + } catch (e) { + logger.severe('Error occured while triggering characteristic', e); } return completer.future.then((res) { diff --git a/app/lib/features/bluetooth/backend/bluetooth_low_energy/ble_manager.dart b/app/lib/features/bluetooth/backend/bluetooth_low_energy/ble_manager.dart index dbb31b5c..de92bc24 100644 --- a/app/lib/features/bluetooth/backend/bluetooth_low_energy/ble_manager.dart +++ b/app/lib/features/bluetooth/backend/bluetooth_low_energy/ble_manager.dart @@ -20,7 +20,15 @@ final class BluetoothLowEnergyManager extends BluetoothManager enable() async => Platform.isAndroid && await backend.authorize(); + Future enable() async { + if (!Platform.isAndroid) { + return null; + } + + final b = await backend.authorize(); + await backend.showAppSettings(); + return b; + } /// The actual backend implementation final CentralManager backend = CentralManager(); diff --git a/app/lib/features/bluetooth/backend/bluetooth_manager.dart b/app/lib/features/bluetooth/backend/bluetooth_manager.dart index 7279f803..d43fc450 100644 --- a/app/lib/features/bluetooth/backend/bluetooth_manager.dart +++ b/app/lib/features/bluetooth/backend/bluetooth_manager.dart @@ -26,7 +26,7 @@ abstract class BluetoothManager enable(); // TODO: use task specific plugin/native code + Future enable(); // TODO: use task specific plugin/native code /// Last known adapter state /// diff --git a/app/lib/features/bluetooth/backend/bluetooth_service.dart b/app/lib/features/bluetooth/backend/bluetooth_service.dart index 4fd020d4..a8f3d13f 100644 --- a/app/lib/features/bluetooth/backend/bluetooth_service.dart +++ b/app/lib/features/bluetooth/backend/bluetooth_service.dart @@ -1,4 +1,3 @@ -import 'package:blood_pressure_app/logging.dart'; import 'package:collection/collection.dart'; /// Bluetooth Base UUID from Bluetooth Core Spec diff --git a/app/lib/features/bluetooth/backend/flutter_blue_plus/fbp_manager.dart b/app/lib/features/bluetooth/backend/flutter_blue_plus/fbp_manager.dart index 3034658e..cedc6f14 100644 --- a/app/lib/features/bluetooth/backend/flutter_blue_plus/fbp_manager.dart +++ b/app/lib/features/bluetooth/backend/flutter_blue_plus/fbp_manager.dart @@ -23,12 +23,12 @@ class FlutterBluePlusManager extends BluetoothManager enable() async { + Future enable() async { if (Platform.isAndroid) { await backend.turnOn(); return true; } - return false; + return null; } @override diff --git a/app/lib/features/bluetooth/backend/mock/mock_manager.dart b/app/lib/features/bluetooth/backend/mock/mock_manager.dart index ad743fa5..92e0cc35 100644 --- a/app/lib/features/bluetooth/backend/mock/mock_manager.dart +++ b/app/lib/features/bluetooth/backend/mock/mock_manager.dart @@ -11,7 +11,7 @@ final class MockBluetoothManager extends BluetoothManager get discovery => throw UnimplementedError(); @override - Future enable() async => false; + Future enable() async => null; @override BluetoothAdapterState get lastKnownAdapterState => BluetoothAdapterState.initial; diff --git a/app/lib/features/bluetooth/logic/bluetooth_cubit.dart b/app/lib/features/bluetooth/logic/bluetooth_cubit.dart index 65a8c235..6f242a62 100644 --- a/app/lib/features/bluetooth/logic/bluetooth_cubit.dart +++ b/app/lib/features/bluetooth/logic/bluetooth_cubit.dart @@ -36,7 +36,7 @@ class BluetoothCubit extends Cubit with TypeLogger { void _onAdapterStateChanged(BluetoothAdapterState state) async { if (state == BluetoothAdapterState.unauthorized) { final success = await manager.enable(); - if (!success) { + if (success != true) { logger.warning('Enabling bluetooth failed or not needed on this platform'); } } @@ -47,7 +47,7 @@ class BluetoothCubit extends Cubit with TypeLogger { } /// Request to enable bluetooth on the device - Future enableBluetooth() async { + Future enableBluetooth() async { assert(state is BluetoothStateDisabled, 'No need to enable bluetooth when ' 'already enabled or not known to be disabled.'); try { diff --git a/app/lib/features/bluetooth/ui/closed_bluetooth_input.dart b/app/lib/features/bluetooth/ui/closed_bluetooth_input.dart index 025d4a9a..f7e841cf 100644 --- a/app/lib/features/bluetooth/ui/closed_bluetooth_input.dart +++ b/app/lib/features/bluetooth/ui/closed_bluetooth_input.dart @@ -62,7 +62,7 @@ class ClosedBluetoothInput extends StatelessWidget with TypeLogger { icon: Icons.bluetooth_disabled, onTap: () async { final bluetoothOn = await bluetoothCubit.enableBluetooth(); - if (!bluetoothOn) await AppSettings.openAppSettings(type: AppSettingsType.bluetooth); + if (bluetoothOn == false) await AppSettings.openAppSettings(type: AppSettingsType.bluetooth); bluetoothCubit.forceRefresh(); }, ),