androidunity-game-engineazure-spatial-anchors

Spatial Anchor : await CreateAnchorAsync stays stuck


I'm trying to save an anchor to the Azure Spatial Anchor Cloud. This app is available on HoloLens (which is working) and now I'm trying to make it works on Android.

The problem
When I try to save the anchor, the code stays stuck on await _spatialAnchorManager.CreateAnchorAsync(cloudSpatialAnchor);

    private async Task CreateAnchor(Vector3 position)
    {
        //Create Anchor GameObject. We will use ASA to save the position and the rotation of this GameObject.
        if (!InputDevices.GetDeviceAtXRNode(XRNode.Head).TryGetFeatureValue(CommonUsages.devicePosition, out Vector3 headPosition))
        {
            headPosition = Vector3.zero;
        }

        Quaternion orientationTowardsHead = Quaternion.LookRotation(position - headPosition, Vector3.up);

        GameObject anchorGameObject = GameObject.Instantiate(_CubePrefab);

        anchorGameObject.GetComponent<MeshRenderer>().material.color = Color.white;
        anchorGameObject.transform.position = position;
        anchorGameObject.transform.rotation = orientationTowardsHead;
        anchorGameObject.transform.localScale = Vector3.one * 0.1f;

        //Add and configure ASA components
        CloudNativeAnchor cloudNativeAnchor = anchorGameObject.AddComponent<CloudNativeAnchor>();

        if (cloudNativeAnchor.CloudAnchor == null)
        {
            await cloudNativeAnchor.NativeToCloud();
        }

        CloudSpatialAnchor cloudAnchor = cloudNativeAnchor.CloudAnchor;

        // Then we create a new local cloud anchor
        CloudSpatialAnchor cloudSpatialAnchor = new CloudSpatialAnchor();
        // Now we set the local cloud anchor's position to the native XR anchor's position
        cloudSpatialAnchor.LocalAnchor = await anchorGameObject.FindNativeAnchor().GetPointer();
        // Check to see if we got the local XR anchor pointer
        if (cloudSpatialAnchor.LocalAnchor == IntPtr.Zero)
        {
            return;
        }
  
        //Collect Environment Data
        /*while (!_spatialAnchorManager.IsReadyForCreate) // debug 20.12.2022
        {
            float createProgress = _spatialAnchorManager.SessionStatus.RecommendedForCreateProgress;
            UnityEngine.Debug.Log($"ASA - Move your device to capture more environment data: {createProgress:0%}");
        }*/
        try
        {
            // Now that the cloud spatial anchor has been prepared, we can try the actual save here.
            await _spatialAnchorManager.CreateAnchorAsync(cloudSpatialAnchor);
            bool saveSucceeded = cloudSpatialAnchor != null;
            if (!saveSucceeded)
            {
                return;
            }
            _foundOrCreatedAnchorGameObjects.Add(anchorGameObject);
            _createdAnchorIDs.Add(cloudSpatialAnchor.Identifier);
            anchorGameObject.GetComponent<MeshRenderer>().material.color = Color.green;
        }
        catch (Exception exception)
        {
            UnityEngine.Debug.LogException(exception);
        }
    }

Previously I had a while loop in order to make sure SessionStatus.RecommendedForCreateProgress had the right value but it ended up blocking my app so I commented it. On the HoloLens I didn't have this issue so I could let the while loop. I don't think this is the reason why I can't save up the anchor

What I've tried
I tried to uncomment the while loop but then the app starts to freeze because it is stuck in it.

The question
How can I solve this issue where the code stays stuck on await _spatialAnchorManager.CreateAnchorAsync(cloudSpatialAnchor); and throws no errors, it just stays stuck


Solution

  • Answer is very simple, despite the fact that it works on HoloLens, on Android you must have SessionStatus.RecommendedForCreateProgress >= 1 if you want to be able to perform _spatialAnchorManager.CreateAnchorAsync(cloudSpatialAnchor);