Saturday, 2 August 2025

Checking Battery Level in .NET MAUI

In .NET MAUI, you can check the device's battery level using the Battery class from the Microsoft.Maui.Devices namespace. Here's how to implement it:

Basic Implementation

  1. First, add the necessary namespace to your file:

csharp
using Microsoft.Maui.Devices;
  1. Get the battery level:

csharp
public double GetBatteryLevel()
{
    // Returns a value between 0.0 and 1.0
    return Battery.Default.ChargeLevel; 
}

Complete Example with UI

Here's a more complete example with a UI that displays the battery level:

csharp
using Microsoft.Maui.Controls;
using Microsoft.Maui.Devices;

namespace YourNamespace
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
            CheckBattery();
            
            // Optional: Subscribe to battery level changes
            Battery.Default.BatteryInfoChanged += Battery_BatteryInfoChanged;
        }

        private void CheckBattery()
        {
            var level = Battery.Default.ChargeLevel;
            BatteryLabel.Text = $"Battery Level: {level * 100}%";
            
            // You can also check other properties:
            var state = Battery.Default.State;
            var powerSource = Battery.Default.PowerSource;
        }

        private void Battery_BatteryInfoChanged(object sender, BatteryInfoChangedEventArgs e)
        {
            // Update UI when battery level changes
            MainThread.BeginInvokeOnMainThread(() =>
            {
                BatteryLabel.Text = $"Battery Level: {e.ChargeLevel * 100}%";
                
                // You can also check:
                // e.State - Charging, Discharging, Full, NotCharging, Unknown
                // e.PowerSource - Battery, AC, Usb, Wireless, Unknown
            });
        }
    }
}

XAML for the Example

xml
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="YourNamespace.MainPage">
    <VerticalStackLayout>
        <Label x:Name="BatteryLabel"
               Text="Checking battery level..."
               HorizontalOptions="Center"
               VerticalOptions="Center" />
    </VerticalStackLayout>
</ContentPage>

Important Notes

  1. Permissions: On Android, you need to add the BATTERY_STATS permission to your Platforms/Android/AndroidManifest.xml:

xml
<uses-permission android:name="android.permission.BATTERY_STATS" />
  1. Platform Differences:

    • On iOS, you can only read the battery level (not write)

    • On Windows, this requires the "Power" capability

  2. Battery State: You can also check:

    • Battery.Default.State (Charging, Discharging, Full, NotCharging, Unknown)

    • Battery.Default.PowerSource (Battery, AC, Usb, Wireless, Unknown)

  3. Energy Saver: You can also check if the device is in energy saver mode:

csharp
var energySaverStatus = Battery.Default.EnergySaverStatus;

No comments:

Post a Comment

Complete Guide: Building a Live Cricket Streaming App for 100M Users

Comprehensive guide to building a scalable live cricket streaming platform for 100M users, covering backend infrastructure, streaming techno...