Search Unity

Display Battery Level

Discussion in 'Android' started by Crazy Robot, Sep 3, 2012.

  1. Crazy Robot

    Crazy Robot

    Joined:
    Apr 18, 2009
    Posts:
    921
    Hello,

    Is there anyway to display the battery level in Unity?

    Thanks,

    JL
     
  2. RazorCut

    RazorCut

    Joined:
    May 7, 2009
    Posts:
    393
    You'd need to make a plugin.
     
  3. Crazy Robot

    Crazy Robot

    Joined:
    Apr 18, 2009
    Posts:
    921
    Thanks,

    I've been trying to figure out how to make a plugin for Android and I'm lost. But I'm still trying! LOL
     
  4. 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.     }