-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbluetooth.js
79 lines (72 loc) · 2.5 KB
/
bluetooth.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
function connectToBle() {
// Connect to a device by passing the service UUID
blueTooth.connect("a5f125c0-7cec-4334-9214-58cffb8706c0", gotCharacteristics);
console.log('trying to connect');
}
function disconnectBle() {
blueTooth.disconnect();
}
// A function that will be called once got characteristics
function gotCharacteristics(error, characteristics) {
console.log('looking for characteristics');
if (error) {
console.log('error: ', error);
} else {
console.log('characteristics: ', characteristics);
console.log(characteristics.length);
if (characteristics.length != 2) {
return;
}
for (let i = 0; i < 2; i++) {
if (characteristics[i].uuid == 'a5f125c1-7cec-4334-9214-58cffb8706c0') {
blueToothTXCharacteristic = characteristics[i];
console.log('detected first characteristic');
}
if (characteristics[i].uuid == 'a5f125c2-7cec-4334-9214-58cffb8706c0') {
blueToothRXCharacteristic = characteristics[i];
console.log('detected second characteristic');
}
}
isConnected = blueTooth.isConnected();
hidePasswordParam();
blueTooth.startNotifications(blueToothRXCharacteristic, gotValue, 'string');
connectButton.hide();
// Add a event handler when the device is disconnected
blueTooth.onDisconnected(onDisconnected);
}
}
// A function that will be called once got values
function gotValue(value) {
console.log('value: ', value);
let splitString = split(value, ',');
newData = true;
if (splitString[0] == 'pwOk') {//status string
showPasswordParam();
} else if (splitString[0] == 'authFailed'){
statusTitle.style.color = 'red';
statusTitle.textContent = "Authentication failed. Wrong password!";
} else if (splitString[0] == 'sucess'){
if(splitString[1] == 'odo'){
statusTitle.style.color = 'green';
statusTitle.textContent = "Odometer sucessfully updated";
} else if(splitString[1] == 'ppKm'){
statusTitle.style.color = 'green';
statusTitle.textContent = "pulses per KM sucessfully updated";
}
}
setTimeout(statusTitle.textContent = "", 10000);
}
function onDisconnected() {
console.log('Device got disconnected.');
connectButton.show();
hidePasswordParam();
hideUnconnected();
}
function sendData(data) {
const inputValue = data;
if (!("TextEncoder" in window)) {
console.log("Sorry, this browser does not support TextEncoder...");
}
var enc = new TextEncoder(); // always utf-8
blueToothTXCharacteristic.writeValue(enc.encode(inputValue));
}