• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Next Beat Games · Dec 26, 2012 at 03:11 AM · androidiosmobileiphonebattery

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cecarlsen · Sep 22, 2014 at 08:31 AM 0
Share

I get a "`UIDevice' could not be found" error. What namespace does UIDevice reside in?

avatar image
1

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
4

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
     }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nokhal · Feb 10, 2016 at 01:14 AM 0
Share

Thank you very much mate. Works perfectly well out of the box on android.

avatar image jemonsuarez · Jun 22, 2016 at 10:23 AM 0
Share

Does anyone connected the UIDevice class? I can't find what to include in order to get that working. Thanks.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

size of GUI pics too big 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges