Detecting And Get The Battery Status In Windows 10 UWP App

Introduction 

 
I am going to show you how you can get the status if the battery is charged, discharged and the specific amount of percentage. It will show you how you can help users with just a few lines of code and ensure that you respect their device’s battery life. It is also useful to notify the battery status before your app performs any battery intensive operation performed by asking plug-in charge when the battery is low and no power supplied.
 

Let’s start

 
Create a new Windows 10 project and design using the following XAML code,
  1. <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">    
  2.     <Grid.RowDefinitions>    
  3.         <RowDefinition Height="*"></RowDefinition>    
  4.         <RowDefinition Height="*"></RowDefinition>    
  5.     </Grid.RowDefinitions>    
  6.     <TextBlock x:Name="batteryStatusbox" HorizontalAlignment="Center" Grid.Row="0" VerticalAlignment="Center">  
  7.     </TextBlock>    
  8.     <ProgressBar x:Name="statusinPer" HorizontalAlignment="Center" Height="40" Grid.Row="1" VerticalAlignment="Top" Width="360" SmallChange="0.1" />     
  9. </Grid>    

Now go to code-behind page

 
The Windows.Devices.Power namespaces provide a set of API to deal with battery.
 
Battery class provides information about a battery controller that is currently connected or not in your device.
 
GetReport() method helps you to return a BatteryReport object that indicates the charge, capacity, and status of the battery.
 
Write the following code,
  1. public sealed partial class MainPage: Page    
  2. {    
  3.     public MainPage()    
  4.     {    
  5.         this.InitializeComponent();    
  6.         Windows.Devices.Power.Battery.AggregateBattery.ReportUpdated += AggregateBatteryOnReportUpdated;    
  7.     }    
  8.     private async void AggregateBatteryOnReportUpdated(Windows.Devices.Power.Battery sender, object args)    
  9.     {    
  10.         await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>    
  11.         {    
  12.             // await UpdatePercentage(sender);    
  13.             var details = sender.GetReport();    
  14.             var getPercentage = (details.RemainingCapacityInMilliwattHours.Value / (double) details.FullChargeCapacityInMilliwattHours.Value);    
  15.             var getStatus = details.Status;    
  16.             string per = getPercentage.ToString("##%");    
  17.             statusinPer.Value = getPercentage;    
  18.             batteryStatusbox.Text = "Status: " + getStatus + "-Percentage :" + per;    
  19.         });    
  20.     }    
  21. }    
We get the battery capacity value in milliwat’s and we need to convert it to percentage so I am changing the value using the following code.
  1. string per = getPercentage.ToString("##%");    
 
Subscribe the Event handler ReportUpdated event-triggered automatically when there are any changes in charge, capacity, or status.
 
Now run the app and see the output look like the following screen.
 
When the battery is not charged,
 
battery
 
When the battery is charging,
 
charging
 

Summary

 
In this article, we learned about Detecting And Get The Battery Status In Windows 10 UWP App.