Tal Angel
Tal Angel

Reputation: 1788

How to know if the device is plugged in using C# and Unity

I am building a game, and would like to know: How to know if an Android device is plugged-in (i.e. battery is charging) using C# pure code in Unity?

Upvotes: 3

Views: 1685

Answers (2)

Doh09
Doh09

Reputation: 2385

Device is plugged in:

SystemInfo.batteryStatus == BatteryStatus.Charging || SystemInfo.batteryStatus == BatteryStatus.Full || SystemInfo.batteryStatus == BatteryStatus.NotCharging

Battery is charging:

SystemInfo.batteryStatus == BatteryStatus.Charging

Sources:

https://docs.unity3d.com/ScriptReference/SystemInfo-batteryStatus.html

Returns the current status of the device's battery (Read Only).

See the BatteryStatus enumeration for possible values.

The battery status includes information about whether the device is plugged in to a power source and whether the battery is charging. If battery status is not available on your target platform, this property returns BatteryStatus.Unknown.

https://docs.unity3d.com/ScriptReference/BatteryStatus.html

  • Unknown The device's battery status cannot be determined. If battery status is not available on your target platform, SystemInfo.batteryStatus will return this value.

  • Charging Device is plugged in and charging.

  • Discharging Device is unplugged and discharging.

  • NotCharging Device is plugged in, but is not charging.

  • Full Device is plugged in and the battery is full.

Upvotes: 6

Andrea
Andrea

Reputation: 6123

In Android, you can use SystemInfo.batteryStatus, checking for the value BatteryStatus.Charging. Look here for the other possible status: https://docs.unity3d.com/ScriptReference/BatteryStatus.html

Upvotes: 2

Related Questions