Tango Permissions and Lifecycle in Unity

Tango devices work by using visual cues from the device's cameras to track location and learn areas. Users may be concerned about their privacy, so you must ask users for permission prior to using the camera for motion tracking or accessing/saving area description files. Tango will not work without the user's permission in these cases.

In Unity, permission handling and the application lifecycle are tightly connected. For many applications, they will be handled for you.

Android Camera permission

Applications that intend to use depth perception or show the camera preview must request the Android Camera permission in their App Manifest file to get access to the camera feed through the Tango API. To do this, add the line

<uses-permission android:name="android.permission.CAMERA" />

to your AndroidManifest file located at Assets/Plugins/Android/AndroidManifest.xml.

The AndroidManifest bundled with the Tango SDK comes with this permission enabled by default.

Startup flow

At a high level, the Tango startup flow for Unity is:

  • Enable Tango features in Tango Manager
  • Request permissions using TangoApplication.RequestPermissions()
  • (optional) Perform any additional pre-connection steps
  • Connect to Tango using TangoApplication.Startup()

The Unity SDK also provides an Auto-connect to Service option, which handles the permission request and connect calls for you. You cannot use auto-connect if you have any pre-connection steps.

Enable Tango features

The Tango Application script of the Tango Manager prefab contains options to enable the different Tango features. Make sure you have enabled all the options you are using, as this controls permissions and startup. You can do this in the editor using the Inspector tab, or by setting the appropriate attributes on the TangoApplication script.

Using the Auto-connect option

If you have Auto-connect to Service enabled in the Tango Manager, the lifecycle and permission requests are handled for you. When Tango Manager starts (usually at scene start), it will request all necessary permissions and start the service if permissions are granted. When the Tango Manager gets destroyed (usually at scene end), the service will disconnect automatically. If you are not using area descriptions, you can usually use auto-connect.

Manually request permissions and connect (Area Learning)

You can also implement the permissions request and connect calls yourself. This is usually necessary when loading area descriptions. You need to get the list of available area descriptions to pick one to load before connecting to Project Tango. However, to get the list of area descriptions, you must first request permissions. Because we must do an extra step between RequestPermissions() and Startup(), we cannot use auto-connect.

To request permissions, use the TangoApplication.RequestPermissions() method. A common place to do this is in a Start function:

public void Start()
{
    TangoApplication tangoApplication = FindObjectOfType<TangoApplication>();

    if (tangoApplication != null)
    {
        tangoApplication.Register(this);
        tangoApplication.RequestPermissions();
    }
}

Once all requested permissions are accepted or denied, the ITangoLifecycle callback OnTangoPermissions() will get called. If permissions are granted, you can now connect to Tango with TangoApplication.Startup():

public void OnTangoPermissions(bool permissionsGranted)
{
    TangoApplication tangoApplication = FindObjectOfType<TangoApplication>();
    if (permissionsGranted)
    {
        tangoApplication.Startup(null);
    }
}

As mentioned earlier, when loading area descriptions you will typically get the list of available area descriptions and pick one to load. You must do this before calling TangoApplication.Startup() so you can pass in the area description. In the example below, we find the most recent area description. More typically, you would show a list of area descriptions to the user and have them pick one to load.

public void OnTangoPermissions(bool permissionsGranted)
{
    if (permissionsGranted)
    {
        AreaDescription[] list = AreaDescription.GetList();
        AreaDescription mostRecent = null;
        AreaDescription.Metadata mostRecentMetadata = null;
        if (list.Length > 0)
        {
            // Find and load the most recent Area Description
            mostRecent = list[0];
            mostRecentMetadata = mostRecent.GetMetadata();
            foreach (AreaDescription areaDescription in list)
            {
                AreaDescription.Metadata metadata = areaDescription.GetMetadata();
                if (metadata.m_dateTime > mostRecentMetadata.m_dateTime)
                {
                    mostRecent = areaDescription;
                    mostRecentMetadata = metadata;
                }
            }

            m_tangoApplication.Startup(mostRecent);
        }
        else
        {
            // No Area Descriptions available.
            Debug.Log("No area descriptions available.");
        }
    }
}

Detecting connects and disconnects

Typically, you will connect to Tango when starting your application and disconnect when the user quits. You may also be connected/disconnected for other reasons, such as the OS suspending or resuming your app. There are two more callbacks in ITangoLifecycle for when these connect/disconnect events occur:

/// <summary>
/// This is called when succesfully connected to the Tango service.
/// </summary>
public void OnTangoServiceConnected()
{
}

/// <summary>
/// This is called when disconnected from the Tango service.
/// </summary>
public void OnTangoServiceDisconnected()
{
}

For example, you could use OnTangoServiceConnected() to run initialization code like turning off the depth camera or getting camera intrinsics, or use OnTangoServiceDisconnected() to pause the parts of your application that use Tango.

Enviar comentarios sobre…