sample-app-guide

Membangun aplikasi DAT lengkap dengan pembuatan sesi, streaming kamera, dan pengambilan foto

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

Sample App Guide (Android)

Build an Android DAT app with registration, sessions, camera streaming, and photo capture.

Pair this with the CameraAccess sample.

Project setup

  1. Create an Android Studio app project.
  2. Add the DAT Maven repository and dependencies.
  3. Configure AndroidManifest.xml for registration callbacks plus APPLICATION_ID and CLIENT_TOKEN.
  4. Initialize Wearables in your Application.

Suggested app structure

app/src/main/java/com/example/myapp/
├── MyApplication.kt
├── MainActivity.kt
├── session/
│   └── SessionViewModel.kt
└── ui/
    ├── RegistrationScreen.kt
    └── CameraScreen.kt

Registration and session creation

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        lifecycleScope.launch {
            Wearables.registrationState.collect { state ->
                // Update registration UI
            }
        }
    }

    fun register() {
        Wearables.startRegistration(this)
    }
}

DeviceSession.start() is fire-and-forget. Attach the camera from the session-state collector once the session reports DeviceSessionState.STARTED; adding a capability to an IDLE session fails with DeviceSessionError.SESSION_IDLE.

class SessionViewModel : ViewModel() {
    private var session: DeviceSession? = null
    private var camera: Camera? = null

    fun startCameraSession() {
        Wearables.createSession(AutoDeviceSelector()).fold(
            onSuccess = { createdSession ->
                session = createdSession

                viewModelScope.launch {
                    createdSession.errors.collect { error -> showError(error.description) }
                }
                viewModelScope.launch {
                    createdSession.state.collect { state ->
                        when (state) {
                            DeviceSessionState.STARTED -> attachCamera(createdSession)
                            DeviceSessionState.STOPPED -> clearSession(createdSession)
                            else -> Unit
                        }
                    }
                }

                createdSession.start()
            },
            onFailure = { error, _ -> showError(error.description) },
        )
    }

    private fun attachCamera(activeSession: DeviceSession) {
        if (camera != null) return
        activeSession.addCamera(
            StreamConfiguration(videoQuality = VideoQuality.MEDIUM, frameRate = 24),
        ).fold(
            onSuccess = { addedCamera ->
                camera = addedCamera
                addedCamera.stream.start().onFailure { error, _ ->
                    showError(error.description)
                }
            },
            onFailure = { error, _ -> showError(error.description) },
        )
    }

    private fun clearSession(stoppedSession: DeviceSession) {
        if (session !== stoppedSession) return
        camera = null
        session = null
    }
}

Observe frames and capture photos

viewModelScope.launch {
    camera?.stream?.videoStream?.collect { frame ->
        // Render preview
    }
}

fun capturePhoto() {
    viewModelScope.launch {
        camera?.stream?.capturePhoto()
            ?.onSuccess { photoData ->
                when (photoData) {
                    is PhotoData.Bitmap -> savePhoto(photoData.bitmap)
                    is PhotoData.HEIC -> saveHeic(photoData.data)
                }
            }
            ?.onFailure { error, _ ->
                showCaptureError(error.description)
            }
    }
}

Shutdown

fun stopCameraSession() {
    camera?.stop()
    session?.stop()
    camera = null
    session = null
}

Testing with MockDeviceKit

Use MockDeviceKit to simulate linking glasses, permission state, and camera media without physical hardware. See MockDevice Testing for setup details.

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...