Skip to main content
Version: 20 Mar 2024

Requesting Permissions

Permissions in Magic Leap applications can be categorized under two classes: Normal permissions (also referred to as Install-time permissions) and Dangerous permissions (also known as Runtime permissions). These classifications are analogous with the Android permission system.

The Magic Leap SDK includes helper functions designed to streamline the permission request process and verification for Magic Leap-specific functionalities. You can access these functions via the MLPermissions API. It's recommended to use these functions to verify permissions before activating any restricted feature to avoid runtime errors.

tip

You can view a list of Magic Leap permissions and their corresponding security level by navigating to Edit > Project Settings... and then selecting MagicLeap > Permissions from the sidebar.

MagicLeap Permissions

Requesting Normal (Install-time) Permissions

Normal permissions, also known as Install-time permissions, are granted automatically upon installation if declared in your app's AndroidManifest.xml. These permissions typically provide access to isolated, non-sensitive user data or device functionalities, such as com.magicleap.permission.HAND_TRACKING. Consequently if the permission is not included in your apps AndroidManifest.xml, they are implicitly denied.

The example below verifies if the HAND_TRACKING permission is declared in the Manifest, and disables its functionality if the permission wasn't declared.

void Start()
{
if(MLPermissions.CheckPermission(MLPermission.HandTracking).IsOk)
{
// continue as planned
}
else
{
this.enabled = false;
}
}

Note that the function CheckPermission() will return:

  • MLResult.Code.Ok if the permission has been granted.
  • MLResult.Code.PermissionDenied if the permission has been denied.
  • MLResult.Code.InvalidParam if the input string is null or empty.

Requesting Dangerous (Runtime) Permissions

Dangerous permissions need to be declared in the AndroidManifest.xml and also require to be explicitly requested at runtime. Since these permissions offer access to potentially sensitive data, users can decide whether they want to allow these permissions or not. If the permission is denied, your application should adapt its behavior according.

Note that if a Dangerous permission is not included in the AndroidManifest.xml, your application won't have the ability to request this permission at runtime.

Magic Leap Permissions

The Magic Leap SDK includes a helper class to request permissions without creating a PermissionsCallbacks object. Instead Developers can use the API and bass in the call back action directly if the choose. All of the callbacks are optional.

Permissions.RequestPermission(MLPermission.EyeTracking, OnPermissionGranted, OnPermissionDenied, OnPermissionDeniedDontAskAgain);

To use the Magic Leap API, use the following namespace:

using MagicLeap.Android;

Use the simple example below as a reference from requesting permissions:


using MagicLeap.Android;
using UnityEngine;
using UnityEngine.XR.MagicLeap;

public class PermissionsTest : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
//Request a single Permission
Permissions.RequestPermission(MLPermission.EyeTracking, OnPermissionGranted, OnPermissionDenied, OnPermissionDeniedDontAskAgain);

//Request Multiple permissions
Permissions.RequestPermissions(new []{MLPermission.DepthCamera, MLPermission.RecordAudio}, OnPermissionGranted, OnPermissionDenied, OnPermissionDeniedDontAskAgain);

//Check Permission
bool permissionGranted = Permissions.CheckPermission(MLPermission.EyeTracking);
// ..
}

private void OnPermissionGranted(string obj)
{
// ..
}

private void OnPermissionDenied(string obj)
{
// ..
}

private void OnPermissionDeniedDontAskAgain(string obj)
{
// ..
}
}

Under the hood this uses the generic Android Permissions API from Unity as described in further detail in the Android permissions guide.

  namespace MagicLeap.Android
{
public static class Permissions
{
public static void RequestPermission(string permission,
Action<string> onPermissionGranted = null,
Action<string> onPermissionDenied = null,
Action<string> onPermissionDeniedDontAskAgain = null)
{
RequestPermissions(new string[] { permission }, onPermissionGranted, onPermissionDenied, onPermissionDeniedDontAskAgain);
}

public static void RequestPermissions(string[] permissions,
Action<string> onPermissionGranted = null,
Action<string> onPermissionDenied = null,
Action<string> onPermissionDeniedDontAskAgain = null)
{
var callbacks = new PermissionCallbacks();
callbacks.PermissionGranted += onPermissionGranted;
callbacks.PermissionDenied += onPermissionDenied;
callbacks.PermissionDeniedAndDontAskAgain += onPermissionDeniedDontAskAgain;
Permission.RequestUserPermissions(permissions, callbacks);
}

public static bool CheckPermission(string permission) => Permission.HasUserAuthorizedPermission(permission);
}
}
tip

We recommend requesting all permissions for the application in a single script. Performing concurrent request on the same frame may cause the request to be ignored and prevent the application from requesting the specified permissions.