With ARCore for Jetpack XR, your app can retrieve a device's pose: the orientation (pitch, yaw, roll) and a position (X, Y, Z) of the device relative to the world origin.
Use this information to render digital content in the real world, or convert the device pose to a geospatial pose to generate location-aware data.
Access a session
Access device pose information through a Jetpack XR Runtime Session,
which your app must create.
Configure the session
Device pose information is not enabled by default on XR sessions. To enable your
app to retrieve device pose information, configure the session and set the
HeadTrackingMode.LAST_KNOWN mode:
// Define the configuration object to enable tracking device pose.
val newConfig = session.config.copy(
headTrackingMode = Config.HeadTrackingMode.LAST_KNOWN
)
// Apply the configuration to the session.
try {
when (val configResult = session.configure(newConfig)) {
is SessionConfigureSuccess -> {
// The session is now configured to track the device's pose.
}
else -> {
// Catch-all for other configuration errors returned using the result class.
}
}
} catch (e: UnsupportedOperationException) {
// Handle configuration failure. For example, if the specific mode is not supported on the current device or API version.
}
Not all XR devices support the HeadTrackingMode.LAST_KNOWN mode. If
Session.configure() succeeds, the device supports this mode.
Obtain the device pose
Once the session is configured, you can obtain the device's pose within the AR
coordinate system using the ArDevice object:
// Get the ArDevice instance
val arDevice = ArDevice.getInstance(session)
// Collect the state to process the device pose
arDevice.state.collect { state ->
// processDevicePose gets called automatically when a new pose is available.
processDevicePose(state.devicePose)
}
// Or, get the current device Pose from the AR Device's state.
// This is the device's position and orientation relative to the tracking origin.
val devicePose = ArDevice.getInstance(session).state.value.devicePose
Get the device pose's translation and rotation
The device Pose represents the device's position (translation) and orientation
(rotation) relative to the tracking origin. Use this information in your app to
enhance your app's experience:
Provide positionally accurate navigation instructions: Positional data can be used to help a user orient themselves and navigate their surroundings with help from overlaid digital content.
Intermediate world alignment: This pose is consumed by the Geospatial API to calculate the real-world location.
fun processDevicePose(pose: Pose) {
// Extract Translation and Rotation
val translation = pose.translation // Vector3(x, y, z)
val rotation = pose.rotation // Quaternion (x, y, z, w)
TODO(/* Use the translation and rotation in your app. */)
}
Convert the device pose to a geospatial pose
Once you have the device pose, you can obtain a geospatial pose from it. Converting to a geospatial pose takes your AR content from a temporary, isolated experience to a permanent, universally shared, and context-aware feature in the real world.
Learn how to convert a device pose to a geospatial pose in our Geospatial API documentation.