MENU
Bluetooth
Websites can communicate with nearby Bluetooth devices in a secure and privacy-preserving way. This way, heart rate monitors, singing lightbulbs, and even turtles could interact directly with a website.
Note: this is an experimental technology which may not work in some environments.RESETRUNFULL
<!DOCTYPE html><html><body>
<button onclick="connectBluetooth()">Connect Bluetooth</button>
<script>
function connectBluetooth(){
navigator.bluetooth.getAvailability().then(available => {
if (available)
console.log("This device supports Bluetooth!");
else {
console.log("Doh! Bluetooth is not supported");
return;
}
});
// ......
navigator.bluetooth.requestDevice({
// filters: [{name: 'Francois robot'}],
acceptAllDevices: true,
//optionalServices: ['battery_service']
}).then(device => {
console.log(device.name);
return device.gatt.connect();
}).then(server => {
return server.getPrimaryService('battery_service');
}).then(service => {
return service.getCharacteristic('battery_level');
}).then(characteristic => {
characteristic.addEventListener('characteristicvaluechanged', handleBatteryLevelChanged);
characteristic.writeValue('...');
return characteristic.readValue();
}).then(value => {
console.log(`Battery percentage is ${value.getUint8(0)}`);
}).catch(error => { console.error(error); });
// ......
// navigator.bluetooth.referringDevice
// navigator.bluetooth.onavailabilitychanged = ()=>{};
}
</script></body></html>
This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
More demos can be found here and here.