From 1df17747231fa7bd179a1d067562b364e6c161f5 Mon Sep 17 00:00:00 2001 From: reasje Date: Mon, 8 Jul 2024 17:24:06 +0330 Subject: [PATCH] feat: bluetooth turn on check --- .../blue_plus/bluetooth_use_case.dart | 49 ++++++++++++++----- 1 file changed, 37 insertions(+), 12 deletions(-) diff --git a/lib/features/common/packages/bluetooth/blue_plus/bluetooth_use_case.dart b/lib/features/common/packages/bluetooth/blue_plus/bluetooth_use_case.dart index 59ced3e7..2a9002b4 100644 --- a/lib/features/common/packages/bluetooth/blue_plus/bluetooth_use_case.dart +++ b/lib/features/common/packages/bluetooth/blue_plus/bluetooth_use_case.dart @@ -22,9 +22,12 @@ class BluetoothTimeoutError extends Error { class BluetoothUseCase extends ReactiveUseCase { BluetoothUseCase( - this._repository, this._chainConfigurationUseCase, this._authUseCase){ - initBluetoothUseCase(); - } + this._repository, + this._chainConfigurationUseCase, + this._authUseCase, + ) { + initBluetoothUseCase(); + } final Web3Repository _repository; final ChainConfigurationUseCase _chainConfigurationUseCase; @@ -101,19 +104,40 @@ class BluetoothUseCase extends ReactiveUseCase { // Try to turn on await turnOnBluetooth(); - // Wait till IT's turned on - await bluetoothStatus.firstWhere((event) => event == BluetoothAdapterState.on).timeout(const Duration(seconds: 5), onTimeout: () => throw BluetoothTimeoutError() ); + // Wait till IT's turned on + await bluetoothStatus + .firstWhere((event) => event == BluetoothAdapterState.on) + .timeout(const Duration(seconds: 5), + onTimeout: () => throw BluetoothTimeoutError()); + } + + Future checkBluetoothTurnedOn( + Future Function() turnOnBluetoothFunction) async { + final isBluetoothOn = await isBluetoothTurnedOn(); + if (!isBluetoothOn) { + await turnOnBluetoothFunction(); + } } Future turnOnBluetooth() async { - // turn on bluetooth ourself if we can - // for iOS, the user controls bluetooth enable/disable - if (Platform.isAndroid) { - await FlutterBluePlus.turnOn(); + await checkBluetoothTurnedOn(() async { + // turn on bluetooth ourself if we can + // for iOS, the user controls bluetooth enable/disable + if (Platform.isAndroid) { + await FlutterBluePlus.turnOn(); + } else { + // IOS + await AppSettings.openAppSettings(type: AppSettingsType.bluetooth); + } + }); + } + + Future isBluetoothTurnedOn() async { + if (bluetoothStatus.value == BluetoothAdapterState.on) { + return true; } else { - // IOS - await AppSettings.openAppSettings(type: AppSettingsType.bluetooth); + return false; } } @@ -134,9 +158,10 @@ class BluetoothUseCase extends ReactiveUseCase { }) async { // Start scanning w/ timeout // Optional: use `stopScan()` as an alternative to timeout - if (isScanning.value == true) { + if (isScanning.value == true) { return; } + await FlutterBluePlus.startScan( withServices: withServices ?? [], withNames: withNames ?? [],