Quickstart: Create an Android app with Azure Spatial Anchors

This quickstart covers how to create an Android app using Azure Spatial Anchors in either Java or C++/NDK. Azure Spatial Anchors is a cross-platform developer service that allows you to create mixed reality experiences using objects that persist their location across devices over time. When you're finished, you'll have an ARCore Android app that can save and recall a spatial anchor.

You'll learn how to:

  • Create a Spatial Anchors account
  • Configure the Spatial Anchors account identifier and account key
  • Deploy and run on an Android device

If you don't have an Azure subscription, create a free account before you begin.

Prerequisites

To complete this quickstart, make sure you have:

  • A Windows or macOS machine with Android Studio 3.3+.
    • If running on Windows, you'll also need Git for Windows.
    • If running on macOS, get Git installed via HomeBrew. Enter the following command into a single line of the Terminal: /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)". Then, run brew install git.
    • To build the NDK sample, you'll also need to install the NDK and CMake 3.6 SDK Tools in Android Studio.
  • A developer enabled and ARCore capable Android device.
  • Your app must target ARCore 1.5 (support for ARCore 1.6+ will be coming soon)

Create a Spatial Anchors resource

  1. Navigate to the Azure portal.

  2. From the left menu in the Azure portal, select Create a resource.

  3. Search for "Spatial Anchors" in the search bar.

    Search for Spatial Anchors

  4. Select Spatial Anchors to open a dialog and select Create.

  5. In the Spatial Anchors Account form:

    1. Specify a unique resource name.
    2. Select the subscription to attach the resource to.
    3. Create a resource group by selecting Create new and name the resource group myResourceGroup and select OK.

      A resource group is a logical container into which Azure resources like web apps, databases, and storage accounts are deployed and managed. For example, you can choose to delete the entire resource group in one simple step later.

    4. Select a location (region) where the resource will be placed.
    5. Select New to begin creating the resource.

    Create a resource

  6. After the resource creation has completed successfully, the resource properties can be viewed. Copy the resource's Account ID value into a text editor, as it will be required later.

    View resource properties

  7. Under Settings, select Key, and copy into a text editor the Primary key value. This value is the Account Key and will be used later.

    View account key

Open the sample project

Clone the samples repository by running the following command:

git clone https://github.com/Azure/azure-spatial-anchors-samples.git

If you're building the Android NDK sample, you'll need to download arcore_c_api.h from here and place it in Android\NDK\libraries\include.

Open Android Studio.

Select Open an existing Android Studio project and select the project located at Android/Java/.

Configure account identifier and key

The next step is to configure the app to use your account identifier and account key. You copied them into a text editor when setting up the Spatial Anchors resource.

Open Android/Java/app/src/main/java/com/microsoft/sampleandroid/AzureSpatialAnchorsActivity.java.

Locate the SpatialAnchorsAccountKey field and replace Set me with the account key.

Locate the SpatialAnchorsAccountId field and replace Set me with the account identifier.

Deploy the app to your Android device

Power on the Android device, sign in, and connect it to the PC using a USB cable.

Select Run from the Android Studio toolbar.

Android Studio Deploy and Run

Select the Android device in the Select Deployment Target dialog, and select OK to run the app on the Android device.

Follow the instructions in the app to place and recall an anchor.

Stop the app by selecting Stop from the Android Studio toolbar.

Android Studio Stop

Clean up resources

In the preceding steps, you created Azure resources in a resource group. If you don't expect to need these resources in the future, you can delete them by deleting the resource group.

From the left menu in the Azure portal, select Resource groups and then select myResourceGroup.

On the resource group page, make sure that the listed resources are the ones you want to delete.

Select Delete, type myResourceGroup in the text box, and then select Delete.

Next steps

In this quickstart, you've created a Spatial Anchors account. You then configured and deployed an App to save and recall Spatial Anchors. To learn more about how to improve the App so that it can share Spatial Anchors with other devices, continue to the next tutorial.