Check for spatial capabilities

Spatial capabilities can change as users interact with your app or the system, or can even be changed by your app itself—for example, moving into Home Space or Full Space. To avoid issues, your app needs to check for spatial capabilities to determine which APIs are supported in the current environment.

Check for spatial capabilities using Jetpack Compose for XR

Jetpack Compose for XR creates a Composition Local for checking spatial capabilities. Use this to check whether spatial UI, spatial audio, environments, passthrough, or 3D content is enabled.

You can use LocalSpatialCapabilities.current to check if the following spatial capabilities are currently available:

The following example shows how to check if spatial UI is enabled.

if (LocalSpatialCapabilities.current.isSpatialUiEnabled) {
    Subspace {
        SpatialPanel(
            modifier = SubspaceModifier
                .width(1488.dp)
                .fillMaxHeight()
        ) {
            AppContent()
        }
    }
} else {
    AppContent()
}

Check for spatial capabilities using SceneCore

When using the SceneCore library, you'll have to create a session. Once the session is created, call getSpatialCapabilities on the session to query which spatial capabilities are currently available.

You can also choose to subscribe to a callback, addSpatialCapabilitiesChangedListener that notifies you when spatial capabilities have changed.

val xrSession = Session.create(this)

// Example 1: check if enabling passthrough mode is allowed
if (xrSession.getSpatialCapabilities().hasCapability(
       SpatialCapabilities.SPATIAL_CAPABILITY_PASSTHROUGH_CONTROL)) {
   xrSession.spatialEnvironment.setPassthroughOpacityPreference(0f)
}
// Example 2: Multiple capability flags can be checked simultaneously:
if (xrSession.getSpatialCapabilities().hasCapability(
       SpatialCapabilities.SPATIAL_CAPABILITY_PASSTHROUGH_CONTROL and
               SpatialCapabilities.SPATIAL_CAPABILITY_3D_CONTENT)) {
   // ...
}

// Example 3: Create a spatialized panel if/when spatialization UI becomes available
xrSession.addSpatialCapabilitiesChangedListener((capabilities) -> {
  if (capabilities.hasCapability(SpatialCapabilities.SPATIAL_CAPABILITY_UI)){
Subspace{
   SpatialPanel(...)
      }
   }
});

See also