Reputation: 47
This is my main code
package com.example.ar;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.widget.Toast;
import com.google.ar.core.Anchor;
import com.google.ar.core.HitResult;
import com.google.ar.core.Plane;
import com.google.ar.sceneform.AnchorNode;
import com.google.ar.sceneform.rendering.ModelRenderable;
import com.google.ar.sceneform.ux.ArFragment;
import com.google.ar.sceneform.ux.TransformableNode;
public class MainActivity extends AppCompatActivity
{
private static final String TAG = MainActivity.class.getSimpleName();
private static final double MIN_OPENGL_VERSION = 3.0;
ArFragment arFragment;
ModelRenderable lampPostRenderable;
@Override
@SuppressWarnings({"AndroidApiChecker", "FutureReturnValueIgnored"})
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!checkIsSupportedDeviceOrFinish(this))
{
return;
}
setContentView(R.layout.activity_main);
arFragment = (ArFragment) getSupportFragmentManager().findFragmentById(R.id.ux_fragment);
ModelRenderable.builder().setSource(this, Uri.parse("lamppost.sfb")).build()
.thenAccept(renderable -> lampPostRenderable = renderable)
.exceptionally(throwable ->
{
Toast toast = Toast.makeText(this, "Unable to load andy renderable",
Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
return null;
}
);
arFragment.setOnTapArPlaneListener((HitResult hitresult, Plane plane, MotionEvent motionevent) -> {
if (lampPostRenderable == null){
return; }
Anchor anchor = hitresult.createAnchor();
AnchorNode anchorNode = new AnchorNode(anchor);
anchorNode.setParent(arFragment.getArSceneView().getScene());
TransformableNode lamp = new TransformableNode(arFragment.getTransformationSystem());
lamp.setParent(anchorNode);
lamp.setRenderable(lampPostRenderable);
lamp.select();
});
}
public static boolean checkIsSupportedDeviceOrFinish(final Activity activity)
{
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N)
{
Log.e(TAG, "Sceneform requires Android N or later");
Toast.makeText(activity, "Sceneform requires Android N or later", Toast.LENGTH_LONG).show();
activity.finish();
return false;
}
String openGlVersionString =
((ActivityManager) activity.getSystemService(Context.ACTIVITY_SERVICE))
.getDeviceConfigurationInfo().getGlEsVersion();
if (Double.parseDouble(openGlVersionString) < MIN_OPENGL_VERSION)
{
Log.e(TAG, "Sceneform requires OpenGL ES 3.0 later");
Toast.makeText(activity, "Sceneform requires OpenGL ES 3.0 or later", Toast.LENGTH_LONG).show();
activity.finish();
return false;
}
return true;
}
}
And I've also included
<meta-data
android:name="com.google.ar.core"
android:value="required" />
in the Android manifest file.
Gradle module:app has
implementation 'com.google.ar.sceneform.ux:sceneform-ux:1.15.0'
and gradle project has
classpath 'com.google.ar.sceneform:plugin:1.5.0'
in dependencies.
My sampledata folder has an obj and a mtl file with the name lamppost.
On opening the app the error is displayed as "Failed to create AR session" with a black screen and an animation of a hand holding a device rotating in circles here's how it looks like.
Upvotes: 4
Views: 6022
Reputation: 71
In my case I forgot to include the following code in manifest file
<meta-data
android:name="com.google.ar.core"
android:value="required" />
Here's my complete manifest file
<?xml version="1.0" encoding="utf-8"?>
<uses-feature
android:name="android.hardware.camera"
android:required="true" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-feature
android:name="android.hardware.camera.ar"
android:required="true" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="com.google.ar.core"
android:value="required" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Hope it helps! Thanks!
Upvotes: 3
Reputation: 1
here is an example of the manifest file that is wrote for my ar app. you forget to put uses permission android.hardware.camera.ar that's why you get this error.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.arguide">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true">
<activity android:name=".Activities.AddEquipementActivity"></activity>
<activity android:name=".Activities.ArActivity" />
<activity android:name=".Activities.AdminMainActivity" />
<activity android:name=".Activities.InterventionDetailsActivity" />
<activity android:name=".Activities.AddGroupActivity" />
<activity android:name=".Activities.RegisterPasswordActivity" />
<activity android:name=".Activities.RegisterVerifEmailActivity" />
<activity android:name=".Activities.Register2Activity" />
<activity android:name=".Activities.MainActivity" />
<activity android:name=".Activities.RegisterActivity" />
<activity android:name=".Activities.SplashScreenActivity" />
<activity android:name=".Activities.LoginActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.ar.core"
android:value="required" />
</application>
<uses-feature
android:name="android.hardware.camera.ar"
android:required="true" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
Upvotes: 0