-
Notifications
You must be signed in to change notification settings - Fork 32
/
utils.js
58 lines (42 loc) · 2.04 KB
/
utils.js
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
/* Check out https://www.npmjs.com/package/web-bluetooth-utils as well */
/* BluetoothRemoteGATTCharacteristic helpers */
BluetoothRemoteGATTCharacteristic.prototype.getFloat32Value = function(byteOffset, littleEndian = true) {
return this.value.getFloat32(byteOffset, littleEndian);
};
BluetoothRemoteGATTCharacteristic.prototype.getFloat64Value = function(byteOffset, littleEndian = true) {
return this.value.getFloat64(byteOffset, littleEndian);
};
BluetoothRemoteGATTCharacteristic.prototype.getInt16Value = function(byteOffset, littleEndian = true) {
return this.value.getInt16(byteOffset, littleEndian);
};
BluetoothRemoteGATTCharacteristic.prototype.getInt32Value = function(byteOffset, littleEndian = true) {
return this.value.getInt32(byteOffset, littleEndian);
};
BluetoothRemoteGATTCharacteristic.prototype.getInt8Value = function(byteOffset) {
return this.value.getInt8(byteOffset);
};
BluetoothRemoteGATTCharacteristic.prototype.getStringValue = function(encoding = 'utf8') {
var decoder = new TextDecoder(encoding);
return decoder.decode(this.value);
};
BluetoothRemoteGATTCharacteristic.prototype.getUint16Value = function(byteOffset, littleEndian = true) {
return this.value.getUint16(byteOffset, littleEndian);
};
BluetoothRemoteGATTCharacteristic.prototype.getUint32Value = function(byteOffset, littleEndian = true) {
return this.value.getUint32(byteOffset, littleEndian);
};
BluetoothRemoteGATTCharacteristic.prototype.getUint8Value = function(byteOffset) {
return this.value.getUint8(byteOffset);
};
/* BluetoothRemoteGATTService helpers */
(function() {
let nativeGetCharacteristics = BluetoothRemoteGATTService.prototype.getCharacteristics;
let getCharacteristics = function(characteristics) {
if (characteristics instanceof Array) {
let promises = characteristics.map(characteristic => this.getCharacteristic(characteristic));
return Promise.all(promises);
}
return nativeGetCharacteristics.apply(this, [characteristics]);
}
window.BluetoothRemoteGATTService.prototype.getCharacteristics = getCharacteristics;
})();