-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdroid-bt-connect.js
124 lines (113 loc) · 3.44 KB
/
droid-bt-connect.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
121
122
123
124
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
var writeC;
const serviceUuid = '09b600a0-3e42-41fc-b474-e9c0c8f0c801';
const notificationUuid = '09b600b0-3e42-41fc-b474-e9c0c8f0c801';
const writeUuid = '09b600b1-3e42-41fc-b474-e9c0c8f0c801';
var currentStep = -1;
const fromHexString = (hexString) =>
new Uint8Array(
hexString
.replace(/\s+/g, '')
.match(/.{1,2}/g)
.map((byte) => parseInt(byte, 16))
);
document.getElementById('connect-button').addEventListener('click', function () {
navigator.bluetooth
.requestDevice({
optionalServices: [serviceUuid],
filters: [
{
name: 'DROID',
},
],
})
.then((device) => {
console.log('Connecting to GATT Service...');
progressStep();
return device.gatt.connect();
})
.then((droid) => {
console.log('Getting Droid...');
progressStep();
return droid.getPrimaryService(serviceUuid);
})
.then((service) => {
console.log('Getting Characteristics...');
progressStep();
return service.getCharacteristics();
})
.then((characteristics) => {
characteristics.forEach((c) => {
if (c.uuid === notificationUuid) {
return handleNotificationCharacteristic(c);
} else if (c.uuid === writeUuid) {
writeC = c;
return initialWrites().catch(error => console.log(error));
}
});
})
.catch((error) => {
console.log('Argh! ' + error);
clearProgress()
setErrorText()
});
});
async function initialWrites() {
await sleep(1500);
await writeC.writeValue(fromHexString('222001'));
await sleep(1500);
await writeC.writeValue(fromHexString('222001'));
await sleep(1500);
await writeC.writeValue(fromHexString('222001'));
await sleep(1500);
await writeC.writeValue(fromHexString('222001'));
await sleep(1500);
await writeC.writeValue(fromHexString('27420f4444001f00'));
await sleep(1500);
await writeC.writeValue(fromHexString('27420f4444001802'));
await sleep(1500);
await writeC.writeValue(fromHexString('27420f4444001f00'));
await sleep(1500);
await writeC.writeValue(fromHexString('27420f4444001802'));
await sleep(2000);
return writeC;
}
async function handleNotificationCharacteristic(c) {
c = await c.startNotifications();
return c.addEventListener(
'characteristicvaluechanged',
handleCharacteristicValueChanged
);
}
function handleCharacteristicValueChanged(event) {
var value = event.target.value;
console.log('Received ' + JSON.stringify(value));
progressStep();
}
var time = Math.random();
// var red = document.querySelector('#red');
// red.style.setProperty('--animation-time', time +'s');
function progressStep() {
currentStep++;
clearErrorText();
if (document.getElementsByClassName('status-light')[currentStep]) {
const currentStepItem = document.getElementsByClassName('status-light')[currentStep];
currentStepItem.classList.add("status-light-active");
currentStepItem.style.setProperty('--animation-time', time + 's');
}
}
function clearProgress() {
currentStep = -1;
var list = document.getElementsByClassName('status-light');
for (let light of list) {
light.classList.remove('status-light-active');
}
}
function setErrorText() {
document.getElementById("error-text").textContent = "Error connecting to droid...";
}
function clearErrorText() {
document.getElementById("error-text").textContent = "";
}