Skip to main content
Version: 14 Oct 2024

Requesting Permissions

The Magic Leap SDK includes a helper MagicLeap.Android.Permissions API 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. Under the hood this uses the generic Android Permissions API from Unity as described in further detail in the Android permissions guide.

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

Permission Categories

Permissions in Magic Leap applications are similar to the Android permission system, categorized under Normal (Install-time) and Dangerous (Runtime) permissions.

  • Normal permissions are granted automatically upon installation if declared in your app's AndroidManifest.xml.
  • Dangerous permissions must be declared in the AndroidManifest.xml and requested at runtime.
tip

To view and manage Magic Leap-specific permissions, go to Edit > Project Settings... and select MagicLeap > Permissions in Unity.

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. Consequently if the permission is not included in your apps AndroidManifest.xml, they are implicitly denied.

You can verify if a normal permission was declared using the MagicLeap.Android.Permissions.CheckPermission() API. 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 (MagicLeap.Android.Permissions.CheckPermission(MLPermission.HandTracking))
{
// continue as planned
}
else
{
this.enabled = false;
}
}

Requesting Dangerous (Runtime) Permissions

Dangerous permissions need to be declared in the AndroidManifest.xml and 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.

caution

If a Dangerous permission is not declared in the AndroidManifest.xml, your application won't have the ability to request this permission at runtime.

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.

Request Single Dangerous Permission

This example shows how to request a single Dangerous permission. If multiple permissions are required, it is recomended to use MagicLeap.Android.Permissions.RequestPermissions(string[] permissions) instead to ensure that multiple permissions are not requested during the same frame.


// Start is called before the first frame update
void Start()
{
MagicLeap.Android.Permissions.RequestPermission(
MLPermission.EyeTracking,
OnPermissionGranted, OnPermissionDenied, OnPermissionDeniedDontAskAgain);
}
private void OnPermissionGranted(string permission)
{
// Permission Granted
}

private void OnPermissionDenied(string permission)
{
// Permission Denied
}

private void OnPermissionDeniedDontAskAgain(string permission)
{
// Permission Denied Dont Ask Again
}

Request Multiple Dangerous Permissions

The following example shows how to request multiple permissions at a time. This ensures that each permission request is made without conflicting request calls.

    void Start()
{
// The permissions to request
string[] permissions = new string[]
{ MLPermission.Camera, MLPermission.EyeTracking, MLPermission.FacialExpression };

MagicLeap.Android.Permissions.RequestPermissions(
permissions,
OnPermissionGranted, OnPermissionDenied, OnPermissionDeniedDontAskAgain);
}
private void OnPermissionGranted(string permission)
{
Debug.Log($"{permission} was granted.");
}

private void OnPermissionDenied(string permission)
{
Debug.Log($"{permission} was denied.");
}

private void OnPermissionDeniedDontAskAgain(string permission)
{
Debug.Log($"{permission} was denied and cannot be request again.");
}