Lesson 09 The Night Shift · Web Bluetooth
JavaScript · drive a hub from the browser
Control LEGO smart hubs directly from your web browser. No apps, no installs: just pure JavaScript and the power of Web Bluetooth API.
const hub = await connectHub();
await hub.setMotorSpeed(50);
// Motor running!
A browser API that lets web pages communicate with Bluetooth Low Energy devices, no app installation required.
No apps to download or install. Works directly in your web browser.
Works on Windows, Mac, Linux, ChromeOS, and Android devices.
Build your own control interfaces with HTML, CSS, and JavaScript.
Combine with other Web APIs for powerful automation and IoT projects.
Follow these steps to connect and control your LEGO hub from JavaScript
Request a connection to a LEGO hub: the user will see a browser popup to select their device
// 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);
Connect to the hub's GATT server and get the characteristic for sending commands
// 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'
);
Send commands to control motors: here's how to start a motor on port A at 50% speed
// Command to start motor on port A at 50% speed
// Format: [length, hub_id, message_type, port, startup, sub_command, speed, max_power, profile]
const command = new Uint8Array([
0x09, // Message length
0x00, // Hub ID
0x81, // Port output command
0x00, // Port A
0x11, // Execute immediately, with command feedback
0x07, // StartSpeed sub command
0x32, // Speed: 50 (0x32 = 50)
0x64, // Max power: 100
0x00 // Use profile 0
]);
await characteristic.writeValue(command);
Subscribe to notifications to receive real-time sensor data from the hub
// 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}`);
}
});
A reusable JavaScript class that wraps all the Bluetooth functionality. Copy this into your project to get started quickly.
class LegoHub {
constructor() {
this.device = null;
this.characteristic = null;
}
async connect() {
this.device = await navigator.bluetooth.requestDevice({
filters: [{ services: ['00001623-1212-efde-1623-785feabcd123'] }],
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;
// Port output command, executed at once, StartSpeed (0x07): speed, max power 100, profile 0
const command = new Uint8Array([0x09, 0x00, 0x81, port, 0x11, 0x07, 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);
Everything you need to build amazing LEGO Bluetooth projects
Try our interactive demo right now: connect your LEGO hub and control motors directly from your browser. No installation required!
Launch Bluetooth Demo