From 795d47bf292febe1512c308439cf092dc426ef67 Mon Sep 17 00:00:00 2001 From: reasje Date: Thu, 1 Aug 2024 16:40:15 +0330 Subject: [PATCH 1/2] fix: BG fetch data retrieval chunks --- .../domain/blueberry_ring_use_case.dart | 59 ++++++++++++++----- .../utils/blueberry_commands_utils.dart | 3 +- 2 files changed, 47 insertions(+), 15 deletions(-) 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 928991bf..096e0807 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 @@ -158,7 +158,7 @@ class BlueberryRingUseCase extends ReactiveUseCase { } Future readData(Uint8List Function() getCommandFunc, String dataName, - T Function(Uint8List) resolveData) async { + T Function(Uint8List) resolveData, bool isFrag) async { return checkEstablishment( () async { final command = getCommandFunc(); @@ -171,13 +171,29 @@ class BlueberryRingUseCase extends ReactiveUseCase { // Create a completer to handle the response final Completer> completer = Completer>(); + Timer? timer; + List data = []; + // Subscribe to the stream and filter for the specific command final StreamSubscription> subscription = stream.listen((element) { if (element.isNotEmpty && element.first == command.first) { + timer?.cancel(); collectLog('read$dataName:value $element'); - if (!completer.isCompleted) { - completer.complete(element); + data.addAll(element); + collectLog('read$dataName:value $data'); + + if (!completer.isCompleted && !isFrag) { + completer.complete(data); + } else if (!completer.isCompleted && + isFrag && + element.last == 255) { + completer.complete(data); + } else { + timer = Timer( + const Duration(milliseconds: 5000), + () => completer.complete(data), + ); } } }); @@ -196,29 +212,44 @@ class BlueberryRingUseCase extends ReactiveUseCase { ); } - Future readLevel() async => readData( - BlueberryCommands.readLevel, 'Level', BlueberryResolves.readLevel); + Future readLevel() async => readData(BlueberryCommands.readLevel, + 'Level', BlueberryResolves.readLevel, BlueberryMethods.readLevel.frag); Future readVersion() async => readData( - BlueberryCommands.readVersion, 'Version', BlueberryResolves.readVersion); + BlueberryCommands.readVersion, + 'Version', + BlueberryResolves.readVersion, + BlueberryMethods.readVersion.frag); Future readTime() async => readData( - BlueberryCommands.readTime, 'Time', BlueberryResolves.readTime); + BlueberryCommands.readTime, + 'Time', + BlueberryResolves.readTime, + BlueberryMethods.readTime.frag); Future> readSleep() async => - readData>( - BlueberryCommands.readSleep, 'Sleep', BlueberryResolves.readSleep); + readData>(BlueberryCommands.readSleep, 'Sleep', + BlueberryResolves.readSleep, BlueberryMethods.readSleep.frag); Future> readBloodOxygens() async => - readData>(BlueberryCommands.readBloodOxygens, - 'BloodOxygens', BlueberryResolves.readBloodOxygens); + readData>( + BlueberryCommands.readBloodOxygens, + 'BloodOxygens', + BlueberryResolves.readBloodOxygens, + BlueberryMethods.readBloodOxygens.frag); Future> readSteps() async => readData>( - BlueberryCommands.readSteps, 'Steps', BlueberryResolves.readSteps); + BlueberryCommands.readSteps, + 'Steps', + BlueberryResolves.readSteps, + BlueberryMethods.readSteps.frag); Future> readHeartRate() async => - readData>(BlueberryCommands.readHeartRates, - 'HeartRate', BlueberryResolves.readHeartRates); + readData>( + BlueberryCommands.readHeartRates, + 'HeartRate', + BlueberryResolves.readHeartRates, + BlueberryMethods.readHeartRates.frag); Future getBlueberryRingCharacteristic() async { final service = await getBlueberryRingBluetoothService(); diff --git a/lib/features/common/packages/bluetooth/blueberry_ring/utils/blueberry_commands_utils.dart b/lib/features/common/packages/bluetooth/blueberry_ring/utils/blueberry_commands_utils.dart index 78d0ef09..5f079853 100644 --- a/lib/features/common/packages/bluetooth/blueberry_ring/utils/blueberry_commands_utils.dart +++ b/lib/features/common/packages/bluetooth/blueberry_ring/utils/blueberry_commands_utils.dart @@ -3,8 +3,9 @@ import 'dart:typed_data'; class Method { final int uid; final List? arg; + final bool frag; - Method({required this.uid, this.arg}); + Method({required this.uid, this.arg, this.frag = false}); } class BlueberryCommandsUtils { From a4b242bafe4f294a9fa5125d77fa455e75a19f87 Mon Sep 17 00:00:00 2001 From: reasje Date: Thu, 1 Aug 2024 16:40:46 +0330 Subject: [PATCH 2/2] feat: Methods config update --- .../bluetooth/blueberry_ring/utils/blueberry_configs.dart | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/features/common/packages/bluetooth/blueberry_ring/utils/blueberry_configs.dart b/lib/features/common/packages/bluetooth/blueberry_ring/utils/blueberry_configs.dart index 6dcc2e70..4757063b 100644 --- a/lib/features/common/packages/bluetooth/blueberry_ring/utils/blueberry_configs.dart +++ b/lib/features/common/packages/bluetooth/blueberry_ring/utils/blueberry_configs.dart @@ -15,11 +15,11 @@ class GetMappings { class BlueberryMethods { static final readLevel = Method(uid: 0x13, arg: [1]); static final readVersion = Method(uid: 0x27); - static final readSteps = Method(uid: 0x52, arg: [1]); static final readTime = Method(uid: 0x41); - static final readSleep = Method(uid: 0x53, arg: [1, 2, 3]); - static final readHeartRates = Method(uid: 0x55, arg: [1, 2, 3]); - static final readBloodOxygens = Method(uid: 0x66, arg: [1, 2, 3]); + static final readSteps = Method(uid: 0x52, arg: [1], frag: true); + static final readSleep = Method(uid: 0x53, arg: [1, 2, 3], frag: true); + static final readHeartRates = Method(uid: 0x55, arg: [1, 2, 3], frag: true); + static final readBloodOxygens = Method(uid: 0x66, arg: [1, 2, 3], frag: true); static final writeTime = Method(uid: 0x01, arg: [1, 2, 3, 4, 5, 6]); static final writeRestore = Method(uid: 0x12, arg: []); }