Skip to main content

SpectaclesMobileKitModule

Class

SpectaclesMobileKitModule serves as the primary entry point for the Spectacles Mobile Kit, enabling interaction with a specific mobile application, such as r…

  • Snapchat: not supported
  • SPECS: fully supported
  • Camera Kit Web: not supported
  • Camera Kit Mobile: not supported
  • SnapchatNot supported — Available on SPECS only
  • SPECSFully supported
  • Camera Kit WebNot supported — Available on SPECS only
  • Camera Kit MobileNot supported — Available on SPECS only

Platform details

  • Snapchat — Not supported — Available on SPECS only.

  • SPECS — Fully supported.

  • Camera Kit Web — Not supported — Available on SPECS only.

  • Camera Kit Mobile — Not supported — Available on SPECS 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.

Index

Example

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}`);
}

Extends

Properties

name

name: string

The name of the Asset in Lens Studio.

Default Value

""

Inherited from

Asset.name


uniqueIdentifier

readonly uniqueIdentifier: string

Since

Lens Scripting Version 176

Inherited from

Asset.uniqueIdentifier

Methods

createSession()

createSession(): SpectaclesMobileKitSession

SPECS

Creates a SpectaclesMobileKitSession instance. To begin interacting with your mobile application, a Lens must first start a session. Once the mobile application connects to the session, the Lens can start communicating with it.

Returns

SpectaclesMobileKitSession


getTypeName()

static getTypeName(): TypeName<SpectaclesMobileKitModule>

SPECS

Returns the fully-qualified JavaScript type name for this class (for example, "Component.Camera"). This is a static accessor (no instance required) and returns the same value as the instance getTypeName().

Returns

TypeName<SpectaclesMobileKitModule>

Overrides

Asset.getTypeName


getTypeName()

getTypeName(): string

Returns the name of this object's type.

Returns

string

Inherited from

Asset.getTypeName


isOfType()

isOfType(type): boolean

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

Parameters

ParameterType
typestring

Returns

boolean

Inherited from

Asset.isOfType


isSame()

isSame(other): boolean

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

Parameters

ParameterType
otherScriptObject

Returns

boolean

Inherited from

Asset.isSame