API reference

DeviceSession Class

Modifiers: final
Manages a session with a Meta Wearables device. Sessions are created via Wearables.createSession and provide explicit lifecycle control through DeviceSession.start and DeviceSession.stop.
A session progresses through these states:
DeviceSessionState.STOPPED is a terminal state. Once stopped (explicitly or due to external termination), create a new session via Wearables.createSession.
Capabilities (e.g., Stream, Capture) are attached to a session via addStream, addCapture, and removed via removeStream, removeCapture, or Capability.stop. When the session is stopped, all attached capabilities are automatically stopped (cascading stop).

See Also

Signature

class DeviceSession

Properties

errors : SharedFlow<DeviceSessionError>
[Get]
A SharedFlow that emits session errors.
Errors are emitted when DeviceSession.start fails (e.g., device becomes unavailable during connection), when the device disconnects externally, or when the session is ended by the device. Observe this flow alongside DeviceSession.state for comprehensive session monitoring.
Example:
session.errors.collect { error ->
    when (error) {
        DeviceSessionError.DEVICE_DISCONNECTED -> showReconnectPrompt()
        DeviceSessionError.DEVICE_POWERED_OFF -> showDeviceOffMessage()
        else -> showError(error.description)
    }
}

Signature
val errors: SharedFlow<DeviceSessionError>
state : StateFlow<DeviceSessionState>
[Get]
A StateFlow that provides the current state of this session.
The state progresses through DeviceSessionState.IDLEDeviceSessionState.STARTINGDeviceSessionState.STARTEDDeviceSessionState.STOPPINGDeviceSessionState.STOPPED. The session may also transition to DeviceSessionState.PAUSED and back to DeviceSessionState.STARTED. Observe this flow to react to session lifecycle changes, including external termination (device disconnect, power off).
Both DeviceSession.start and DeviceSession.stop are sync fire-and-forget — observe this flow for completion signals.
Signature
val state: StateFlow<DeviceSessionState>

Methods

addDisplay ( config )
Adds a display capability to this session.
Creates and starts a display connection to the device connected to this session. Only one display can exist per session — calling this method when a display is already attached returns DeviceSessionError.CAPABILITY_ALREADY_ADDED.
The display must be added after the session reaches DeviceSessionState.STARTED. Adding a display to an idle session returns DeviceSessionError.SESSION_IDLE, and adding to a stopped session returns DeviceSessionError.SESSION_ALREADY_STOPPED.
Example:
session.addDisplay().fold(
    onSuccess = { display ->
        display.sendContent { flexBox { text("Hello") } }
    },
    onFailure = { error -> showError(error.description) }
)

Signature
fun DeviceSession.addDisplay(config: DisplayConfiguration = DisplayConfiguration()): DatResult<Display, DeviceSessionError>
Parameters
config: DisplayConfiguration  Configuration for the display (reserved for future options)
Returns
DatResult DatResult containing the Display on success, or DeviceSessionError on failure
addStream ( streamConfiguration )
Adds a camera streaming capability to this session.
Creates and starts a video stream from the device connected to this session. Only one stream can exist per session — calling this method when a stream is already attached returns DeviceSessionError.CAPABILITY_ALREADY_ADDED.
The stream must be added after the session reaches DeviceSessionState.STARTED. Adding a stream to an idle session returns DeviceSessionError.SESSION_IDLE, and adding to a stopped session returns DeviceSessionError.SESSION_ALREADY_STOPPED.
Example:
session.addStream(
    streamConfiguration = StreamConfiguration(videoQuality = VideoQuality.HIGH),
).fold(
    onSuccess = { stream ->
        stream.videoStream.collect { frame -> processFrame(frame) }
    },
    onFailure = { error -> showError(error.description) }
)

Signature
fun DeviceSession.addStream(streamConfiguration: StreamConfiguration = StreamConfiguration()): DatResult<Stream, DeviceSessionError>
Parameters
streamConfiguration: StreamConfiguration  Configuration for the stream (video quality, frame rate)
Returns
DatResult DatResult containing the Stream on success, or DeviceSessionError on failure
removeDisplay ()
Removes the display capability from this session.
Stops the display and detaches it from the session. After removal, a new display can be added via DeviceSession.addDisplay.
Signature
fun DeviceSession.removeDisplay(): DatResult<Unit, DeviceSessionError>
Returns
DatResult DatResult with Unit on success, or DeviceSessionError on failure
removeStream ()
Removes the camera streaming capability from this session.
Stops the stream and detaches it from the session. After removal, a new stream can be added via DeviceSession.addStream.
Signature
fun DeviceSession.removeStream(): DatResult<Unit, DeviceSessionError>
Returns
DatResult DatResult with Unit on success, or DeviceSessionError on failure
start ()
Starts the session, connecting to the device resolved during Wearables.createSession.
This method is sync fire-and-forget: it returns immediately and the connection proceeds in the background. Observe DeviceSession.state for DeviceSessionState.STARTINGDeviceSessionState.STARTED transitions, and DeviceSession.errors for failure details.
Calling DeviceSession.start on a session that is not in DeviceSessionState.IDLE is a no-op.
Signature
fun start()
stop ()
Stops the session and all attached capabilities.
This method is sync fire-and-forget: it transitions to DeviceSessionState.STOPPING and then DeviceSessionState.STOPPED, performing cleanup. All attached capabilities are stopped (cascading stop). The session is unregistered from the session registry, releasing the device for new sessions.
Calling DeviceSession.stop on an already-stopped or stopping session is a no-op.
Signature
fun stop()