session-lifecycle

Status sesi, status aliran, perilaku jeda dan lanjutkan, serta pemantauan ketersediaan perangkat

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 suspendedKeep state, wait for resume or stop
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() and stop() are both 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 capabilities from onStarted(session). 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

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

Links

Lebih banyak skill dari facebook

gc-safe-coding
facebook
Untuk penjelasan lengkap dan alasannya, lihat doc/GCSafeCoding.md .
app-review-prep
facebook
Siapkan aplikasi Meta untuk App Review — periksa status terkini, persyaratan yang belum terpenuhi, hak akses yang diberikan, dan riwayat pengiriman. Gunakan sebelum mengirimkan aplikasi…
api-health
facebook
Pantau kesehatan API untuk aplikasi Meta — periksa batas tarif, volume panggilan, dan penghentian API. Gunakan untuk mendiagnosis pembatasan, merencanakan kapasitas, atau bersiap untuk versi API…
debug-webhooks
facebook
Atasi masalah webhook untuk aplikasi Meta — periksa langganan aktif, identifikasi salah konfigurasi, dan kirim payload uji untuk memverifikasi pengiriman. Gunakan ketika…
api-integration
facebook
Pandu pengembang melalui penyiapan integrasi API Meta dari awal — temukan API yang tepat, ambil panduan penyiapan, persyaratan autentikasi,…
webhook-setup
facebook
Siapkan webhook untuk aplikasi Meta secara end-to-end — temukan topik yang tersedia, berlangganan ke bidang, dan verifikasi dengan payload uji. Gunakan saat mengonfigurasi webhook untuk…
test-ui
facebook
Uji sistem UI (PanelUI, ScreenSpace) terhadap contoh poke menggunakan CLI iwsdk.
flags
facebook
Periksa dan bandingkan status bendera fitur di seluruh saluran rilis React. Lihat semua bendera di berbagai saluran (www, www-modern, canary, next, experimental, varian rn) atau bandingkan saluran tertentu dengan --diff. Format keluaran mencakup tampilan tabel default, ekspor CSV, dan pengelompokan status pembersihan. Status bendera ditunjukkan dengan simbol: aktif (✅), nonaktif (❌), pengujian varian (🧪), hanya profiling (📊). Kendala umum: bendera __VARIANT__ diuji dalam kedua status di www; gunakan --diff untuk menemukan perbedaan yang berarti...