Run the sample app: Android - Android Studio (Java or C++/NDK)
Članak
This quickstart covers how to run the Azure Spatial Anchors sample app for Android devices using Android Studio (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
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 and brew install git-lfs.
To build the NDK sample, you'll also need to install the NDK and CMake 3.6 or greater SDK Tools in Android Studio.
Additional device drivers may be required for your computer to communicate with your Android device. See here for additional information and instructions.
On the Spatial Anchors Account pane, do the following:
Enter a unique resource name by using regular alphanumeric characters.
Select the subscription that you want to attach the resource to.
Create a resource group by selecting Create new. Name it myResourceGroup, and then select OK.
A resource group is a logical container into which Azure resources, such as 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.
Select a location (region) in which to place the resource.
Select Create to begin creating the resource.
After the resource is created, the Azure portal shows that your deployment is complete.
Select Go to resource. You can now view the resource properties.
Copy the resource's Account ID value into a text editor for later use.
Also copy the resource's Account Domain value into a text editor for later use.
Under Settings, select Access Key. Copy the Primary key value, Account Key, into a text editor for later use.
Start by preparing your environment for the Azure CLI:
If you prefer to run CLI reference commands locally, install the Azure CLI. If you're running on Windows or macOS, consider running Azure CLI in a Docker container. For more information, see How to run the Azure CLI in a Docker container.
If you're using a local installation, sign in to the Azure CLI by using the az login command. To finish the authentication process, follow the steps displayed in your terminal. For other sign-in options, see Sign in with the Azure CLI.
When you're prompted, install the Azure CLI extension on first use. For more information about extensions, see Use extensions with the Azure CLI.
Run az version to find the version and dependent libraries that are installed. To upgrade to the latest version, run az upgrade.
After you sign in, use the az account set command to select the subscription in which to set up the spatial anchors account:
az account set --subscription 00000000-0000-0000-0000-000000000000
Run the az group create command to create a resource group, or use an existing resource group:
az group create --name myResourceGroup --location eastus2
A resource group is a logical container into which Azure resources, such as 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.
You can view your current spatial anchors accounts for a resource group by using the az spatial-anchors-account list command:
az spatial-anchors-account list --resource-group myResourceGroup
You can also view the spatial anchors accounts for your subscription:
az spatial-anchors-account key renew --resource-group myResourceGroup --name example --key primary
az spatial-anchors-account key renew --resource-group myResourceGroup --name example --key secondary
While the Az.MixedReality PowerShell module is in preview, you must install it separately using
the Install-Module cmdlet. After this PowerShell module becomes generally available, it will be
part of future Az PowerShell module releases and available by default from within Azure Cloud
Shell.
Install-Module -Name Az.MixedReality
After you sign in, use the Set-AzContext cmdlet to select the subscription in which to set up the spatial anchors account:
A resource group is a logical container into which Azure resources, such as 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.
You can view your current spatial anchors accounts for a resource group by using the Get-AzSpatialAnchorsAccount cmdlet:
Select Open an existing Android Studio project and select the project located at Android/Java/.
Select Open an existing Android Studio project and select the project located at Android/NDK/.
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/AzureSpatialAnchorsManager.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.
Locate the SpatialAnchorsAccountDomain field and replace Set me with the account domain.
Open Android/NDK/app/src/main/cpp/AzureSpatialAnchorsApplication.cpp.
Locate the SpatialAnchorsAccountKey field and replace Set me with the account key.
Locate the SpatialAnchorsAccountId field and replace Set me with the account identifier.
Locate the SpatialAnchorsAccountDomain field and replace Set me with the account domain.
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.
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.
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 Azure portal menu or Home page, select Resource groups. Then, on the Resource groups page, select myResourceGroup.
On the myResourceGroup page, make sure that the listed resources are the ones you want to delete.
Select Delete resource group, type myResourceGroup in the text box to confirm, and then select Delete.
Next steps
In this quickstart, you 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 it can share spatial anchors with other devices, continue to the next tutorial.