Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions app/lib/providers/device_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ class DeviceProvider extends ChangeNotifier implements IDeviceServiceSubsciption
return await connection.disconnect();
}

Future<int> _retrieveBatteryLevel(String deviceId) async {
var connection = await ServiceManager.instance().device.ensureConnection(deviceId);
if (connection == null) {
return -1;
}
return connection.retrieveBatteryLevel();
}

Future<StreamSubscription<List<int>>?> _getBleBatteryLevelListener(
String deviceId, {
void Function(int)? onBatteryLevelChange,
Expand Down Expand Up @@ -304,6 +312,13 @@ class DeviceProvider extends ChangeNotifier implements IDeviceServiceSubsciption
setisDeviceStorageSupport();
setIsConnected(true);

// Read initial battery level
int currentLevel = await _retrieveBatteryLevel(device.id);
if (currentLevel != -1) {
batteryLevel = currentLevel;
}

// Then set up listener for battery changes
await initiateBleBatteryListener();
if (batteryLevel != -1 && batteryLevel < 20) {
_hasLowBatteryAlerted = false;
Expand Down