# Share scene content between Android mobile and Jetpack XR

Share the **scene content**, then provide a separate host for each tracking
runtime. Mobile ARCore (`com.google.ar.core`) and ARCore for Jetpack XR
(`androidx.xr.arcore`) have different sessions and trackables. A phone AR scene
does not become a headset renderer by placing it in an XR layout.

| Host | Content reuse | Tracking |
|---|---|---|
| Mobile `SceneView` | Calls the shared `SceneScope` function | Orbit camera |
| Mobile `PlacementScene` | Calls it inside `AnchorNode` | Phone ARCore |
| XR inspection preview in `SceneView` | Calls it at a mapped hand position | Host-owned Jetpack XR session |

The bridge below is an **experimental Filament viewport preview**. It does not
implement stereoscopic rendering, headset camera alignment or native SceneCore
entities. Its API was inspected in 4.76.0; it has not been verified on a headset.

## Shared content and mobile hosts

Use `implementation("io.github.sceneview:arsceneview:4.76.0")`, which includes
3D. Follow the [Android setup](../quickstart.md) for the toolchain and AR
manifest. Put a GLB at `app/src/main/assets/models/model.glb`.

```kotlin
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import io.github.sceneview.SceneScope
import io.github.sceneview.SceneView
import io.github.sceneview.ar.PlacementScene
import io.github.sceneview.math.Position
import io.github.sceneview.rememberModelInstance

@Composable
fun SceneScope.InspectionContent(modelPath: String, at: Position = Position(0f)) {
    rememberModelInstance(modelLoader, modelPath)?.let { instance ->
        ModelNode(
            modelInstance = instance,
            scaleToUnits = 0.25f,
            position = at,
            autoAnimate = true,
        )
    }
}

@Composable
fun MobileModelInspection(modelPath: String = "models/model.glb") {
    SceneView(modifier = Modifier.fillMaxSize()) {
        InspectionContent(modelPath)
    }
}

@Composable
fun MobileArInspection(modelPath: String = "models/model.glb") {
    PlacementScene(
        modifier = Modifier.fillMaxSize(),
        coaching = true,
        onPlaced = { anchor ->
            AnchorNode(anchor = anchor) {
                InspectionContent(modelPath)
            }
        },
    )
}
```

`NodeScope` and `ARSceneScope` inherit `SceneScope`, so the same extension can
run at the root or under an anchor. Each call owns its own model instance; do
not attach one instance to multiple nodes. This is a 25 cm inspection model.
For authored dimensions and grounding, use the [real-size placement recipe](native-ar-product-placement.md).

## Optional Jetpack XR adapter

`arsceneview/build.gradle` declares the XR artifact as `compileOnly`. Phone-only
apps do not receive it transitively. In the **XR app module or XR build variant**,
add the same pin used by this tree:

```kotlin
implementation("androidx.xr.arcore:arcore:1.1.0-alpha02")
```

Keep the following file in that XR module/variant. Its host supplies a `Hand`
from a successfully created, resumed Jetpack XR session, obtains the required
tracking permissions and stops composing the preview when tracking is unavailable.
Session creation and device capability checks remain the host's responsibility.

```kotlin
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.xr.arcore.Hand
import io.github.sceneview.SceneView
import io.github.sceneview.ar.xr.XrHandJoint
import io.github.sceneview.ar.xr.XrPreviewApi
import io.github.sceneview.ar.xr.readHandJoints
import io.github.sceneview.math.Position

@OptIn(XrPreviewApi::class)
@Composable
fun XrHandInspectionPreview(
    hand: Hand,
    modelPath: String,
    toScenePosition: (Position) -> Position,
) {
    // Collect to invalidate composition when the upstream hand snapshot changes.
    val snapshot = hand.state.collectAsState().value
    val palm = remember(hand, snapshot) { readHandJoints(hand)[XrHandJoint.PALM] }
    SceneView(
        modifier = Modifier.fillMaxSize(),
        autoCenterContent = false,
        autoFitContent = false,
    ) {
        palm?.let { InspectionContent(modelPath, at = toScenePosition(it)) }
    }
}
```

Use the same package as the shared file, or import `InspectionContent` from
your app's package. `readHandJoints` copies joint positions into SceneView's
`Position` type. `toScenePosition` is an **app-supplied coordinate mapping** from
XR tracking space into the viewport's scene space; it is not an SDK function.
The example follows palm position only, without hand orientation. Missing joint
data hides the model. The XR host must also handle tracking loss explicitly.

For a phone entry point that never references XR types, the classpath gate is:

```kotlin
import android.content.Context
import io.github.sceneview.ar.xr.XrFeatures

fun hasXrRuntimeClasses(context: Context): Boolean = XrFeatures.isAvailable(context)
```

A `true` result proves the XR classes are loadable, **not** that the device
supports XR or a session is active. Choose the mobile host on phones; enter the
XR adapter only after the XR host has established a valid session. Keeping
`Hand` signatures in the XR module avoids loading optional types in a phone-only
APK.

The tree also provides `SceneScope.XrHandNode` for joint-node geometry, but its
root is not automatically a palm anchor. Its wrapper updates on recomposition;
the host must drive those updates. The explicit position adapter above avoids
assuming that nesting a model under the hand root attaches it to the palm.

Sources: [XrFeatures.kt](https://github.com/sceneview/sceneview/blob/v4.76.0/arsceneview/src/main/java/io/github/sceneview/ar/xr/XrFeatures.kt),
[XrHandNode.kt](https://github.com/sceneview/sceneview/blob/v4.76.0/arsceneview/src/main/java/io/github/sceneview/ar/xr/XrHandNode.kt)
and [XrHandSkeleton.kt](https://github.com/sceneview/sceneview/blob/v4.76.0/arsceneview/src/main/java/io/github/sceneview/ar/xr/XrHandSkeleton.kt).
All snippets were verified by source inspection at 4.76.0; none were compiled or run on a device.
