Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

Commit

Permalink
Merge pull request #271 from MXCzkEVM/blueberry_ring
Browse files Browse the repository at this point in the history
fix: Read battery
  • Loading branch information
reasje authored Jul 30, 2024
2 parents 67bf716 + 80ce9a8 commit d20228a
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ class BlueberryRingUseCase extends ReactiveUseCase {
}

Future<void> connectToBlueberryRing() async {
await _bluetoothUseCase.connectionHandler(selectedBlueberryRing.value!.device);
await _bluetoothUseCase
.connectionHandler(selectedBlueberryRing.value!.device);
}

Future<BluetoothService> getBlueberryRingBluetoothService() async {
Expand All @@ -126,7 +127,8 @@ class BlueberryRingUseCase extends ReactiveUseCase {
'checkEstablishment:isBlueberryRingConnected $isBlueberryRingConnected');

if (!isBlueberryRingConnected) {
await _bluetoothUseCase.connectionHandler(selectedBlueberryRing.value!.device);
await _bluetoothUseCase
.connectionHandler(selectedBlueberryRing.value!.device);
isBlueberryRingConnected =
selectedBlueberryRing.value?.device.isConnected ?? false;
if (!isBlueberryRingConnected) {
Expand All @@ -150,7 +152,8 @@ class BlueberryRingUseCase extends ReactiveUseCase {
await getBlueberryRingNotificationsCharacteristic();
}

final isNotifying = blueberryRingNotificationsCharacteristic.value!.isNotifying;
final isNotifying =
blueberryRingNotificationsCharacteristic.value!.isNotifying;
collectLog(
'checkEstablishment:isBlueberryRingNotificationsCharacteristicNotifiying $isNotifying');
if (!isNotifying) {
Expand All @@ -165,10 +168,38 @@ class BlueberryRingUseCase extends ReactiveUseCase {
() async {
final command = BlueberryCommands.readLevel();
collectLog('readLevel:command $command');

// Prepare to listen for the response before writing
final Stream<List<int>>? stream =
blueberryRingNotificationsCharacteristic.value?.lastValueStream;
if (stream == null) {
throw Exception('Value stream is not available.');
}

// Create a completer to handle the response
final Completer<List<int>> completer = Completer<List<int>>();

// Subscribe to the stream and filter for the specific command
final StreamSubscription<List<int>> subscription =
stream.listen((element) {
if (element.isNotEmpty && element.first == command.first) {
collectLog('readLevel:value $element');
if (!completer.isCompleted) {
completer.complete(element);
}
}
});

blueberryRingCharacteristic.value?.write(command);
final value = await blueberryRingNotificationsCharacteristic.value?.lastValueStream.firstWhere((element) => element.isNotEmpty && element.first == command.first);

// Wait for the expected value to be received
final List<int> value = await completer.future;

// Cancel the subscription to avoid memory leaks
await subscription.cancel();

collectLog('readLevel:value $value');
return BlueberryResolves.readLevel(Uint8List.fromList(value!));
return BlueberryResolves.readLevel(Uint8List.fromList(value));
},
);
}
Expand All @@ -179,7 +210,10 @@ class BlueberryRingUseCase extends ReactiveUseCase {
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);
final value = await blueberryRingNotificationsCharacteristic
.value?.lastValueStream
.firstWhere((element) =>
element.isNotEmpty && element.first == command.first);
collectLog('readVersion:value $value');
return BlueberryResolves.readVersion(Uint8List.fromList(value!));
},
Expand All @@ -192,7 +226,10 @@ class BlueberryRingUseCase extends ReactiveUseCase {
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);
final value = await blueberryRingNotificationsCharacteristic
.value?.lastValueStream
.firstWhere((element) =>
element.isNotEmpty && element.first == command.first);
collectLog('readTime:value $value');
return BlueberryResolves.readTime(Uint8List.fromList(value!));
},
Expand All @@ -205,7 +242,10 @@ class BlueberryRingUseCase extends ReactiveUseCase {
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);
final value = await blueberryRingNotificationsCharacteristic
.value?.lastValueStream
.firstWhere((element) =>
element.isNotEmpty && element.first == command.first);
collectLog('readSleep:value $value');
return BlueberryResolves.readSleep(Uint8List.fromList(value!));
},
Expand All @@ -218,7 +258,10 @@ class BlueberryRingUseCase extends ReactiveUseCase {
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);
final value = await blueberryRingNotificationsCharacteristic
.value?.lastValueStream
.firstWhere((element) =>
element.isNotEmpty && element.first == command.first);
collectLog('readBloodOxygens:value $value');
return BlueberryResolves.readBloodOxygens(Uint8List.fromList(value!));
},
Expand All @@ -231,7 +274,10 @@ class BlueberryRingUseCase extends ReactiveUseCase {
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);
final value = await blueberryRingNotificationsCharacteristic
.value?.lastValueStream
.firstWhere((element) =>
element.isNotEmpty && element.first == command.first);
collectLog('readSteps:value $value');
return BlueberryResolves.readSteps(Uint8List.fromList(value!));
},
Expand All @@ -244,7 +290,10 @@ class BlueberryRingUseCase extends ReactiveUseCase {
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);
final value = await blueberryRingNotificationsCharacteristic
.value?.lastValueStream
.firstWhere((element) =>
element.isNotEmpty && element.first == command.first);
collectLog('readHeartRate:value $value');
return BlueberryResolves.readHeartRates(Uint8List.fromList(value!));
},
Expand All @@ -259,8 +308,8 @@ class BlueberryRingUseCase extends ReactiveUseCase {
return resp;
}


Future<BluetoothCharacteristic> getBlueberryRingNotificationsCharacteristic() async {
Future<BluetoothCharacteristic>
getBlueberryRingNotificationsCharacteristic() async {
final service = await getBlueberryRingBluetoothService();
final resp = await _getBlueberryRingCharacteristic(
service, bluetoothCharacteristicNotificationUUID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ class SettingsPresenter extends CompletePresenter<SettingsState> {
}

void testOnly() async {
await blueberryRingBackgroundNotificationsUseCase.checkLowBattery();
await blueberryRingBackgroundNotificationsUseCase.checkActivityReminder();
await blueberryRingBackgroundNotificationsUseCase.checkSleepInsight();
await blueberryRingBackgroundNotificationsUseCase.checkHeartAlert();
await blueberryRingBackgroundNotificationsUseCase.checkLowBattery();
}
}

0 comments on commit d20228a

Please sign in to comment.