dat-conventions

作者: facebook

用於 DAT SDK Android 開發的 Kotlin 模式、DatResult、會話與能力慣例

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

DAT SDK Conventions (Android)

Quick Reference

TaskCommand
Build app./gradlew assembleDebug
Run tests./gradlew test
Install app./gradlew installDebug
Lint app./gradlew lint

Architecture

The SDK is organized into four public modules:

  • mwdat-core: Registration, permissions, devices, and session creation
  • mwdat-camera: Camera capability, video frames, and photo capture
  • mwdat-display: Display capability, display UI components, icons, images, buttons, and video
  • mwdat-mockdevice: MockDeviceKit for testing without hardware

Initialization and session setup

DeviceSession.start() is fire-and-forget and returns Unit; capabilities can only be attached once the session reports DeviceSessionState.STARTED.

Wearables.initialize(context)

Wearables.createSession(AutoDeviceSelector()).fold(
    onSuccess = { session ->
        lifecycleScope.launch {
            session.state.collect { state ->
                if (state == DeviceSessionState.STARTED) {
                    session.addCamera(StreamConfiguration()).fold(
                        onSuccess = { camera ->
                            camera.stream.start().onFailure { error, _ ->
                                showError(error.description)
                            }
                        },
                        onFailure = { error, _ -> showError(error.description) },
                    )
                }
            }
        }
        lifecycleScope.launch {
            session.errors.collect { error -> showError(error.description) }
        }
        session.start()
    },
    onFailure = { error, _ -> showError(error.description) },
)

Kotlin patterns

  • Use DatResult<T, E> for typed success and failure handling
  • Prefer fold, onSuccess, and the two-parameter onFailure { error, cause -> } overload, which hands you the typed DatError with a description
  • getOrElse { } receives a raw Throwable, not a typed DatError, so it cannot read error.description
  • Observe state with StateFlow and Flow
  • Create a DeviceSession first, then attach capabilities such as Camera or Display after it reaches STARTED
  • Keep frame handling off the main thread when doing heavier processing

Error handling

Wearables.checkPermissionStatus(Permission.CAMERA)
    .onSuccess { status -> /* handle status */ }
    .onFailure { error, _ -> /* handle error */ }

Avoid getOrThrow() in user-facing samples. Surface typed errors from DatResult instead.

Naming conventions

TypePurposeExample
DeviceSessionDevice connection lifecycleWearables.createSession(...)
CameraCamera capability on a sessionsession.addCamera(...)
StreamVideo stream from a cameracamera.stream
DisplayDisplay capability on a sessionsession.addDisplay(...)
*SelectorDevice targetingAutoDeviceSelector, SpecificDeviceSelector
*ErrorTyped failure surfaceDeviceSessionError, StreamError, CaptureError

Key types

  • Wearables — SDK entry point
  • DeviceSession — lifecycle for an interaction with a linked device
  • Camera — camera capability attached to a session
  • Stream — video stream accessed through camera.stream
  • Display — display capability attached to a session
  • StreamConfiguration — video quality, frame rate, and compression configuration
  • MockDeviceKit — simulated device environment for testing

Live docs search

If your editor supports remote MCP servers, connect https://mcp.developer.meta.com/wearables and use search_dat_docs for current DAT setup, session lifecycle, camera streaming, MockDeviceKit, permissions, and exact API symbols. This public docs server does not require authentication; do not configure tokens, OAuth, or custom authorization headers for it.

See the dat-docs-mcp skill for client setup and troubleshooting. Use llms.txt when your tool only supports static reference context.

Testing with MockDeviceKit

val mockDeviceKit = MockDeviceKit.getInstance(context)
mockDeviceKit.enable()
mockDeviceKit.pairGlasses(GlassesModel.RAYBAN_META).fold(
    onSuccess = { device -> onMockDevicePaired(device) },
    onFailure = { error, _ -> showError(error.description) },
)

Use MockDeviceKit to drive registration, device availability, streaming media, and permission scenarios without physical hardware.

Common pitfalls

  • Do not call SDK APIs before Wearables.initialize(context)
  • Do not call addCamera(...) or addDisplay() immediately after session.start(); wait for DeviceSessionState.STARTED
  • Do not assume a session implies streaming or display access; capabilities are attached separately
  • Do not ignore DatResult failures from createSession, start, addCamera, addDisplay, or capturePhoto
  • Do not reuse terminally stopped sessions, cameras, or streams

Links

來自 facebook 的更多技能

gc-safe-coding
facebook
完整解釋與理由請參閱 doc/GCSafeCoding.md。
app-review-prep
facebook
準備將 Meta 應用程式提交至 App Review——檢查目前狀態、未完成的要求、已授予的權限及提交紀錄。請在提交應用程式前使用……
api-health
facebook
監控 Meta 應用程式的 API 健康狀態 — 檢查速率限制、呼叫量與 API 棄用情況。用於診斷節流問題、規劃容量,或為 API 版本…做好準備。
debug-webhooks
facebook
排解 Meta 應用程式的 webhook 問題——檢查作用中的訂閱、找出設定錯誤,並傳送測試負載以驗證傳遞。當……時使用。
api-integration
facebook
引導開發者從零開始設定 Meta API 整合 — 找出正確的 API、取得設定指南、驗證需求、…
webhook-setup
facebook
端到端設定 Meta 應用程式的 Webhook — 探索可用主題、訂閱欄位,並透過測試負載驗證。在為… 設定 Webhook 時使用。
test-ui
facebook
使用 iwsdk CLI 針對 poke 範例測試 UI 系統(PanelUI、ScreenSpace)。
flags
facebook
檢查並比較 React 發佈通道中的功能旗標狀態。檢視所有通道(www、www-modern、canary、next、experimental、rn 變體)的旗標,或使用 --diff 比較特定通道。輸出格式包括預設表格檢視、CSV 匯出及清理狀態分組。旗標狀態以符號表示:啟用(✅)、停用(❌)、變體測試(🧪)、僅分析(📊)。常見陷阱:__VARIANT__ 旗標在 www 上會以兩種狀態進行測試;使用 --diff 可找出有意義的差異。