What This Example Demonstrates
- Initializing a
BridgeHostwith typed capability slots - Declaring document capture support via handler assignment
- Displaying a journey by attaching a
WebViewin Compose - Observing bridge errors through a
BridgeHostDelegate
Complete Source
How It Works
-
BridgeHost(hostVersion = ...)creates a host with typed capability slots. No separate configuration object is needed.BridgeHostis main-thread-only, so construct it and call its methods on the main thread β composition already runs there. -
Setting a handler on
documentCapturedeclares the capability as supported. The handler usesawaitCompletion()to suspend until the UI layer callscomplete(). -
CameraDetector.check(context)detects camera hardware and permission state. ThepermissionStateis included in capability query responses. -
host.attach(webView)configures the WebView (enables JavaScript and DOM storage), installs a bootstrap-injectingWebViewClient, and registers thewindow.GBGBridgeJavaScript interface. There is noBridgeWebViewwrapper on Android β you create a plainWebView(here viaAndroidView), attach it to the host, and callloadUrl()yourself. -
Error observation β unlike iOS, where
host.lastErroris@Published, the AndroidlastErrorproperty is not observable. Instead, implementBridgeHostDelegate.onErrorand mirror errors into Compose state. The host holds its delegate in a weak reference, so keep a strong reference to it (here viaremember) or it will be garbage-collected and silently stop firing. -
DisposableEffect.onDisposecallshost.detach()when the screen leaves composition. Detach cancels any in-flight capture, removes the JavaScript interface, and clears the message buffers, so the host is ready for a clean re-attach.
What Happens at Runtime
Required Project Configuration
To compile this example, add the SDK dependency to your moduleβsbuild.gradle.kts (no repository setup is needed beyond mavenCentral()):
10.0.2.2, not localhost):
android:networkSecurityConfig="@xml/network_security_config" on the <application> element. Never ship an unscoped android:usesCleartextTraffic="true" in production.
Next Steps
- Two-Way Communication β Send events and handle requests
- Advanced Integration β Custom handlers, lifecycle, and error handling