I have developed an Ar App, I am using hostCloudAnchor() and resolveCloudAnchor() methods to deal with cloud anchors in my application. Now Google has mentiond that we must migrate to new ArCoreAPI could anchor enpoint before this september,2023.
The older ARCore Cloud Anchor API cloud endpoint has been deprecated and will not be supported after August 31, 2023. If your app is using this API, you must update it to use the new ARCore API cloud endpoint as soon as possible.
So, I am planning to move new ArCoreAPI and use hostCloudAnchorAsync() method and resolveCloudAnchorAsync() methods, but couldn’t clearly understand use of these methods, and couldn’t find latest Cloud Anchor examples.
This is how i am creating cloud anchors in the meantime with depricated methods.
public class CloudAnchorActivity extends AppCompatActivity implements
BaseArFragment.OnSessionConfigurationListener {
private ArFragment anchorArFragment;
private enum AppCloudAnchorState{
NONE,
HOSTING,
HOSTED
}
private AppCloudAnchorState appCloudAnchorState = AppCloudAnchorState.NONE;
private Anchor anchor;
private boolean isPlaced;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_cloud_anchor);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
anchorArFragment = (ArFragment) getSupportFragmentManager().findFragmentById(R.id.cloudAnchorArFragment);
anchorArFragment.setOnSessionConfigurationListener(this);
anchorArFragment.setOnTapArPlaneListener((hitResult, plane, motionEvent) -> {
if (!isPlaced) {
anchor = anchorArFragment.getArSceneView().getSession().hostCloudAnchor(hitResult.createAnchor());
appCloudAnchorState = AppCloudAnchorState.HOSTING;
Toast.makeText(this, "Hosting Cloud Anchor...", Toast.LENGTH_SHORT).show();
createModel(anchor);
isPlaced = true;
}
});
anchorArFragment.getArSceneView().getScene().addOnUpdateListener(frameTime -> {
if (appCloudAnchorState != AppCloudAnchorState.HOSTING)
return;
Anchor.CloudAnchorState cloudAnchorState = anchor.getCloudAnchorState();
if (cloudAnchorState.isError()){
Toast.makeText(this, cloudAnchorState.toString(), Toast.LENGTH_SHORT).show();
} else if (cloudAnchorState == Anchor.CloudAnchorState.SUCCESS) {
appCloudAnchorState = AppCloudAnchorState.HOSTED;
String cloudAnchorId = anchor.getCloudAnchorId();
Toast.makeText(this, "Anchor Hosted Successfully, Anchor Id: "+cloudAnchorId, Toast.LENGTH_SHORT).show();
}
});
}
private void createModel(Anchor anchor) {
ModelRenderable.builder()
.setSource(this, R.raw.andy)
.setIsFilamentGltf(true)
.setAsyncLoadEnabled(true)
.build()
.thenAccept(modelRenderable -> placeModel(anchor,modelRenderable))
.exceptionally(throwable -> {
Toast.makeText(
this, "Unable to load model Andy", Toast.LENGTH_LONG).show();
return null;
});
}
private void placeModel(Anchor anchor, ModelRenderable modelRenderable) {
AnchorNode anchorNode = new AnchorNode(anchor);
anchorNode.setRenderable(modelRenderable);
anchorArFragment.getArSceneView().getScene().addChild(anchorNode);
}
@Override
public void onSessionConfiguration(Session session, Config config) {
config.setCloudAnchorMode(Config.CloudAnchorMode.ENABLED);
}
}