P.O.W.
P.O.W.

Reputation: 2215

Hololens 2 / MRTK / Unity / Asking for permissions programmatically

Is it possible to check if app has permissions and if don't ask for permissions programmatically on Hololens 2?

In manifest permissions are already declared by sometimes if you miss the permission popup or click on NO by mistake the app doesn't ask for permission never again.

Unity 2020.3.13f1

MRTK 2.7.3

- Update -

I asked for this feature here:

https://github.com/microsoft/MixedRealityToolkit-Unity/issues/10675

Please support the request!

Upvotes: 1

Views: 1136

Answers (2)

So the LaunchUriAsync() doesn't work for me on HoloLens 2 -- throws exception.

But here is my solution to checking for both Microphone and Camera permissions separately.

using UnityEngine;
using System.Threading.Tasks;

#if ENABLE_WINMD_SUPPORT
using Windows.Media.Capture;
using Windows.System;
using System;
#endif

public static class PermissionsManager
{
#if ENABLE_WINMD_SUPPORT
    public static async Task<bool> CheckMicrophonePermissions()
    {
        try
        {
            using (MediaCapture mediaCapture = new MediaCapture())
            {
                var settings = new MediaCaptureInitializationSettings();
                settings.StreamingCaptureMode = StreamingCaptureMode.Audio;
                await mediaCapture.InitializeAsync(settings);
            }
            return true; //--- Permissions are granted
        }
        catch (UnauthorizedAccessException)
        {
            return false;
        }
        catch (Exception ex)
        {
            return false;
        }
    }

    public static async Task<bool> CheckCameraPermissions()
    {
        try
        {
            using (MediaCapture mediaCapture = new MediaCapture())
            {
                var settings = new MediaCaptureInitializationSettings();
                settings.StreamingCaptureMode = StreamingCaptureMode.Video;
                await mediaCapture.InitializeAsync(settings);
            }
            return true; //--- Permissions are granted
        }
        catch (UnauthorizedAccessException)
        {
            return false;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
#endif
}

...and here's how I call it...

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Threading.Tasks;

public class MainManager : MonoBehaviour
{
    // Start is called before the first frame update
    private async void Start()
    {

#if ENABLE_WINMD_SUPPORT
        try
        {
            bool permissionsMicrophone = await CheckMicrophonePermissions();
            if (permissionsMicrophone)
            {
                Debug.Log("--- Microphone Permissions ON ---");
            }
            else
            {
                Debug.Log("--- Microphone Permissions OFF ---");
            }
        }
        catch (System.Exception ex)
        {
            Debug.Log("--- Microphone Permission Check Exception ---");
        }
#else
        Debug.Log("Microphone Permissions Checking Only in UWP.");
#endif

#if ENABLE_WINMD_SUPPORT
        try
        {
            bool permissionsCamera = await CheckCameraPermissions();
            if (permissionsCamera)
            {
                Debug.Log("--- Camera Permissions ON ---");
            }
            else
            {
                Debug.Log("--- Camera Permissions OFF ---");
            }
        }
        catch (System.Exception ex)
        {
            Debug.Log("--- Camera Permission Check Exception ---");
        }
#else
        Debug.Log("Camera Permissions Checking Only in UWP.");
#endif
    }

    // Update is called once per frame
    void Update()
    {
    }

#if ENABLE_WINMD_SUPPORT
    private async Task<bool> CheckMicrophonePermissions()
    {
        return await PermissionsManager.CheckMicrophonePermissions();
    }

    private async Task<bool> CheckCameraPermissions()
    {
        return await PermissionsManager.CheckCameraPermissions();
    }
#endif
}

Upvotes: 0

Jay Zuo
Jay Zuo

Reputation: 15758

To check Microphone and Camera permissions on HoloLens 2, we can take advantage of WinRT API MediaCapture. If the app does not have Microphone and Camera permissions, when calling the InitializeAsync method, it will throw a UnauthorizedAccessException.

If we got this exception, there is no way to re-ask for permission programmatically. We should let the user to grant us the required permissions again in Settings with using the ms-settings:appsfeatures-app URI, which can open the advanced settings page for our app.

#if ENABLE_WINMD_SUPPORT
using (MediaCapture mediaCapture = new MediaCapture())
    try
    {
        await mediaCapture.InitializeAsync();       
    }
    catch (UnauthorizedAccessException)
    {
        await Windows.System.Launcher.LaunchUriAsync(new Uri("ms-settings:appsfeatures-app"));
    }
#endif

Another way to check the permissions would be using AppCapability Class. CheckAccess method will return the access status. If the status is DeniedByUser, we will still need to open the advanced settings page for our app using the ms-settings:appsfeatures-app URI and let the user to grant us the required permissions.

Upvotes: 3

Related Questions