JavaScript & Browser APIs

Web Bluetooth Control Guide

Control LEGO smart hubs directly from your web browser. No apps, no installs — just pure JavaScript and the power of Web Bluetooth API.

No App Required
Works in Browser
Real-time Control
lego-hub.js
const hub = await connectHub();
await hub.setMotorSpeed(50);
// 🎉 Motor running!
Connected

Browser Compatibility

Web Bluetooth requires a supported browser

What is Web Bluetooth?

A browser API that lets web pages communicate with Bluetooth Low Energy devices — no app installation required

Zero Installation

No apps to download or install. Works directly in your web browser.

Cross-Platform

Works on Windows, Mac, Linux, ChromeOS, and Android devices.

Custom Interfaces

Build your own control interfaces with HTML, CSS, and JavaScript.

API Integration

Combine with other Web APIs for powerful automation and IoT projects.

Compatible LEGO Hubs

Powered UP Hub

88009

Technic Hub

88012

City Hub

88016

SPIKE Prime Hub

45678

Robot Inventor Hub

88015

Boost Move Hub

88006

Getting Started Tutorial

Follow these steps to connect and control your LEGO hub from JavaScript

1

Request Bluetooth Device

Request a connection to a LEGO hub — the user will see a browser popup to select their device

JavaScript
// Request connection to a LEGO hub
const device = await navigator.bluetooth.requestDevice({
  filters: [
    { namePrefix: 'LEGO' },
    { services: ['00001623-1212-efde-1623-785feabcd123'] }
  ],
  optionalServices: ['00001623-1212-efde-1623-785feabcd123']
});

console.log('Connected to:', device.name);
2

Connect to GATT Server

Connect to the hub's GATT server and get the characteristic for sending commands

JavaScript
// Connect to GATT server
const server = await device.gatt.connect();

// Get the LEGO Hub service
const service = await server.getPrimaryService(
  '00001623-1212-efde-1623-785feabcd123'
);

// Get the characteristic for sending commands
const characteristic = await service.getCharacteristic(
  '00001624-1212-efde-1623-785feabcd123'
);
3

Send Motor Commands

Send commands to control motors — here's how to start a motor on port A at 50% speed

JavaScript
// Command to start motor on port A at 50% speed
// Format: [length, hub_id, message_type, port, start_speed, max_power, end_state, profile]
const command = new Uint8Array([
  0x08,  // Message length
  0x00,  // Hub ID
  0x81,  // Port output command
  0x00,  // Port A
  0x11,  // Start speed subcommand
  0x32,  // Speed: 50 (0x32 = 50)
  0x64,  // Max power: 100
  0x00   // Use profile 0
]);

await characteristic.writeValue(command);
4

Listen for Sensor Data

Subscribe to notifications to receive real-time sensor data from the hub

JavaScript
// Enable notifications
await characteristic.startNotifications();

// Listen for incoming data
characteristic.addEventListener('characteristicvaluechanged', (event) => {
  const data = new Uint8Array(event.target.value.buffer);
  console.log('Received:', Array.from(data).map(b => b.toString(16)));

  // Parse sensor data based on message type
  const messageType = data[2];
  if (messageType === 0x45) {  // Sensor value message
    const port = data[3];
    const value = data[4];
    console.log(`Port ${port} sensor value: ${value}`);
  }
});

Complete Example

A reusable JavaScript class that wraps all the Bluetooth functionality. Copy this into your project to get started quickly.

Connection management
Motor speed control
Clean disconnect
LegoHub.js
class LegoHub {
  constructor() {
    this.device = null;
    this.characteristic = null;
  }

  async connect() {
    this.device = await navigator.bluetooth.requestDevice({
      filters: [{ namePrefix: 'LEGO' }],
      optionalServices: ['00001623-1212-efde-1623-785feabcd123']
    });

    const server = await this.device.gatt.connect();
    const service = await server.getPrimaryService('00001623-1212-efde-1623-785feabcd123');
    this.characteristic = await service.getCharacteristic('00001624-1212-efde-1623-785feabcd123');

    return this.device.name;
  }

  async setMotorSpeed(port, speed) {
    // Speed: -100 to 100
    const speedByte = speed < 0 ? 256 + speed : speed;
    const command = new Uint8Array([0x08, 0x00, 0x81, port, 0x11, speedByte, 0x64, 0x00]);
    await this.characteristic.writeValue(command);
  }

  async stopMotor(port) {
    await this.setMotorSpeed(port, 0);
  }

  disconnect() {
    if (this.device && this.device.gatt.connected) {
      this.device.gatt.disconnect();
    }
  }
}

// Usage
const hub = new LegoHub();
await hub.connect();
await hub.setMotorSpeed(0, 50);  // Port A at 50% speed
await new Promise(r => setTimeout(r, 2000));  // Wait 2 seconds
await hub.stopMotor(0);

Resources & Documentation

Everything you need to build amazing LEGO Bluetooth projects

Ready to Control Your LEGO Hub?

Try our interactive demo right now — connect your LEGO hub and control motors directly from your browser. No installation required!