Lens Scripting API
    Preparing search index...

    Class BluetoothCentralModule

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

    // @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.onConnectionStateChangedEvent.add(function(event) {
    print("Connection state changed to " + event.state);
    });
    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();

    Hierarchy (View Summary)

    • Asset
      • BluetoothCentralModule
    Index

    Properties

    name: string

    The name of the Asset in Lens Studio.

    onBluetoothStatusChangedEvent: event1<BluetoothStatusChangedEvent, void>

    Event for Bluetooth status changes. Currently unused.

    Get the current status of the Bluetooth adapter.

    uniqueIdentifier: string

    Methods

    • Experimental Wearable Only

      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

      • deviceAddress: Uint8Array

      Returns Promise<BluetoothGatt>

    • 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

    • Experimental Wearable Only

      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

      Returns Promise<ScanResult>

    • Experimental Wearable Only

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

      Returns Promise<void>