Skip to main content

Bluetooth.BluetoothCentralModule

Class

Provides access to Bluetooth GATT devices.

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

Provides access to Bluetooth GATT devices. APIs include scanning for and connecting to these devices, and reading and writing to their descriptors and characteristics.

Index

Example

// @input Asset.BluetoothCentralModule bluetoothModule

var bluetoothGatt;
async function scanForGatt() {
var scanFilter = new Bluetooth.ScanFilter();
var scanSetting = new Bluetooth.ScanSettings();
scanSetting.timeoutSeconds = 30;
scanSetting.scanMode = Bluetooth.ScanMode.LowPower;

var scanResult = await script.bluetoothModule.startScan([scanFilter], scanSetting,
function(result) {
print("Running predicate on " + result.deviceName);
return result.deviceName == "[DEVICE NAME]";
}
);

print("Found GATT device " + scanResult.deviceName);

bluetoothGatt = await script.bluetoothModule.connectGatt(scanResult.deviceAddress);
sprint("Connected to GATT device");

bluetoothGatt.onDisconnectedEvent.add(function() {
print("Device disconnected");
});
bluetoothGatt.onMtuChangedEvent.add(function(event) {
print("Mtu changed to " + event.mtu);
});

let services = bluetoothGatt.getServices()
if (services.length == 0) {
print("No services found");
return;
}
print("Services found [" + services.length + "]");

let service = services[0];
let characteristics = service.getCharacteristics();
if (characteristics.length == 0) {
print('No characteristics found');
return;
}
print("Characteristics found [" + characteristics.length + "]");

let characteristic = characteristics[0];
let value = await characteristic.readValue();
print("Read value [" + value + "]");
}

async function run() {
scanForGatt();
return "";
}

await run();

Extends

Properties

name

name: string

The name of the Asset in Lens Studio.

Default Value

""

Inherited from

Asset.name


onBluetoothStatusChangedEvent

readonly onBluetoothStatusChangedEvent: event1<BluetoothStatusChangedEvent, void>

ExperimentalSPECS

Event for Bluetooth status changes. Currently unused.


status

readonly status: BluetoothStatus

ExperimentalSPECS

Get the current status of the Bluetooth adapter.


uniqueIdentifier

readonly uniqueIdentifier: string

Since

Lens Scripting Version 176

Inherited from

Asset.uniqueIdentifier

Methods

connectGatt()

connectGatt(deviceAddress): Promise<BluetoothGatt>

ExperimentalSPECS

Connect to a GATT server on a given device address.

deviceAddress Address to which to connect. Received in Bluetooth.ScanResult.deviceAddress

Returns: Promise resolving to a {Bluetooth.BluetoothGatt} object if successful. The Promise is rejected if the connection cannot be made.

Parameters

ParameterType
deviceAddressUint8Array

Returns

Promise<BluetoothGatt>


getTypeName()

static getTypeName(): TypeName<BluetoothCentralModule>

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

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


startScan()

startScan(filters, settings, predicate): Promise<ScanResult>

ExperimentalSPECS

Start a scan for Bluetooth GATT devices. The first device which passes the predicate will be returned, at which point the scan will stop.

filters Filters to apply to the scan. If a device passes any filter then the predicate will be invoked for that device. If no filters are passed then the predicate will be invoked for all devices.

settings Bluetooth.ScanSettings to configure the scan.

predicate Predicate to select a device. Returning true will stop the scan and return the device.

Returns: Promise resolving to the first device which passes the predicate. The promise is rejected if the scan times out.

var scanFilter = new Bluetooth.ScanFilter();
var scanSettings = new Bluetooth.ScanSettings();
scanSettings.timeoutSeconds = 30;
scanSettings.scanMode = Bluetooth.ScanMode.LowPower;

var scanResult = await script.bluetoothModule.startScan([scanFilter], scanSettings,
function(result) {
print("Running predicate on " + result.deviceName);
return result.deviceName == "[DEVICE NAME]";
}
);

print("Found GATT device " + scanResult.deviceName);

Parameters

ParameterType
filtersScanFilter[]
settingsScanSettings
predicate(result) => boolean

Returns

Promise<ScanResult>


stopScan()

stopScan(): Promise<void>

ExperimentalSPECS

Stop a scan for Bluetooth devices, if one is running.

Returns

Promise<void>