Skip to content

Instantly share code, notes, and snippets.

@filipbech
Created October 6, 2016 18:12
Show Gist options
  • Save filipbech/15a01decd592f903a5f307187e78b7c8 to your computer and use it in GitHub Desktop.
Save filipbech/15a01decd592f903a5f307187e78b7c8 to your computer and use it in GitHub Desktop.
Playing around with an BLE LED bulb and web-bluetooth
const serviceId = 0xffe5;
const characteristicId = 0xffe9;
const btn = document.getElementById('btn');
const form = document.forms[0];
let bulb;
document.getElementById('random').addEventListener('click',()=> {
setInterval(()=> {
bulb.writeValue(getColorValue(Math.ceil(Math.random()*255), Math.ceil(Math.random()*255), Math.ceil(Math.random()*255)));
}, 500);
})
function getColorValue(red, green, blue) {
if(red === "255" && green === "255" && blue === "255") {
const strength = 255;
return new Uint8Array([0x56, 00, 00, 00, strength, 0x0f, 0xaa]);
}
return new Uint8Array([0x56, red, green, blue, 0x00, 0xf0, 0xaa]);
}
form.addEventListener('submit', event=> {
event.preventDefault();
const red = event.target.red.value;
const green = event.target.green.value;
const blue = event.target.blue.value;
const value = getColorValue(red, green, blue);
bulb.writeValue(value);
return false;
});
btn.addEventListener('click', ()=> {
navigator.bluetooth.requestDevice({
filters: [{
services: [serviceId],
name:'LEDBLE-A6E811E9'
}]
})
.then(device => {
device.gatt.connect()
.then(server => server.getPrimaryService(serviceId))
.then(service => service.getCharacteristic(characteristicId))
.then(characteristic => {
bulb = characteristic;
form.style.display = 'block';
btn.style.display = 'none';
})
})
.catch(error => {
console.log(error);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment