-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
148 lines (130 loc) · 6.88 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<html>
<head>
</head>
<body>
<h1>Wahoo BTLE</h1>
<button id="connect">Connect</button>
<pre id="log">
</pre>
<h1 id="instant_power_heading"></h1>
<script>
// Discovering bluetooth devices MUST be triggered by a user gesture for security
connect.addEventListener('pointerup', function(event) {
//navigator.bluetooth.requestDevice({ filters: [{ services: ['battery_service'] }] })
navigator.bluetooth.requestDevice({
filters: [{
namePrefix: 'Wahoo'
}],
optionalServices: [
'0000180a-0000-1000-8000-00805f9b34fb', //Device Information
'00001816-0000-1000-8000-00805f9b34fb', // Cycling Speed & Cadence
'00001818-0000-1000-8000-00805f9b34fb', // Cycling Power
'00001826-0000-1000-8000-00805f9b34fb', // Fitness Machine
]
})
.then(device => device.gatt.connect())
.then(server => {
return Promise.all([
getDeviceInformation(server),
server.getPrimaryService('00001818-0000-1000-8000-00805f9b34fb') // Cycling Speed & Cadence
.then(service => {
return Promise.all([
// service.getCharacteristic('00002a5d-0000-1000-8000-00805f9b34fb') // Sensor location
// .then(characteristic => {
// // Set up event listener for when characteristic value changes.
// characteristic.addEventListener('characteristicvaluechanged', (event) => {
// console.log("Sensor location", characteristic.uuid, event.target.value.getUint8(0) )
// });
// return characteristic.readValue()
// }),
service.getCharacteristic('00002a63-0000-1000-8000-00805f9b34fb') // Cycling Power Measurement (NOTIFY)
.then(characteristic => {
return characteristic.startNotifications().then(_ => {
console.log('> Notifications started');
characteristic.addEventListener('characteristicvaluechanged', (event) => {
let rawMeasurement = event.target.value
characteristic.addEventListener('characteristicvaluechanged', (event) => {
const flagsString = dec2bin(event.target.value.getUint16(0));
const instant_power = event.target.value.getInt16(2, true) // Resolution is 1 watts
instant_power_heading.innerHTML = `${instant_power} watts`
console.log("Cycling Power", flagsString, instant_power )
});
});
});
}),
// service.getCharacteristic('00002a65-0000-1000-8000-00805f9b34fb') // Cycling Power Feature
// .then(characteristic => {
// // Set up event listener for when characteristic value changes.
// characteristic.addEventListener('characteristicvaluechanged', (event) => { console.log("value changed", characteristic.uuid, event.target.value.getUint8(0))});
// return characteristic.readValue()
// }),
// service.getCharacteristic('00002a66-0000-1000-8000-00805f9b34fb') // Cycling Power Control Point
// .then(characteristic => {
// // Set up event listener for when characteristic value changes.
// characteristic.addEventListener('characteristicvaluechanged', (event) => { console.log("value changed", characteristic.uuid, event.target.value.getUint8(0))});
// return characteristic.readValue()
// }),
// service.getCharacteristic('a026e005-0a7d-4ab3-97fa-f1500f9feb8b') // ????
// .then(characteristic => {
// // Set up event listener for when characteristic value changes.
// characteristic.addEventListener('characteristicvaluechanged', (event) => { console.log("value changed", characteristic.uuid, event.target.value.getUint8(0))});
// return characteristic.readValue()
// }),
])
})
]);
})
.catch(error => { console.log(error); });
const logDataView = (labelOfDataSource, key, valueDataView) => {
const hexString = [...new Uint8Array(valueDataView.buffer)].map(b => {
return b.toString(16).padStart(2, '0');
}).join(' ');
const textDecoder = new TextDecoder('ascii');
const asciiString = textDecoder.decode(valueDataView.buffer);
console.log(
labelOfDataSource +
' Data: ' + key +
'\n (Hex) ' + hexString +
'\n (ASCII) ' + asciiString
);
};
// From https://stackoverflow.com/a/16155417
function dec2bin(dec){
return (dec >>> 0).toString(2);
}
const getDeviceInformation = (server) => {
return server.getPrimaryService('0000180a-0000-1000-8000-00805f9b34fb')
.then(service => {
return Promise.all([
// Firmware Revision String
service.getCharacteristic('00002a26-0000-1000-8000-00805f9b34fb')
.then(characteristic => {
characteristic.addEventListener('characteristicvaluechanged', (event) => {
logDataView("Firmware Revision", characteristic.uuid, event.target.value )
});
return characteristic.readValue()
}),
// Hardware revision string
service.getCharacteristic('00002a27-0000-1000-8000-00805f9b34fb')
.then(characteristic => {
characteristic.addEventListener('characteristicvaluechanged', (event) => {
logDataView("Hardware Revision", characteristic.uuid, event.target.value )
});
return characteristic.readValue()
}),
// Manufacturer name string
service.getCharacteristic('00002a29-0000-1000-8000-00805f9b34fb')
.then(characteristic => {
// Set up event listener for when characteristic value changes.
characteristic.addEventListener('characteristicvaluechanged', (event) => {
logDataView("Manufacturer", characteristic.uuid, event.target.value )
});
return characteristic.readValue()
}),
])
})
}
});
</script>
</body>
</html>