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) {
|
async function connect(props) {
|
||||||
const device = await navigator.bluetooth.requestDevice({
|
const device = await navigator.bluetooth.requestDevice({
|
||||||
filters: [{ services: ["cycling_speed_and_cadence"] }],
|
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");
|
console.log(`%c\n👩🏼⚕️`, "font-size: 82px;", "Starting CSC...\n\n");
|
||||||
const server = await device.gatt.connect();
|
const server = await device.gatt.connect();
|
||||||
|
await delay(500);
|
||||||
|
console.log("Getting Service...");
|
||||||
const service = await server.getPrimaryService("cycling_speed_and_cadence");
|
const service = await server.getPrimaryService("cycling_speed_and_cadence");
|
||||||
const char = await service.getCharacteristic("csc_measurement");
|
console.log("Getting Characteristic...");
|
||||||
char.oncharacteristicvaluechanged = props.onChange;
|
const characteristic = await service.getCharacteristic("csc_measurement");
|
||||||
char.startNotifications();
|
await characteristic.startNotifications();
|
||||||
return char;
|
console.log("> Notifications started");
|
||||||
|
characteristic.addEventListener("characteristicvaluechanged", props.onChange);
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseCSC(e) {
|
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 hasWheel = !!(flags & 0x01);
|
||||||
const hasCrank = !!(flags & 0x02);
|
const hasCrank = !!(flags & 0x02);
|
||||||
let index = 1;
|
let index = 1;
|
||||||
|
|||||||
Reference in New Issue
Block a user