You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
2.8 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

const startButton = document.getElementById("start");
console.log(navigator.bluetooth);
const devices = await navigator.bluetooth.getDevices();
devices.forEach(async (device) => {
const gattServer = await device.gatt.connect();
const primaryService = await gattServer.getPrimaryService(0x1800);
const MANUFACTURER_UUID = 0x2a29;
const MODEL_UUID = 0x2a0a;
const char = await primaryService.getCharacteristic(MODEL_UUID);
const val = await char.readValue();
//const char2 = await primaryService.getCharacteristic(MODEL_UUID);
//const val2 = await char2.readValue();
const decoder = new TextDecoder("utf-8");
const manufacturer = decoder.decode(val);
//const model = decoder.decode(val2);
console.log("Manufacturer:", manufacturer);
});
startButton.addEventListener("click", async () => {
try {
const devices = await navigator.bluetooth.getDevices();
console.log(2, devices);
const device = await navigator.bluetooth.requestDevice({
filters: [{ services: [0x180d] }],
optionalServices: [
0x1818, // Cycling Power watts, torque, crank torque, etc.
0x1816, // Cycling Speed & Cadence speed, cadence, distance
0x180d, // Heart Rate optional, if the trainer includes an HR sensor
0x180f, // Battery Service trainer battery level (if batterypowered)
0x180a, // Device Information manufacturer, model, firmware version
0x1800,
// add any custom 128bit UUIDs as strings, e.g.
// '0000abcd-0000-1000-8000-00805f9b34fb'
],
acceptAllDevices: true,
});
const server = await device.gatt.connect();
console.log(await server.getPrimaryServices());
} catch (error) {
console.log(error);
}
});
// startButton.addEventListener("click", async () => {
// try {
// const devices = await navigator.bluetooth.getDevices();
// console.log(2, devices);
// const device = await navigator.bluetooth.requestDevice({
// //filters: [{ services: ["heart_rate"] }],
// optionalServices: [
// 0x1818, // Cycling Power watts, torque, crank torque, etc.
// 0x1816, // Cycling Speed & Cadence speed, cadence, distance
// 0x180d, // Heart Rate optional, if the trainer includes an HR sensor
// 0x180f, // Battery Service trainer battery level (if batterypowered)
// 0x180a, // Device Information manufacturer, model, firmware version
// 0x1800,
// // add any custom 128bit UUIDs as strings, e.g.
// // '0000abcd-0000-1000-8000-00805f9b34fb'
// ],
// acceptAllDevices: true,
// });
// const server = await device.gatt.connect();
// console.log(await server.getPrimaryServices());
// } catch (error) {
// console.log(error);
// }
// });