Skip to content

Instantly share code, notes, and snippets.

@scytacki
Created October 10, 2017 03:57
Show Gist options
  • Save scytacki/a675a1cc1fb28d8326a2127bb9a60ca8 to your computer and use it in GitHub Desktop.
Save scytacki/a675a1cc1fb28d8326a2127bb9a60ca8 to your computer and use it in GitHub Desktop.
device = // Step 1: ask for a device
await navigator.bluetooth.requestDevice({
filters: [{ services: [tagIdentifier] }],
optionalServices: [serviceAddr]
});
server = // Step 2: Connect to device
await device.gatt.connect();
deviceConnected(server);
service = // Step 3: Get the Service
await server.getPrimaryService(serviceAddr);
enableChar = // Step 4: Enable Light Sensor
await service.getCharacteristic(enableAddr);
await enableChar.writeValue(new Uint8Array([0x01]));
valueChar = // Step 5: Get light characteristic
await service.getCharacteristic(valueAddr);
// Step 6: Loop every 600ms
readIntervalID = setInterval(async () => {
byteArray = // Step 7: Read bytes
await valueChar.readValue();
// Step 8: display light
displayLight(byteArray.getUint16(0, true));
},1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment