Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
175 lines (156 sloc) 6.05 KB
<!DOCTYPE html>
<html>
<head>
<title>Bluetooth experiment with Thingy 52 - LED</title>
<meta charset="utf-8">
<!--
https://nordicsemiconductor.github.io/Nordic-Thingy52-FW/documentation/
-->
<script>
const serviceUUID = 'ef680100-9b35-4933-9b10-52ffa9740042';
const optionalServicesUUID = [
"ef680200-9b35-4933-9b10-52ffa9740042",
"ef680300-9b35-4933-9b10-52ffa9740042",
"ef680400-9b35-4933-9b10-52ffa9740042",
"ef680500-9b35-4933-9b10-52ffa9740042"
];
//UI
const uiServicesUUID = 'ef680300-9b35-4933-9b10-52ffa9740042';
const buttonCharacteristicUUID = 'ef680302-9b35-4933-9b10-52ffa9740042';
const ledCharacteristicUUID = 'ef680301-9b35-4933-9b10-52ffa9740042';
//Battery
const batteryServiceUUID = '180f';
const batteryCharacteristicUUID = '2a19';
var bleDevice;
var bleServer;
var uiService;
var ledChar;
window.onload = function() {
document.querySelector('#connect').addEventListener('click', connect);
document.querySelector('#disconnect').addEventListener('click', disconnect);
document.querySelector('#ledOff').addEventListener('click', LEDOff);
document.querySelector('#ledConstant').addEventListener('click', LEDConstantRGB);
document.querySelector('#ledBreathe').addEventListener('click', LEDBreathe);
document.querySelector('#ledOneshot').addEventListener('click', LEDOneShot);
};
function connect() {
if (!navigator.bluetooth) {
return log('Web Bluetooth API is not available in browser.');
}
log('=============================');
log('Requesting Bluetooth Device...');
// RequestDevice must be in click handler (or other user inited events)
navigator.bluetooth.requestDevice({
filters: [{
services: [serviceUUID],
//optionalServices: optionalServicesUUID
//"name": "Thingy"
}]
})
.then(device => {
bleDevice = device;
console.log({
Name: device.name,
Id: device.id,
UUIDs: device.uuids,
Device: device
});
log('Connected to device: ' + device.name);
return device.gatt.connect();
})
.then(server => {
bleServer = server;
log('Found Server', server);
})
.then(() => bleServer.getPrimaryService(uiServicesUUID))
.then(service => {
log('Found UI service', service);
uiService = service;
})
.then(() => uiService.getCharacteristic(ledCharacteristicUUID))
.then(characteristic => {
log('Found LED characteristic');
ledChar = characteristic;
})
.then(() => {
document.getElementById("ledButtons").classList.remove('hidden');
log('=========== READY ===========');
})
.catch(error => {
log('Connection Error: ' + error);
});
}
function disconnect() {
if (!bleDevice) {
log('No Bluetooth Device found...');
return;
}
log('Disconnecting from Bluetooth Device...');
if (bleDevice.gatt.connected) {
bleDevice.gatt.disconnect();
log('Bluetooth Device connected: ' + bleDevice.gatt.connected);
} else {
log('Bluetooth Device is already disconnected');
}
}
function LEDOff(event) {
if (!ledChar) return;
const mode = 0
const dataArray = new Uint8Array([mode])
log("LED off", dataArray)
ledChar.writeValue(dataArray)
}
function LEDConstantRGB(event) {
if (!ledChar) return;
const mode = 1;
const red = 255;
const green = 0;
const blue = 128;
const dataArray = new Uint8Array([mode, red, green, blue])
log("LED constant color: rgb(" + red + "," + green + "," + blue + ")", dataArray)
ledChar.writeValue(dataArray)
}
function LEDBreathe(event) {
if (!ledChar) return
const mode = 2
const predefinedColor = 4
const intensity = 50
const delay = 500
const dataArray = new Uint8Array([mode, predefinedColor, intensity, delay & 0xFF, (delay >> 8) & 0xFF])
log("LED Breathe mode: ", dataArray)
ledChar.writeValue(dataArray)
}
function LEDOneShot(event) {
if (!ledChar) return;
const mode = 3
const predefinedColor = 2
const intensity = 90
const dataArray = new Uint8Array([mode, predefinedColor, intensity])
log("LED one shot breath", dataArray);
ledChar.writeValue(dataArray);
}
function log(text) {
console.log(arguments);
const logElm = document.querySelector('#log');
logElm.textContent = text + '\n' + logElm.textContent;
}
</script>
<style>
.hidden {
display: none;
}
</style>
</head>
<body>
<button id="connect">Connect</button>
<button id="disconnect">Disonnect</button>
<div id="ledButtons" class="hidden">
<h3>LED</h3>
<button id="ledOff">LED off</button>
<button id="ledConstant">LED Constant</button>
<button id="ledBreathe">LED Breathe</button>
<button id="ledOneshot">LED One Shot</button>
</div>
<div><pre id="log"></pre></div>
</body>
</html>