Using the Web SDK
Create the API object with createGMPlugin() and await gm.ready() before calling phone or glasses capabilities.
Where to find code and type definitions#
- Public implementation: PhoneSDK/packages/web-sdk/src/index.js.
- Parameters and return type: PhoneSDK/packages/web-sdk/src/index.d.ts.
- Permission format and method mapping: PhoneSDK/packages/bridge-contract/src/permission-policy.js.
- For a minimal runnable project, follow “Your first Web plugin”. Do not copy handshake code from older native Bridge examples.
Find APIs by feature#
| Feature | JavaScript entry point | Declaration |
|---|---|---|
| Initialization and Capability Query | gm.ready()、gm.runtime.getCapabilities()、getBridgeVersion() | Complete the handshake first, then use returned capabilities to decide which features to show. |
| Glasses page | gm.display.createPage / updateText / updateImage / closePage | display; use with GM Web Bridge in Studio. |
| Device Status | gm.device.getInfo() | device.info。 |
| Button and head-gesture events | gm.device.subscribeEvents / unsubscribeEvents;onButton / onGesture / onRawImu / onConnection | device.events, with scope.types declared. |
| Application data | gm.storage.get / set / remove / clear | storage; use the host's isolated storage. |
| User files | gm.files.pick / list / stat / openRead / getUsage / delete | files.user-selected; let the user select files instead of reading arbitrary system paths. |
| Phone–glasses messages | gm.plugin.sendMessage / onMessage | device.messaging; declare request and response channels. Requires a matching GMP. |
| Recording | gm.audio.openCapture / openRecording / stopCapture / onCaptureState | audio.capture; manage the returned sessions. Do not use the old startRecording API. |
| Foreground location | gm.location.getCurrentPosition / watchPosition / clearWatch / onPosition | location.foreground; Studio may return simulated location values. |
| Networking and playback | Standard browser APIs such as fetch and audio | network / audio.playback; system, CORS, CSP, and host-state restrictions also apply. |
Initialize before starting application logic#
gm.ready() means Web has completed its host handshake; it does not mean the glasses GMP is running. The current App prepares the Web session and starts the glasses component in parallel. Glasses business communication and runtime results become available only when both sides are ready.
Do not wait for glasses display, a plugin reply, or a business network response before calling ready. Initialize the page and complete the handshake first, then run business logic according to user actions and host state. Handle temporary unavailability and timeouts.
Call gm.close() when an SDK instance is no longer needed. Before closing, stop recording, cancel event/location subscriptions, and release your own resources. Create a new instance when re-entering the page.
Subscriptions and listeners are different#
const stopListening = gm.device.onButton(event => {
console.log('button', event);
});
const { subscriptionId } = await gm.device.subscribeEvents(['button']);
// When this feature stops:
await gm.device.unsubscribeEvents(subscriptionId);
stopListening();Files and audio#
- Files use fileId and controlled binary streams; short recordings and real-time capture use sessions. Follow the guides below for starting, consuming, stopping, and cleanup.
- Desktop microphones and simulated location are development tools. Verify actual input, permissions, and background behavior on the target phone and glasses.
Where data is stored#
In the current App, gm.storage is isolated by account and installation instance. Same-name updates retain installation ownership. Do not assume that switching accounts or installing under a new name grants access to old data.
Desktop Studio stores gm.storage locally on the computer by Web manifest.id, preserving it across refreshes and reopening Studio. A copied example with the same ID may read the original example's saved data; changing only name does not create separate simulated storage. Change the ID for an independent application. For an upgrade of the same application, keep the ID and test migration of old saved data.
Source and static assets are delivered in the ZIP. Runtime saved data, user-selected files, and local authorizations remain under the current installation's ownership. Design explicit export/import functionality when data migration is needed.
Location: one-time reads and continuous listening#
- Use gm.location.getCurrentPosition({ timeoutMs }) for a one-time location read. For continuous updates, register onPosition / onError first, then call watchPosition and save the watchId.
- When finished, await gm.location.clearWatch(watchId), then remove local event listeners. Removing listeners alone does not release the host's location resources.
- Results include WGS84 longitude and latitude, accuracy, timestamp, and precision. Do not present reduced precision as precise location or treat WGS84 values as another map coordinate system.
- Query getCapabilities().location for actual limits. The current App allows at most one watch, at most one update per second, and a default timeout of 15 seconds. Location still depends on foreground state and system authorization.
Sending large Web images in tiles#
- Start with standard gm.display APIs and verify a small image first. Do not pass PNG/JPEG bytes directly as GRAY_4 data.
- A tightly packed 600 × 350 GRAY_4 bitmap uses 105000 bytes, exceeding the current single Scene-message limit. Split it into independent tiles in the SDK format. Compress each tile separately with LZ4 and retain its uncompressed size.
- To display multiple tiles together, first check support for display.beginFrame and display.updateFrameImageLz4, then call beginFrame({ frameId, tileCount }).
- Starting at tileIndex 0, await updateFrameImageLz4 for each tile in sequence and check each result. The complete frame appears after the last tile succeeds.
- If a tile fails, rebuild the entire frame with a new frameId. Do not skip failed tiles or send tiles of the same frame concurrently with Promise.all.
Reference files in the toolkit#
- PhoneSDK/packages/web-sdk/src/index.d.ts
- PhoneSDK/examples/permission-debug
- PhoneSDK/examples/audio-capture-lab
- PhoneSDK/docs/web-plugin/runtime-and-lifecycle.md