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`](https://developer.android.com/reference/kotlin/androidx/xr/compose/platform/package-summary#LocalSpatialCapabilities()) to check if the following
spatial capabilities are currently available:

- [`isSpatialUiEnabled`](https://developer.android.com/reference/kotlin/androidx/xr/compose/platform/SpatialCapabilities#isSpatialUiEnabled()): Indicates whether the application may create spatial UI elements (for example, `SpatialPanel`).
- [`isContent3dEnabled`](https://developer.android.com/reference/kotlin/androidx/xr/compose/platform/SpatialCapabilities#isContent3dEnabled()): Indicates whether the application may create 3D objects.
- [`isAppEnvironmentEnabled`](https://developer.android.com/reference/kotlin/androidx/xr/compose/platform/SpatialCapabilities#isAppEnvironmentEnabled()): Indicates whether the application may set the environment.
- [`isPassthroughControlEnabled`](https://developer.android.com/reference/kotlin/androidx/xr/compose/platform/SpatialCapabilities#isPassthroughControlEnabled()): Indicates whether the application may control the passthrough state.
- [`isSpatialAudioEnabled`](https://developer.android.com/reference/kotlin/androidx/xr/compose/platform/SpatialCapabilities#isSpatialAudioEnabled()): Indicates whether the application may use spatial audio.

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


```kotlin
if (LocalSpatialCapabilities.current.isSpatialUiEnabled) {
    Subspace {
        SpatialPanel(
            modifier = SubspaceModifier
                .width(1488.dp)
                .fillMaxHeight()
        ) {
            AppContent()
        }
    }
} else {
    AppContent()
}https://github.com/android/snippets/blob/2f246fb3f712172fefe2e1761f3483568348bf90/xr/src/main/java/com/example/xr/compose/SpatialCapabilities.kt#L51-L63
```

<br />

## Check for spatial capabilities using SceneCore

When using the SceneCore library, you'll have to create a [session](https://developer.android.com/develop/xr/jetpack-xr-sdk/add-session). Once the
session is created, call [`spatialCapabilities`](https://developer.android.com/reference/kotlin/androidx/xr/scenecore/Scene#spatialCapabilities()) on the session to query
which spatial capabilities are currently available.

- [`SPATIAL_CAPABILITY_3D_CONTENT`](https://developer.android.com/reference/kotlin/androidx/xr/scenecore/SpatialCapabilities#SPATIAL_CAPABILITY_3D_CONTENT()): The activity can create 3D contents.
- [`SPATIAL_CAPABILITY_APP_ENVIRONMENT`](https://developer.android.com/reference/kotlin/androidx/xr/scenecore/SpatialCapabilities#SPATIAL_CAPABILITY_APP_ENVIRONMENT()): The activity can set its own environment.
- [`SPATIAL_CAPABILITY_EMBED_ACTIVITY`](https://developer.android.com/reference/kotlin/androidx/xr/scenecore/SpatialCapabilities#SPATIAL_CAPABILITY_EMBED_ACTIVITY()): The activity can spatially embed another activity.
- [`SPATIAL_CAPABILITY_PASSTHROUGH_CONTROL`](https://developer.android.com/reference/kotlin/androidx/xr/scenecore/SpatialCapabilities#SPATIAL_CAPABILITY_PASSTHROUGH_CONTROL()): The activity can enable or disable passthrough.
- [`SPATIAL_CAPABILITY_SPATIAL_AUDIO`](https://developer.android.com/reference/kotlin/androidx/xr/scenecore/SpatialCapabilities#SPATIAL_CAPABILITY_SPATIAL_AUDIO()): The activity can use spatial audio.
- [`SPATIAL_CAPABILITY_UI`](https://developer.android.com/reference/kotlin/androidx/xr/scenecore/SpatialCapabilities#SPATIAL_CAPABILITY_UI()): The activity can spatialize itself (for example, adding a spatial panel).

You can also choose to subscribe to a callback,
[`addSpatialCapabilitiesChangedListener`](https://developer.android.com/reference/kotlin/androidx/xr/scenecore/Scene#addSpatialCapabilitiesChangedListener(java.util.function.Consumer)) that notifies you when spatial
capabilities have changed.


```kotlin
// Example 1: check if enabling passthrough mode is allowed
if (xrSession.scene.spatialCapabilities.hasCapability(
        SpatialCapabilities.SPATIAL_CAPABILITY_PASSTHROUGH_CONTROL
    )
) {
    xrSession.scene.spatialEnvironment.preferredPassthroughOpacity = 1f
}
// Example 2: multiple capability flags can be checked simultaneously:
if (xrSession.scene.spatialCapabilities.hasCapability(
        SpatialCapabilities.SPATIAL_CAPABILITY_PASSTHROUGH_CONTROL and
            SpatialCapabilities.SPATIAL_CAPABILITY_3D_CONTENT
    )
) {
    // ...
}https://github.com/android/snippets/blob/2f246fb3f712172fefe2e1761f3483568348bf90/xr/src/main/java/com/example/xr/scenecore/SpatialCapabilities.kt#L25-L39
```

<br />

## See also

- [Create a session](https://developer.android.com/develop/xr/jetpack-xr-sdk/check-spatial-capabilities)
- [Transition between HSM and FSM](https://developer.android.com/develop/xr/jetpack-xr-sdk/transition-home-space-to-full-space)
- [Add spatial environments to your app](https://developer.android.com/develop/xr/jetpack-xr-sdk/add-environments)
- [Add 3D models to your app](https://developer.android.com/develop/xr/jetpack-xr-sdk/add-3d-models)