jamesmontemagno / BatteryPlugin

Battery Plugin for Xamarin and Windows
MIT License
27 stars 18 forks source link

Battery Status Plugin for Xamarin and Windows

Simple cross platform plugin to check battery status of mobile device, get remaining percentage for Xamarin.iOS, Xamarin.Android, Windows, and Xamarin.Forms projects.

Setup

Build Status:

Migrate to: Xamarin.Essentials or .NET MAUI

I have been working on Plugins for Xamarin for a long time now. Through the years I have always wanted to create a single, optimized, and official package from the Xamarin team at Microsoft that could easily be consumed by any application. The time is now with Xamarin.Essentials, which offers over 50 cross-platform native APIs in a single optimized package. I worked on this new library with an amazing team of developers and I highly highly highly recommend you check it out.

Additionally, Xamarin.Essentials is now included in & .NET MAUI.

Due to the functionality being included "in the box" I have decided to officially archive this repo.

Platform Support

Platform Version
Xamarin.iOS iOS 6+
Xamarin.Android API 10+
Windows 10 UWP 10+
Tizen.NET 4.0+

Windows Store has a blank DLL that always returns 100, AC, and Full as there is no API for checking battery

API Usage

Call CrossBattery.Current from any project or PCL to gain access to APIs.

RemainingChargePercent

/// <summary>
/// Current battery level 0 - 100
/// </summary>
int RemainingChargePercent { get; }

Status

/// <summary>
/// Current status of the battery
/// </summary>
BatteryStatus Status { get; }

This returns an enum with the current status of the battery. If charging or not:

/// <summary>
/// Current status of battery
/// </summary>
public enum BatteryStatus
{
  /// <summary>
  /// Plugged in and charging
  /// </summary>
  Charging,
  /// <summary>
  /// Battery is being drained currently
  /// </summary>
  Discharging,
  /// <summary>
  /// Battery is full completely
  /// </summary>
  Full,
  /// <summary>
  /// Not charging, but not discharging either
  /// </summary>
  NotCharging,
  /// <summary>
  /// Unknown or other status
  /// </summary>
  Unknown

}

Important:

PowerSource

/// <summary>
/// Currently how the battery is being charged.
/// </summary>
PowerSource PowerSource { get; }

Returns how the phone is being charged

Events

You can subscribe to BatteryChanged, which will return BatteryChangedEventArgs with all information you need. This occurs when plugged, unplugged, or battery change.

/// <summary>
/// Event handler when battery changes
/// </summary>
event BatteryChangedEventHandler BatteryChanged;

License

Under MIT, see LICENSE file.