Skip to content

Instantly share code, notes, and snippets.

@clemos
Last active July 4, 2021 12:10
Show Gist options
  • Save clemos/ca4f9bdc3ddbaed79387fd005c7dfc86 to your computer and use it in GitHub Desktop.
Save clemos/ca4f9bdc3ddbaed79387fd005c7dfc86 to your computer and use it in GitHub Desktop.
raspberry bluetooth setup
<html>
<head>
<style>
#log {
display:block;
width: 100%;
height: 200px;
}
</style>
</head>
<body>
<button onclick='onGetBluetoothDevicesButtonClick()'>get devices</button>
<button onclick='onRequestBluetoothDeviceButtonClick()'>request devices</button>
<textarea id="log"></textarea>
<script>
const textarea = document.getElementById('log');
function log(...args){
console.log(...args);
textarea.value += `\n${JSON.stringify(args)}`;
}
function onGetBluetoothDevicesButtonClick() {
log("Getting existing permitted Bluetooth devices...");
navigator.bluetooth
.getDevices()
.then((devices) => {
log("> Got " + devices.length + " Bluetooth devices.");
for (const device of devices) {
log(" > " + device.name + " (" + device.id + ")");
}
})
.catch((error) => {
log("Argh! " + error);
});
}
function onRequestBluetoothDeviceButtonClick() {
log("Requesting any Bluetooth device...");
navigator.bluetooth
.requestDevice({
// filters: [...] <- Prefer filters to save energy & show relevant devices.
acceptAllDevices: true,
})
.then((device) => {
log("> Requested " + device.name + " (" + device.id + ")");
})
.catch((error) => {
log("Argh! " + error);
});
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment