Skip to content

Instantly share code, notes, and snippets.

@kotobuki
Last active August 7, 2024 14:34
Show Gist options
  • Save kotobuki/7c67f8b9361e08930da1a5cfcfb0653f to your computer and use it in GitHub Desktop.
Save kotobuki/7c67f8b9361e08930da1a5cfcfb0653f to your computer and use it in GitHub Desktop.
Web Bluetooth with micro:bit (UART)
// https://lancaster-university.github.io/microbit-docs/resources/bluetooth/bluetooth_profile.html
// An implementation of Nordic Semicondutor's UART/Serial Port Emulation over Bluetooth low energy
const UART_SERVICE_UUID = "6e400001-b5a3-f393-e0a9-e50e24dcca9e";
// Allows the micro:bit to transmit a byte array
const UART_TX_CHARACTERISTIC_UUID = "6e400002-b5a3-f393-e0a9-e50e24dcca9e";
// Allows a connected client to send a byte array
const UART_RX_CHARACTERISTIC_UUID = "6e400003-b5a3-f393-e0a9-e50e24dcca9e";
let uBitDevice;
let rxCharacteristic;
function setup() {
createCanvas(200, 200);
const connectButton = createButton("Connect");
connectButton.mousePressed(connectButtonPressed);
const disconnectButton = createButton("Disconnect");
disconnectButton.mousePressed(disconnectButtonPressed);
const pingButton = createButton("Ping");
pingButton.mousePressed(pingButtonPressed);
}
function draw() {
background(0);
}
async function connectButtonPressed() {
try {
console.log("Requesting Bluetooth Device...");
uBitDevice = await navigator.bluetooth.requestDevice({
filters: [{ namePrefix: "BBC micro:bit" }],
optionalServices: [UART_SERVICE_UUID]
});
console.log("Connecting to GATT Server...");
const server = await uBitDevice.gatt.connect();
console.log("Getting Service...");
const service = await server.getPrimaryService(UART_SERVICE_UUID);
console.log("Getting Characteristics...");
const txCharacteristic = await service.getCharacteristic(
UART_TX_CHARACTERISTIC_UUID
);
txCharacteristic.startNotifications();
txCharacteristic.addEventListener(
"characteristicvaluechanged",
onTxCharacteristicValueChanged
);
rxCharacteristic = await service.getCharacteristic(
UART_RX_CHARACTERISTIC_UUID
);
} catch (error) {
console.log(error);
}
}
function disconnectButtonPressed() {
if (!uBitDevice) {
return;
}
if (uBitDevice.gatt.connected) {
uBitDevice.gatt.disconnect();
console.log("Disconnected");
}
}
async function pingButtonPressed() {
if (!rxCharacteristic) {
return;
}
try {
let encoder = new TextEncoder();
rxCharacteristic.writeValue(encoder.encode("P\n"));
} catch (error) {
console.log(error);
}
}
function onTxCharacteristicValueChanged(event) {
let receivedData = [];
for (var i = 0; i < event.target.value.byteLength; i++) {
receivedData[i] = event.target.value.getUint8(i);
}
const receivedString = String.fromCharCode.apply(null, receivedData);
console.log(receivedString);
if (receivedString === "S") {
console.log("Shaken!");
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/p5.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/addons/p5.dom.min.js"></script>

Web Bluetooth with micro:bit (UART)

This is a demo that illustrates a way to connect a micro:bit and a PC running the Chome web browser using the Web Bluetooth API and send/receive strings using the UART service .

You can get the code for micro:bit at https://makecode.microbit.org/_C06XuChrjRvs

Note: You have to open this Pen in the Debug Mode, since the Web Bluetooth doesn't work on cross-origin iframes.

WebBluetoothCG/web-bluetooth#180 https://bugs.chromium.org/p/chromium/issues/detail?id=518042&desc=2

A Pen by Shigeru Kobayashi on CodePen.

License.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment