Lens Scripting API
    Preparing search index...

    Class SpectaclesMobileKitModuleWearable Only

    SpectaclesMobileKitModule serves as the primary entry point for the Spectacles Mobile Kit, enabling interaction with a specific mobile application, such as requesting data or receiving events.

    ⚠️ Best Practice: Only one SpectaclesMobileKitModule should be used per Lens.

    ⚠️ Privacy Note: Using SpectaclesMobileKitModule requires the INTERNET permission. By default, access to sensitive data is restricted when a Lens uses internet-connected components. To enable access to both sensitive data and the internet simultaneously, certain Experimental APIs must be activated through a feature called Extended Permissions. Note that Lenses using Extended Permissions cannot be published.

    Example usage of the SpectaclesMobileKitModule to establish a session, send/receive data, handle subscriptions, and load remote media assets (textures and GLTF meshes).

    //@input bool resetAfterDelay = false
    //@input Components.Image image
    //@input SceneObject gltfContainer
    //@input Assets.Material gltfMaterial
    //@input Asset.SpectaclesMobileKitModule module

    const module = script.module;
    const myImage = script.image;
    myImage.mainMaterial = myImage.mainMaterial.clone();
    const mainPass = myImage.mainPass;

    const myText = script.getSceneObject().getComponent('Component.Text');

    const appendLine = (txt) => {
    print(txt);
    myText.text += `\n${txt}`;
    };

    const createSessionAsync = async (onDisconnect) => new Promise((res, rej) => {
    const session = module.createSession();
    session.onDisconnected.add(onDisconnect);
    session.onConnected.add(() => {
    res(session);
    });
    session.start();
    });

    appendLine('Script Started');

    try {
    appendLine('Awaiting connection');

    if (script.resetAfterDelay) {
    const delay = script.createEvent('DelayedCallbackEvent');
    delay.bind(() => {
    appendLine('Stopping the session');
    if (script.session) {
    script.session.close();
    script.session = null;
    }
    });
    delay.reset(10);
    }

    script.session = await createSessionAsync(() => {
    appendLine('Disconnected');
    });

    const session = script.session;
    appendLine('Client Connected');

    // Send one-way data (fire-and-forget)
    session.sendData('test data');
    appendLine('Sent data');

    // Request app digest to verify trustworthiness
    try {
    const response = await session.sendRequest('app://digest');
    appendLine(`Digest: ${response}`);
    } catch (error) {
    appendLine(`Error: ${error}`);
    }

    // Standard request-response
    try {
    const response = await session.sendRequest('echo me back');
    appendLine(`Response: ${response}`);
    } catch (error) {
    appendLine(`Error: ${error}`);
    }

    // Subscribe to a topic
    const subscription = session.startSubscription(
    'hello world times',
    (error) => {
    appendLine(`Subscription error: ${error}`);
    }
    );
    subscription.add((response) => {
    appendLine(`Subscription response: ${response}`);
    });

    // Load remote texture
    const textureId = 'spectacleskit://test.png';
    appendLine(`Loading asset: ${textureId}`);
    const remoteMediaModule = require("LensStudio:RemoteMediaModule");
    remoteMediaModule.loadAsImageTexture(textureId, (texture) => {
    appendLine('Texture loaded');
    mainPass.baseTex = texture;
    }, (error) => {
    appendLine(`Error loading asset: ${error}`);
    });

    // Load remote GLTF mesh
    const meshId = 'spectacleskit://test.glb';
    appendLine(`Loading asset: ${meshId}`);
    remoteMediaModule.loadAsGltfAsset(meshId, (asset) => {
    appendLine('Mesh loaded');
    asset.tryInstantiate(script.gltfContainer, script.gltfMaterial);
    // TODO: manipulate new object inside script.gltfContainer
    }, (error) => {
    appendLine(`Error loading asset: ${error}`);
    });
    } catch (error) {
    appendLine(`Spectacles Kit is not available: ${error}`);
    }

    Hierarchy (View Summary)

    • Asset
      • SpectaclesMobileKitModule
    Index

    Properties

    name: string

    The name of the Asset in Lens Studio.

    uniqueIdentifier: string

    Methods

    • Returns the name of this object's type.

      Returns string

    • Returns true if the object matches or derives from the passed in type.

      Parameters

      • type: string

      Returns boolean

    • Returns true if this object is the same as other. Useful for checking if two references point to the same thing.

      Parameters

      Returns boolean