-
Notifications
You must be signed in to change notification settings - Fork 4
/
microbit.originalWorks.js
120 lines (106 loc) · 3.56 KB
/
microbit.originalWorks.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
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
//https://makecode.microbit.org/_TVDUghHuMEMV
// https://ladvien.com/bluetooth-low-energy-javascript/
var microbit_Characteristic = null;
var aaa = {
name: "",
service: {
name: "buttons",
uuid: "e95d9882-251d-470a-a062-fa1922dfa9a8"
},
characteristics: [{
name: "readButtonA",
uuid: "e95dda90-251d-470a-a062-fa1922dfa9a8",
read: function () {}
},
{
name: "readButtonB",
uuid: "e95dda91-251d-470a-a062-fa1922dfa9a8",
read: function () {}
}
]
};
function microbit_connect() {
let serviceUuid = "e95d9882-251d-470a-a062-fa1922dfa9a8";
let characteristicUuid = "e95dda91-251d-470a-a062-fa1922dfa9a8";
navigator.bluetooth.requestDevice({
// filters: [{
// services: [serviceUuid]
// }],
acceptAllDevices: true
, optionalServices: [serviceUuid]
})
.then(device => {
return device.gatt.connect();
})
.then(server => {
return server.getPrimaryService(serviceUuid);
})
.then(service => {
service.getCharacteristic("e95dda90-251d-470a-a062-fa1922dfa9a8")
.then(characteristic => {
return characteristic.startNotifications()
.then(_ => {
characteristic.addEventListener(
'characteristicvaluechanged',
function (event) {
let value = event.target.value;
var temp = value.getUint8(0);
document.getElementById('microbit_valueA').value = temp;
log(temp);
}
);
});
});
service.getCharacteristic("e95dda91-251d-470a-a062-fa1922dfa9a8")
.then(characteristic => {
return characteristic.startNotifications()
.then(_ => {
characteristic.addEventListener(
'characteristicvaluechanged',
function (event) {
let value = event.target.value;
var temp = value.getUint8(0);
document.getElementById('microbit_valueB').value = temp;
log(temp);
}
);
});
});
})
.catch(error => {
log('Error! ' + error);
});
}
function microbit_disconnect() {
if (microbit_Characteristic) {
microbit_Characteristic.stopNotifications()
.then(_ => {
microbit_Characteristic.removeEventListener(
'characteristicvaluechanged',
hearRate_read);
})
.catch(error => {
log('Error! ' + error);
});
}
}
/*
Readings from BLE Peripheral Simulator:
I think the byte values are wrong. Need to shift by one more?
val Actual Expect
0: 14, 84
1: 0, 7f 80, 3f
00000000 01111111
10000000 00111111
2: 0, 80 00, 40
3: 40, 80 40, 40
50: 48, 84 48 42
01001000 10000100
01001000 01000110
100: 48, 85 c8 42
01001000 10000101
11001000 01000010
200: 48, 86 48, 43
0, 0, 0, 0, 7f,
0, 0, 0, 80, 3f,
*/