Session

public class Session

Manages AR system state and handles the session lifecycle. This class is the main entry point to ARCore API. This class allows the user to create a session, configure it, start/stop it, and most importantly receive frames that allow access to camera image and device pose.

Public Constructors

Session(Context context)
Creates a new ARCore session.

Protected Constructors

Session()
(FOR TESTING) Constructs a new instance for use as a mock.

Public Methods

void
configure(Config config)
Configures the session.
Anchor
createAnchor(Pose pose)
Defines a tracked location in the physical world.
Collection<Anchor>
getAllAnchors()
Returns all known anchors, including those not currently tracked.
<T extends Trackable> Collection<T>
getAllTrackables(Class<T> filterType)
Returns the list of all known trackables.
void
getConfig(Config configToFill)
Similar to getConfig(), but takes a config object to fill.
Config
getConfig()
Gets the current config.
Anchor
hostCloudAnchor(Anchor anchor)
Uses the pose and other data from anchor to create a new anchor that will be hosted.
boolean
isSupported(Config config)
This method is deprecated. in release 1.2.0. Please refer to (release notes 1.2.0).
void
pause()
Pause the current session.
Anchor
resolveCloudAnchor(String cloudAnchorId)
Creates a new anchor whose pose will be resolved from the ARCore Cloud using the given cloudAnchorId.
void
resume()
Starts or resumes the ARCore Session.
void
setCameraTextureName(int textureId)
Sets the OpenGL texture name (id) that will allow GPU access to the camera image.
void
setDisplayGeometry(int displayRotation, int widthPx, int heightPx)
Sets the aspect ratio, coordinate scaling, and display rotation.
Frame
update()
Updates the state of the ARCore system.

Inherited Methods

Public Constructors

public Session (Context context)

Creates a new ARCore session. This must be the first ARCore action that your application takes.

Parameters
context the Context for your app
Throws
FatalException if an internal error occurred while creating the session. adb logcat may contain useful information.
UnavailableArcoreNotInstalledException if the ARCore APK is not present
UnavailableSdkTooOldException if the ARCore SDK that this application was built with is too old and no longer supported by the installed ARCore APK.
UnavailableApkTooOldException if the installed ARCore APK is too old for the ARCore SDK with which this application was built.
SecurityException if client app fails to grant CameraPermission.
FatalException if initialization failed for any other reason.

Protected Constructors

protected Session ()

(FOR TESTING) Constructs a new instance for use as a mock. Calling any base method implementation on this instance may return unexpected results, throw an exception, or even crash.

To create a Session normally, use Session(Context).

Public Methods

public void configure (Config config)

Configures the session.

A session initially has a default configuration. This should be called if a configuration different than default is needed.

Parameters
config The new configuration setting for the session.
Throws
UnsupportedConfigurationException if the configuration requested is not supported.
See Also

public Anchor createAnchor (Pose pose)

Defines a tracked location in the physical world. See Anchor for more details.

Anchors incur ongoing processing overhead within ARCore. To release unneeded anchors use detach().

Parameters
pose a pose to anchor to the physical world
See Also

public Collection<Anchor> getAllAnchors ()

Returns all known anchors, including those not currently tracked. Anchors forgotten by ARCore due to a call to detach() or entering the STOPPED state will not be included.

public Collection<T> getAllTrackables (Class<T> filterType)

Returns the list of all known trackables. This includes Planes if plane detection is enabled, as well as Points created as a side effect of calls to createAnchor(Pose) or hitTest(float, float).

Parameters
filterType The desired trackable type, or Trackable.class to retrieve all trackables.

public void getConfig (Config configToFill)

Similar to getConfig(), but takes a config object to fill. Use this form to avoid extra object allocations.

Parameters
configToFill
See Also

public Config getConfig ()

Gets the current config. More specifically, returns a copy of the config most recently set by configure(Config).

Note: if the session was not explicitly configured, a default configuration is returned (same as new Config(session)).

To avoid allocations, use getConfig(Config) instead.

public Anchor hostCloudAnchor (Anchor anchor)

Uses the pose and other data from anchor to create a new anchor that will be hosted. The returned anchor will have the cloud anchor state TASK_IN_PROGRESS.

Parameters
anchor The anchor whose pose is to be hosted.
Returns
  • The new anchor with the same pose as anchor which will be hosted.

public boolean isSupported (Config config)

This method was deprecated in API level .
in release 1.2.0. Please refer to (release notes 1.2.0).

Before release 1.2.0: Checks if the provided configuration is usable on the this device. If this method returns false, calls to configure(Config) with this configuration will throw an exception.

This function now always returns true. See documentation for each configuration entry to know which configuration options & combinations are supported.

Parameters
config the configuration to test

public void pause ()

Pause the current session. This method will stop the camera feed and release resources. The session can be restarted again by calling resume().

Typically this should be called from onPause().

Note that ARCore might continue consuming substantial computing resources for up to 10 seconds after calling this method.

public Anchor resolveCloudAnchor (String cloudAnchorId)

Creates a new anchor whose pose will be resolved from the ARCore Cloud using the given cloudAnchorId. The returned anchor will have the cloud anchor state TASK_IN_PROGRESS.

Parameters
cloudAnchorId The cloud anchor ID of the cloud anchor.
Returns
  • The new anchor whose pose will be resolved in a later call to update().

public void resume ()

Starts or resumes the ARCore Session.

Typically this should be called from onResume().

public void setCameraTextureName (int textureId)

Sets the OpenGL texture name (id) that will allow GPU access to the camera image. The provided ID should have been created with glGenTextures(int, int[], int). The resulting texture must be bound to the GL_TEXTURE_EXTERNAL_OES target for use. Shaders accessing this texture must use a samplerExternalOES sampler. See sample code for an example.

Parameters
textureId a valid OpenGL texture name (id)

public void setDisplayGeometry (int displayRotation, int widthPx, int heightPx)

Sets the aspect ratio, coordinate scaling, and display rotation. This data is used by UV conversion, projection matrix generation, and hit test logic.

Parameters
displayRotation display rotation specified by Surface constants: ROTATION_0, ROTATION_90, ROTATION_180, ROTATION_270,
widthPx width of the view, in pixels
heightPx height of the view, in pixels

public Frame update ()

Updates the state of the ARCore system. This includes: receiving a new camera frame, updating the location of the device, updating the location of tracking anchors, updating detected planes, etc.

This call may cause off-screen OpenGL activity. Because of this, to avoid unnecessary frame buffer flushes and reloads, this call should not be made in the middle of rendering a frame or offscreen buffer.

This call may update the pose of all created anchors and detected planes. The set of updated objects is accessible through getUpdatedTrackables(Class).

This call in blocking mode (see Config.UpdateMode) will wait until a new camera image is available, or until the built-in timeout (currently 66ms) is reached. If the camera image does not arrive by the built-in timeout, then update() will return the most recent Frame object. For some applications it may be important to know if a new frame was actually obtained (for example, to avoid redrawing if the camera did not produce a new frame). To do that, compare the current frame's timestamp, obtained via getTimestamp(), with the previously recorded frame timestamp. If they are different, this is a new frame.

Returns
  • The most recent Frame received

Send feedback about...