Web Bluetooth API Integration: Connect the Web with Nearby Devices

Web Bluetooth API

Originally published on Makemychance.com

The Web Bluetooth API bridges the gap between web applications and nearby Bluetooth Low Energy (BLE) devices, allowing your website to connect, read, and write data to real-world hardware without requiring a native app.

If you’ve ever wanted to control smart devices, fitness bands, or custom IoT hardware directly from a website, the Web Bluetooth API makes it possible — all in JavaScript.

In this guide, we’ll walk through the key concepts, permissions, and code examples to help you get started.


🚀 What is the Web Bluetooth API?

The Web Bluetooth API lets a browser communicate with nearby BLE devices using GATT (Generic Attribute Profile). It works securely, requiring HTTPS and user interaction (like clicking a button) to initiate device discovery.

Supported browsers:

  • ✅ Chrome (desktop & Android)
  • ❌ Not supported in Safari (including iOS) or Firefox

🔐 Why Use Web Bluetooth?

  • No need to build a native app
  • Works directly from the browser
  • Great for IoT, healthcare, wearables, robotics
  • Allows reading/writing to characteristics (sensors, commands, etc.)

🧪 Basic Flow of Web Bluetooth

  1. Request a device
  2. Connect to GATT Server
  3. Get Service
  4. Get Characteristic
  5. Read/Write or Subscribe

🧑‍💻 Code Example: Connect to a Bluetooth Device

<button id="connectBtn">Connect to Bluetooth Device</button>

<script>
  document.getElementById('connectBtn').addEventListener('click', async () => {
    try {
      const device = await navigator.bluetooth.requestDevice({
        filters: [{ services: ['battery_service'] }]
      });

      const server = await device.gatt.connect();
      const service = await server.getPrimaryService('battery_service');
      const characteristic = await service.getCharacteristic('battery_level');

      const value = await characteristic.readValue();
      const batteryLevel = value.getUint8(0);

      console.log(`Battery level is ${batteryLevel}%`);
      alert(`Battery level: ${batteryLevel}%`);
    } catch (error) {
      console.error('Bluetooth connection failed:', error);
    }
  });
</script>

🔄 Subscribing to Notifications (e.g. Heart Rate Monitor)

const characteristic = await service.getCharacteristic('heart_rate_measurement');

characteristic.addEventListener('characteristicvaluechanged', event => {
  const value = event.target.value;
  const heartRate = value.getUint8(1);
  console.log('Heart Rate:', heartRate);
});

await characteristic.startNotifications();

⚠️ Things to Keep in Mind

  • Works only on HTTPS domains (except localhost)
  • Requires user gesture (like a click) to trigger device scan
  • Device must support BLE (Bluetooth Low Energy)
  • API is still experimental — test thoroughly

🌐 Real-World Use Cases

  • Fitness trackers (e.g., step count, heart rate)
  • Smart home devices (e.g., light control)
  • Industrial IoT (e.g., sensors, scanners)
  • Educational robotics and toys

✅ Conclusion

The Web Bluetooth API unlocks the ability to create powerful, interactive web applications that can talk to nearby hardware — all without leaving the browser. Whether you’re building a health tracker dashboard, a smart home controller, or experimenting with Arduino/BLE devices, this API opens up exciting possibilities.

As always, test compatibility across devices, and ensure secure, permission-aware implementations.


API Integration Best Practices – A Developer’s Guide

Web MIDI Api Usage-Complete Guide

🛠️ Solving “Deprecation Warning [legacy-js-api]” in Dart Sass While Building a React Project