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 DeviceSession with Wearables.createSession(...), start it, then attach capabilities such as camera streaming once the session is STARTED. Session lifecycle and capability 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 suspended by pause() or the deviceKeep state, call session.resume() or wait for the device to resume
STOPPINGSession is shutting downStop user work and wait
STOPPEDTerminal; session endedRelease resources and create a new session if needed

STOPPED is terminal: a stopped session cannot be restarted, so call Wearables.createSession(...) again for the next connection.

Observe session state

start(), stop(), pause(), and resume() are all sync fire-and-forget and return Unit. Subscribe to state and errors before calling start() so no transition is missed, and treat session.errors as the failure channel for start().

Wearables.createSession(AutoDeviceSelector()).fold(
    onSuccess = { session ->
        lifecycleScope.launch {
            session.state.collect { state ->
                when (state) {
                    DeviceSessionState.STARTED -> onStarted(session)
                    DeviceSessionState.PAUSED -> onPaused()
                    DeviceSessionState.STOPPED -> onStopped()
                    else -> Unit
                }
            }
        }
        lifecycleScope.launch {
            session.errors.collect { error -> showError(error.description) }
        }
        session.start()
    },
    onFailure = { error, _ -> showError(error.description) },
)

Add each capability once from onStarted(session): a resumed session goes from PAUSED back to STARTED and keeps its capabilities, so a second session.addCamera(...) fails with DeviceSessionError.CAPABILITY_ALREADY_ADDED. Calling session.addCamera(...) while the session is still IDLE fails with DeviceSessionError.SESSION_IDLE, and calling it after STOPPED fails with DeviceSessionError.SESSION_ALREADY_STOPPED.

Stream state

Camera streaming has its own state flow after you add a camera:

STOPPED -> STARTING -> STARTED -> STREAMING -> STOPPING -> STOPPED

CLOSED is the terminal stream state, and PAUSED is reported when the device pauses streaming (for example a single cap-touch tap) and can resume on its own.

lifecycleScope.launch {
    camera.stream.state.collect { state ->
        // React to stream 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

Call pause() on a STARTED session and resume() on a PAUSED one. Like start() and stop(), both are sync fire-and-forget: observe state for the STARTED -> PAUSED -> STARTED transitions. pause() outside STARTED and resume() outside PAUSED are no-ops. The state changes only when the device confirms, so wait for PAUSED before calling resume() (and for STARTED before calling pause() again); earlier calls are ignored. If the observed state doesn't match what your app wants, call pause() or resume() again once it settles.

pauseButton.setOnClickListener { session.pause() }   // STARTED -> PAUSED
resumeButton.setOnClickListener { session.resume() } // PAUSED -> STARTED

The device can also pause and resume the session on its own (for example a single cap-touch tap). When a session is paused:

  • The device connection may remain active
  • Attached capabilities stop doing useful work
  • Your app should not restart the session; call resume() or wait for the next observed session state

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 DeviceSessionError and StreamError failures

Links

More skills from facebook

add-ir-instruction
facebook
When adding a new IR instruction, you must touch a specific set of files. This skill describes each file, the pattern to follow, and important conventions.
add-shape-types-to-torch-model
facebook
Port a PyTorch model to use pyrefly's tensor shape type system (Tensor[B, C, H, W], Dim[T]). Use this skill whenever the user wants to add shape annotations to…
add-torch-shapes-example
facebook
Use when adding a new PyTorch model to Pyrefly's shape-tracking example corpus under tensor-shapes/pyrefly-torch-stubs/examples — i.e. importing a model as a…
api-health
facebook
Monitor API health for a Meta app — check rate limits, call volume, and API deprecations. Use to diagnose throttling, plan capacity, or prepare for API version…
api-integration
facebook
Guide a developer through setting up a Meta API integration from scratch — discovers the right APIs, fetches setup guides, authentication requirements,…
app-health-check
facebook
Run a comprehensive health check on a Meta app — audits settings, security, compliance, app review status, rate limits, and API deprecations in one pass. Use…
app-review-prep
facebook
Prepare a Meta app for App Review — checks current status, outstanding requirements, granted privileges, and submission history. Use before submitting an app…
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.