Display battery percentage in Android

Hey devs! I would like to display the android phone’s current battery percentage in my game. How would I do that? I looked up several other topics but none of their solutions seem to be working for me.

Someone posted this script for this purpose.

public float GetBatteryLevel()
{

	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;

but when I attach this to my “public Text bat_lvl;” as “bat_lvl.text = GetBatteryLevel();” I get this error: Cannot implicitly convert type float' to string’. I know what this error is and why it’s here but how do I display a float value to a UI Text is what I’m struggling at.

Any help would be greatly appreciated!

Solved it using

int GetBatteryLevel()
	{
		try {
			string CapacityString = System.IO.File.ReadAllText ("/sys/class/power_supply/battery/capacity");
			return int.Parse (CapacityString);
		} catch (System.Exception e) {
			Debug.Log ("Failed to read battery power; " + e.Message);
		}

		return -1; //if no value is found
	}

try SystemInfo.batteryLevel