How to check the remaining battery level of your controller

Where can I find the battery level on my Magic Leap 2 controller?
I could not find it on the following page.

Thanks.
Sadao Tokuyama

We use a sticky intent similar to the android intent.action.BATTERY_CHANGED intent which apps can use to get updates regarding the compute pack battery level and status. Documentation on using the system intent can be found here: Monitor the Battery Level and Charging State | Android Developers

The controller battery intent is named com.magicleap.controller.action.BATTERY_CHANGED. The extras attached to the intent will follow what the system battery intent uses, so that code that processes the system battery intent can be used as the basis for code to process the controller battery intent. The following extras are populated in the controller battery intent:

  • If the connection to the controller is active, the following extras will be set:
    • BatteryManager.EXTRA_PRESENT : true
    • BatteryManager.EXTRA_LEVEL : current controller battery level (0-100)
    • BatteryManager.EXTRA_SCALE : 100
    • BatteryManager.EXTRA_BATTERY_LOW : true if current level is 25% or lower, otherwise false
    • BatteryManager.EXTRA_STATUS : BatteryManager.BATTERY_STATUS_CHARGING or BatteryManager.BATTERY_STATUS_NOT_CHARGING, based on controller charger connection state
  • If the connection to the controller is not active, the following extras will be set:
    • BatteryManager.EXTRA_PRESENT : false
    • BatteryManager.EXTRA_STATUS : BatteryManager.BATTERY_STATUS_UNKNOWN

There is no way to see the battery level from the outside, such as by lighting LEDs, as is the case with compute packs.

Once the controller is paired with the ML2 headset, the controller's battery level is visible in the main menu.

I am asking about how to check the Controller's battery level when the Magic Leap 2 is not running.

@cdorff One question arose about the Controller's battery status reply in the app.
It seems to be using the Android Java final variable, but if I want to get the Controller's battery status, do I need to create a native Java library to get the Controller's battery status and import it into Unity?

Or can the program to get the Controller's battery status be completed by just developing Unity?

This should work in Unity:

https://docs.unity3d.com/ScriptReference/SystemInfo-batteryLevel.html

Isn't this only possible to get the battery level of the Compute Pack?

In the beginning you explained how the Controller gets it from the Controller's intent for the battery, right?

Is there a sample program that has been tested to get the Controller's battery information?

public static int GetControllerBatteryState()
    {
#if PLATFORM_ANDROID && !UNITY_EDITOR
      using var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
      var activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
      if (activity == null)
      {
        Utils.LogError("Activity is null");
        return -1;
      }
      using var filter = new AndroidJavaObject("android.content.IntentFilter");
      if (filter == null)
      {
        Utils.LogError("IntentFilter is null");
        return -1;
      }
      filter.Call("addAction", "com.magicleap.controller.action.BATTERY_CHANGED");
      using var intent = activity.Call<AndroidJavaObject>("registerReceiver", null, filter);
      if (intent == null)
      {
        Utils.LogError("Intent is null");
        return -1;
      }
      using var batteryManager = new AndroidJavaClass("android.os.BatteryManager");
      if (batteryManager == null)
      {
        Utils.LogError("BatteryManager is null");
        return -1;
      }
      var EXTRA_PRESENT = batteryManager.GetStatic<string>("EXTRA_PRESENT");
      var EXTRA_LEVEL = batteryManager.GetStatic<string>("EXTRA_LEVEL");
      var isPresent = intent.Call<bool>("getBooleanExtra", EXTRA_PRESENT, false);
      return isPresent ? intent.Call<int>("getIntExtra", EXTRA_LEVEL, -1) : -1;
#else
    return -1;
#endif
    }

Thank you for presenting the sample code so quickly. I can't try it because I don't have a Magic Leap 2 at hand, but I will check it when I can get a Magic Leap 2.