- Home /
How to get current battery life on mobile device
Hello. I have Unity4 pro ios pro. Is there a way to get the device's system properties including current battery life?
Answer by u3dxt · Jan 04, 2014 at 02:21 AM
No without a plugin, check out our answer here: http://answers.unity3d.com/questions/600278/how-to-read-battery-level-on-ios.html
Excerpt:
UIDevice device = UIDevice.CurrentDevice();
device.batteryMonitoringEnabled = true; // need to enable this first
Debug.Log("Battery state: " + device.batteryState);
Debug.Log("Battery level: " + device.batteryLevel);
If you want to be notified when the battery state or level changes, you can subscribe to device.BatteryStateDidChange and device.BatteryLevelDidChange events. Please see UIDevice API docs for more info.
I get a "`UIDevice' could not be found" error. What namespace does UIDevice reside in?
Answer by alok_androider · Feb 09, 2016 at 11:21 AM
public static float GetBatteryLevel()
{
#if UNITY_IOS
UIDevice device = UIDevice.CurrentDevice();
device.batteryMonitoringEnabled = true; // need to enable this first
Debug.Log("Battery state: " + device.batteryState);
Debug.Log("Battery level: " + device.batteryLevel);
return device.batteryLevel*100;
#elif UNITY_ANDROID
if (Application.platform == RuntimePlatform.Android)
{
try
{
using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
if (null != unityPlayer)
{
using (AndroidJavaObject currActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
{
if (null != currActivity)
{
using (AndroidJavaObject intentFilter = new AndroidJavaObject("android.content.IntentFilter", new object[]{ "android.intent.action.BATTERY_CHANGED" }))
{
using (AndroidJavaObject batteryIntent = currActivity.Call<AndroidJavaObject>("registerReceiver", new object[]{null,intentFilter}))
{
int level = batteryIntent.Call<int>("getIntExtra", new object[]{"level",-1});
int scale = batteryIntent.Call<int>("getIntExtra", new object[]{"scale",-1});
// Error checking that probably isn't needed but I added just in case.
if (level == -1 || scale == -1)
{
return 50f;
}
return ((float)level / (float)scale) * 100.0f;
}
}
}
}
}
}
} catch (System.Exception ex)
{
}
}
return 100;
#endif
}
Thank you very much mate. Works perfectly well out of the box on android.
Does anyone connected the UIDevice
class? I can't find what to include in order to get that working. Thanks.
Answer by SoylentGraham · Jan 25, 2015 at 10:32 PM
Sorry to revive an old thread, (this is the first google result) but here's an android solution[1] that doesn't require a plugin; I couldn't work out how to get the above to work, and using JNI was a bit of a pain.
int GetBatteryLevel()
{
try {
string CapacityString = System.IO.File.ReadAllText ("/sys/class/power_supply/battery/capacity");
return int.Parse (CapacityString);
} catch (Exception e) {
Debug.Log ("Failed to read battery power; " + e.Message);
}
return -1;
}
[1] Only tested on my Note4, and I believe the file location could change, and you can have multiple batteries etc
Your answer
![](https://koobas.hobune.stream/wayback/20220613085930im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to have my mobile device screen lighting on all the time 2 Answers
testing on an android device 1 Answer
Accelerometer - accounting for gravity for arbitrary device angle 1 Answer
testing on an android device 0 Answers
Mecanim for Mobile Devices 2 Answers