BLEMeshJS
Guides

Control Device

Connect to a provisioned node and send a real model command.

Use this when your target device is already provisioned and you want to verify command flow end to end.

Before you start

  • You completed Setup Runtime.
  • At least one device is already provisioned.

1. Discover and connect to a provisioned proxy

const proxy = await new Promise((resolve, reject) => {
  const offFound = mesh.connection.on("scan:new-proxy", (candidate) => {
    mesh.connection.stopScan();
    offFound();
    offError();
    resolve(candidate);
  });

  const offError = mesh.connection.on("ble:error", (error) => {
    offFound();
    offError();
    reject(error);
  });

  mesh.connection.scan({ timeout: 10_000, notifyOnWaitingForAdvertisements: true });
});

await mesh.connection.connect(proxy);

⚠️ Gotcha

Web Bluetooth scanning is browser-mediated, so expect chooser UX rather than silent passive discovery.

✅ Best practice

Keep one shared mesh manager in memory for the app session to avoid repeated transport setup.

2. Send a Generic OnOff command

import { GenericOnOff, SigModelId } from "@blemeshjs/sdk";

const node = mesh.configuredNodes?.[0];
if (!node) throw new Error("No configured node found");

const onOffModel = mesh.getModel(node.uuid.uuidString, 0, SigModelId.genericOnOffServerModelId);
if (!onOffModel) throw new Error("Generic OnOff model not found on element 0");

const onOff = onOffModel.use(GenericOnOff);
await onOff.get();
await onOff.set(!Boolean(onOff.state), { acknowledged: true });

This reads the current state and then toggles it, so your test command always does something visible.

3. Validate expected behavior

  • The target device changes state.
  • No ble:error is emitted during command flow.
  • Your app remains connected after the command.

⚠️ Gotcha

If onOff.set(...) throws an access error, the model may not be bound to an application key yet. Complete model/key configuration before retrying.

💡 Pro tip

Always test with acknowledged commands first. Once command flow is stable, move latency-sensitive paths to unacknowledged messages where appropriate.

At this point, you should have verified real command delivery over a live proxy connection.

What to do next

Next, you will want to review Advanced for custom runtime control and API Reference for exact signatures.

On this page