-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbluetooth.js
93 lines (85 loc) · 2.67 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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');
}
}
blueTooth.startNotifications(blueToothRXCharacteristic, gotValue, 'string');
isConnected = blueTooth.isConnected();
connectButton.hide();
showAllParam();
sendData("#param");
// 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] == 'stat') {//status string
if (splitString[1] == 'co') {
wifiConnected = true;
falseWifiCredentials = false;
showWifiParam();
}
ssidInput.value(splitString[2]);
connectedSSID = splitString[2];
ip = splitString[3];
if (splitString[4] == 'on') {
AlexaStatusSlider.value(1);
alexaStatus = "on";
} else {
AlexaStatusSlider.value(0);
alexaStatus = "off";
}
if (firstConnected) {
sendData("#param");
firstConnected = false;
}
} else if (splitString[0] == 'wifiFailed') {//pw string
falseWifiCredentials = true;
wifiConnected = false;
} else if (splitString[0] == 'wifiNotConfigured') {//pw string
wifiConnected = false;
}
}
function onDisconnected() {
console.log('Device got disconnected.');
isConnected = false;
firstConnected = true;
connectButton.show();
hideAllParam();
}
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));
}