migueldeicaza / BatteryPlugin

Battery Plugin for Xamarin and Windows

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Battery Status Plugin for Xamarin and Windows

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

Setup

Build Status:

Platform Support

|Platform|Supported|Version| | ------------------- | :------------------: | |Xamarin.iOS|iOS 6+| |Xamarin.Android|API 10+| |Windows 10 UWP|10+|

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:

  • iOS: only returns Charging, Full, Discharging, and Unknown.

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.

About

Battery Plugin for Xamarin and Windows

License:MIT License


Languages

Language:C# 79.0%Language:PowerShell 15.0%Language:Shell 6.0%