diff --git a/lib/features/common/packages/bluetooth/blueberry_ring/domain/blueberry_ring_use_case.dart b/lib/features/common/packages/bluetooth/blueberry_ring/domain/blueberry_ring_use_case.dart index 2a836632..928991bf 100644 --- a/lib/features/common/packages/bluetooth/blueberry_ring/domain/blueberry_ring_use_case.dart +++ b/lib/features/common/packages/bluetooth/blueberry_ring/domain/blueberry_ring_use_case.dart @@ -38,11 +38,6 @@ class BlueberryRingUseCase extends ReactiveUseCase { late final ValueStream bluetoothStatus = reactive(BluetoothAdapterState.off); - // if (state.selectedScanResult != null) { - // responseDevice = BluetoothDevice.getBluetoothDeviceFromScanResult( - // state.selectedScanResult!); - // } - void initBlueberryRingSelectedActions() { selectedBlueberryRing.listen((event) { if (event != null) { @@ -52,7 +47,6 @@ class BlueberryRingUseCase extends ReactiveUseCase { } Future getBlueberryRingBackground() async { - // if (bluetoothStatus.value == BluetoothAdapterState.off || bluetoothStatus.value = BluetoothAdapterState.unauthorized) _bluetoothUseCase.startScanning( withServices: [bluetoothServiceUUID], // withNames: ['Mi Smart Band 4'], @@ -163,18 +157,16 @@ class BlueberryRingUseCase extends ReactiveUseCase { return await func(); } - Future readLevel() async { - return checkEstablishment( + Future readData(Uint8List Function() getCommandFunc, String dataName, + T Function(Uint8List) resolveData) async { + return checkEstablishment( () async { - final command = BlueberryCommands.readLevel(); - collectLog('readLevel:command $command'); + final command = getCommandFunc(); + collectLog('read$dataName:command $command'); // Prepare to listen for the response before writing - final Stream>? stream = - blueberryRingNotificationsCharacteristic.value?.lastValueStream; - if (stream == null) { - throw Exception('Value stream is not available.'); - } + final Stream> stream = + blueberryRingNotificationsCharacteristic.value!.lastValueStream; // Create a completer to handle the response final Completer> completer = Completer>(); @@ -183,7 +175,7 @@ class BlueberryRingUseCase extends ReactiveUseCase { final StreamSubscription> subscription = stream.listen((element) { if (element.isNotEmpty && element.first == command.first) { - collectLog('readLevel:value $element'); + collectLog('read$dataName:value $element'); if (!completer.isCompleted) { completer.complete(element); } @@ -198,107 +190,35 @@ class BlueberryRingUseCase extends ReactiveUseCase { // Cancel the subscription to avoid memory leaks await subscription.cancel(); - collectLog('readLevel:value $value'); - return BlueberryResolves.readLevel(Uint8List.fromList(value)); + collectLog('read$dataName:value $value'); + return resolveData(Uint8List.fromList(value)); }, ); } - Future readVersion() async { - return checkEstablishment( - () async { - final command = BlueberryCommands.readVersion(); - collectLog('readVersion:command $command'); - blueberryRingCharacteristic.value?.write(command); - final value = await blueberryRingNotificationsCharacteristic - .value?.lastValueStream - .firstWhere((element) => - element.isNotEmpty && element.first == command.first); - collectLog('readVersion:value $value'); - return BlueberryResolves.readVersion(Uint8List.fromList(value!)); - }, - ); - } + Future readLevel() async => readData( + BlueberryCommands.readLevel, 'Level', BlueberryResolves.readLevel); - Future readTime() async { - return checkEstablishment( - () async { - final command = BlueberryCommands.readTime(); - collectLog('readTime:command $command'); - blueberryRingCharacteristic.value?.write(command); - final value = await blueberryRingNotificationsCharacteristic - .value?.lastValueStream - .firstWhere((element) => - element.isNotEmpty && element.first == command.first); - collectLog('readTime:value $value'); - return BlueberryResolves.readTime(Uint8List.fromList(value!)); - }, - ); - } + Future readVersion() async => readData( + BlueberryCommands.readVersion, 'Version', BlueberryResolves.readVersion); - Future> readSleep() async { - return checkEstablishment>( - () async { - final command = BlueberryCommands.readSleep(); - collectLog('readSleep:command $command'); - blueberryRingCharacteristic.value?.write(command); - final value = await blueberryRingNotificationsCharacteristic - .value?.lastValueStream - .firstWhere((element) => - element.isNotEmpty && element.first == command.first); - collectLog('readSleep:value $value'); - return BlueberryResolves.readSleep(Uint8List.fromList(value!)); - }, - ); - } + Future readTime() async => readData( + BlueberryCommands.readTime, 'Time', BlueberryResolves.readTime); - Future> readBloodOxygens() async { - return checkEstablishment>( - () async { - final command = BlueberryCommands.readBloodOxygens(); - collectLog('readBloodOxygens:command $command'); - blueberryRingCharacteristic.value?.write(command); - final value = await blueberryRingNotificationsCharacteristic - .value?.lastValueStream - .firstWhere((element) => - element.isNotEmpty && element.first == command.first); - collectLog('readBloodOxygens:value $value'); - return BlueberryResolves.readBloodOxygens(Uint8List.fromList(value!)); - }, - ); - } + Future> readSleep() async => + readData>( + BlueberryCommands.readSleep, 'Sleep', BlueberryResolves.readSleep); - Future> readSteps() async { - return checkEstablishment>( - () async { - final command = BlueberryCommands.readSteps(); - collectLog('readSteps:command $command'); - blueberryRingCharacteristic.value?.write(command); - final value = await blueberryRingNotificationsCharacteristic - .value?.lastValueStream - .firstWhere((element) => - element.isNotEmpty && element.first == command.first); - collectLog('readSteps:value $value'); - return BlueberryResolves.readSteps(Uint8List.fromList(value!)); - }, - ); - } + Future> readBloodOxygens() async => + readData>(BlueberryCommands.readBloodOxygens, + 'BloodOxygens', BlueberryResolves.readBloodOxygens); - Future> readHeartRate() async { - return checkEstablishment>( - () async { - final command = BlueberryCommands.readHeartRates(); - collectLog('readHeartRate:command $command'); - blueberryRingCharacteristic.value?.write(command); - final value = await blueberryRingNotificationsCharacteristic - .value?.lastValueStream - .firstWhere((element) => - element.isNotEmpty && element.first == command.first); - collectLog('readHeartRate:value $value'); - return BlueberryResolves.readHeartRates(Uint8List.fromList(value!)); - }, - ); - } + Future> readSteps() async => readData>( + BlueberryCommands.readSteps, 'Steps', BlueberryResolves.readSteps); + + Future> readHeartRate() async => + readData>(BlueberryCommands.readHeartRates, + 'HeartRate', BlueberryResolves.readHeartRates); Future getBlueberryRingCharacteristic() async { final service = await getBlueberryRingBluetoothService(); @@ -317,15 +237,6 @@ class BlueberryRingUseCase extends ReactiveUseCase { return resp; } - // Future - // getBlueberryRingCharacteristicNotifications() async { - // final service = await getBlueberryRingBluetoothService(); - // final resp = await _getBlueberryRingCharacteristic( - // service, bluetoothCharacteristicNotificationUUID); - // update(blueberryRinNotificationsCharacteristic, resp); - // return resp; - // } - // Future startBlueberryRingCharacteristicNotifications() async { // final characteristicNotifications = // await getBlueberryRingCharacteristicNotifications();