Add delay after connecting to BLE device and start notifications before adding on characteristic change event listener
This commit is contained in:
@@ -1,3 +1,7 @@
|
||||
function delay(ms) {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
async function connect(props) {
|
||||
const device = await navigator.bluetooth.requestDevice({
|
||||
filters: [{ services: ["cycling_speed_and_cadence"] }],
|
||||
@@ -5,15 +9,19 @@ async function connect(props) {
|
||||
});
|
||||
console.log(`%c\n👩🏼⚕️`, "font-size: 82px;", "Starting CSC...\n\n");
|
||||
const server = await device.gatt.connect();
|
||||
await delay(500);
|
||||
console.log("Getting Service...");
|
||||
const service = await server.getPrimaryService("cycling_speed_and_cadence");
|
||||
const char = await service.getCharacteristic("csc_measurement");
|
||||
char.oncharacteristicvaluechanged = props.onChange;
|
||||
char.startNotifications();
|
||||
return char;
|
||||
console.log("Getting Characteristic...");
|
||||
const characteristic = await service.getCharacteristic("csc_measurement");
|
||||
await characteristic.startNotifications();
|
||||
console.log("> Notifications started");
|
||||
characteristic.addEventListener("characteristicvaluechanged", props.onChange);
|
||||
}
|
||||
|
||||
function parseCSC(e) {
|
||||
const flags = e.target.value.getUint8(0, true);
|
||||
const value = e.target.value;
|
||||
const flags = value.getUint8(0, true);
|
||||
const hasWheel = !!(flags & 0x01);
|
||||
const hasCrank = !!(flags & 0x02);
|
||||
let index = 1;
|
||||
|
||||
Reference in New Issue
Block a user