Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Battery status level

Discussion in 'Android' started by _Tauti_, Sep 12, 2014.

  1. _Tauti_

    _Tauti_

    Joined:
    Jun 13, 2013
    Posts:
    8
    Hi. Please help me. How to read battery status on android ?
     
    Last edited: Nov 29, 2014
  2. _Tauti_

    _Tauti_

    Joined:
    Jun 13, 2013
    Posts:
    8
  3. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,353
    enriqueflores likes this.
  4. _Tauti_

    _Tauti_

    Joined:
    Jun 13, 2013
    Posts:
    8
  5. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,353
    Did you test it before replying?
     
  6. elias_t

    elias_t

    Joined:
    Sep 17, 2010
    Posts:
    1,366
    How can he test iOS stuff on android?
     
  7. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,353
    I meant did he test the code on android?
     
  8. alok_androider

    alok_androider

    Joined:
    Apr 26, 2013
    Posts:
    4
    Code (CSharp):
    1.       public static float GetBatteryLevel()
    2.         {
    3.             #if UNITY_IOS
    4.             UIDevice device = UIDevice.CurrentDevice();
    5.             device.batteryMonitoringEnabled = true; // need to enable this first
    6.             Debug.Log("Battery state: " + device.batteryState);
    7.             Debug.Log("Battery level: " + device.batteryLevel);
    8.             return device.batteryLevel*100;
    9.             #elif UNITY_ANDROID
    10.    
    11.             if (Application.platform == RuntimePlatform.Android)
    12.             {
    13.                 try
    14.                 {
    15.                     using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
    16.                     {
    17.                         if (null != unityPlayer)
    18.                         {
    19.                             using (AndroidJavaObject currActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
    20.                             {
    21.                                 if (null != currActivity)
    22.                                 {
    23.                                     using (AndroidJavaObject intentFilter = new AndroidJavaObject("android.content.IntentFilter", new object[]{ "android.intent.action.BATTERY_CHANGED" }))
    24.                                     {
    25.                                         using (AndroidJavaObject batteryIntent = currActivity.Call<AndroidJavaObject>("registerReceiver", new object[]{null,intentFilter}))
    26.                                         {
    27.                                             int level = batteryIntent.Call<int>("getIntExtra", new object[]{"level",-1});
    28.                                             int scale = batteryIntent.Call<int>("getIntExtra", new object[]{"scale",-1});
    29.    
    30.                                             // Error checking that probably isn't needed but I added just in case.
    31.                                             if (level == -1 || scale == -1)
    32.                                             {
    33.                                                 return 50f;
    34.                                             }
    35.                                             return ((float)level / (float)scale) * 100.0f;
    36.                                         }
    37.                                    
    38.                                     }
    39.                                 }
    40.                             }
    41.                         }
    42.                     }
    43.                 } catch (System.Exception ex)
    44.                 {
    45.                  
    46.                 }
    47.             }
    48.          
    49.             return 100;
    50.             #endif
    51.         }
    52.  
     
    Zeldarck, elias_t and _Tauti_ like this.
  9. Lohoris2

    Lohoris2

    Joined:
    Aug 20, 2013
    Posts:
    85
    I take you were trolling?
    Obviously those libs aren't even there…
     
  10. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,353
    Alright, 5 years later we finally got a concrete solution.

    Since Unity 2018.1, you can now get battery level by calling SystemInfo.batteryLevel
     
    LuckyMisterSleven and Lohoris2 like this.
  11. saadaamir3047

    saadaamir3047

    Joined:
    Sep 12, 2021
    Posts:
    4
    This is the function I used to get the complete status of the Battery:

    Code (CSharp):
    1. public void UpdateBatteryStatus()
    2.         {
    3.             if(SystemInfo.batteryStatus == BatteryStatus.Charging)
    4.             {
    5.                 //Do Something
    6.             }
    7.             else if(SystemInfo.batteryStatus == BatteryStatus.Full || SystemInfo.batteryLevel >= 0.9f)
    8.             {
    9.                 //Do Something
    10.             }
    11.             else if (SystemInfo.batteryLevel >= 0.61f && SystemInfo.batteryLevel < 0.9f)
    12.             {
    13.                 //Do Something
    14.             }
    15.             else if (SystemInfo.batteryLevel >= 0.51f && SystemInfo.batteryLevel < 0.61f)
    16.             {
    17.                 //Do Something
    18.             }
    19.             else if (SystemInfo.batteryLevel >= 0.31f && SystemInfo.batteryLevel < 0.51f)
    20.             {
    21.                 //Do Something
    22.             }
    23.             else if (SystemInfo.batteryLevel >= 0.11f && SystemInfo.batteryLevel < 0.31f)
    24.             {
    25.                 //Do Something
    26.             }
    27.             else if (SystemInfo.batteryLevel < 0.11f)
    28.             {
    29.                 //Do Something
    30.             }
    31.         }