session-lifecycle

Session state, stream state, pause and resume behavior, and device availability monitoring

npx skills add https://github.com/facebook/meta-wearables-dat-android --skill session-lifecycle

Session Lifecycle (Android)

Manage session and stream state in DAT SDK integrations.

Create a Session with Wearables.createSession(...), start it, then attach capabilities such as camera streaming. Session lifecycle and stream lifecycle are related but distinct.

Session states

StateMeaningApp action
IDLESession created, not started yetCall session.start()
STARTINGConnecting to the deviceShow loading UI
STARTEDSession active and ready for capabilitiesAdd or use capabilities
PAUSEDSession temporarily suspendedKeep state, wait for resume or stop
STOPPINGSession is shutting downStop user work and wait
STOPPEDSession endedRelease resources and create a new session if needed

Observe session state

val session = Wearables.createSession(AutoDeviceSelector()).getOrElse { error ->
    throw IllegalStateException(error.description)
}
session.start()

lifecycleScope.launch {
    session.state.collect { state ->
        when (state) {
            DeviceSessionState.STARTED -> onStarted()
            DeviceSessionState.PAUSED -> onPaused()
            DeviceSessionState.STOPPED -> onStopped()
            else -> Unit
        }
    }
}

Stream state

Camera streaming has its own state flow after you attach a stream:

STOPPED -> STARTING -> STARTED -> STREAMING -> STOPPING -> STOPPED -> CLOSED
lifecycleScope.launch {
    stream.state.collect { state ->
        // React to camera capability state changes
    }
}

Common transitions

The SDK may pause or stop a session when:

  • Another experience takes over the device
  • The user removes or folds the glasses
  • Bluetooth connectivity drops
  • The user unregisters the app or revokes needed access

Pause and resume

When a session is paused:

  • The device connection may remain active
  • Attached capabilities stop doing useful work
  • Your app should wait for the next observed session state instead of trying to force a restart

Device availability

lifecycleScope.launch {
    Wearables.devices.collect { devices ->
        // Update the list of available devices
    }
}

Use Wearables.devices and device metadata to decide when it is sensible to create a new session after a stop.

Checklist

  • Handle all DeviceSessionState values you care about
  • Observe stream state separately from session state
  • Release resources only after stop or close
  • Recreate sessions after terminal stops instead of reusing dead ones
  • Surface typed SessionError and StreamError failures

Links

More skills from facebook

binary-size-analysis
facebook
Analyze per-commit binary size changes of the hermesvm shared library across a git commit range. Produces a markdown report with per-commit sizes and summary tables of significant increases and decreases.
official
modify-jsi-features
facebook
Guide for adding new JSI functionality to the JavaScript Interface (JSI) layer. Use when the user asks to add, create, or implement new methods or features in…
official
non-interactive-git-rebase
facebook
Use when needing to reorder, split, drop, or amend git commits that are not the top commit, without interactive editor access. Covers programmatic rebase via…
official
click-target
facebook
Find and click a target object in XR. Use when testing UI interactions, clicking buttons, or verifying interactable elements work correctly.
official
iwsdk-planner
facebook
IWSDK project planning and best practices guide. Use when planning new IWSDK features, designing systems/components, reviewing IWSDK code architecture, or when…
official
iwsdk-ui-panel
facebook
Develop and iterate on IWSDK UI panels efficiently. Use when working on PanelUI components, debugging UI layout, or improving UI design in IWSDK applications.
official
test-all
facebook
Parallel test orchestrator. Runs all 9 test suites concurrently via Task sub-agents and the iwsdk CLI. Handles build, example setup, dev servers, agent launch,…
official
test-audio
facebook
Test audio system (AudioSource loading, playback state, stop, spatial audio) against the audio example using the iwsdk CLI.
official