-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo_async.html
182 lines (149 loc) · 4.41 KB
/
demo_async.html
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>esp to js</title>
<link rel="icon" type="image/x-icon" href="src/favicon.png">
</head>
<body style="background-color: black;">
<button id="connect">Connect</button>
<button id="read">Read</button>
<button id="write">Write</button>
<button id="notify">Notify</button>
<button id="disconnect">Disconnect</button>
</body>
<script type="text/javascript">
const connectBtn = document.querySelector("#connect");
const readBtn = document.querySelector("#read");
const writeBtn = document.querySelector("#write");
const notifyBtn = document.querySelector("#notify");
const disconnectBtn = document.querySelector("#disconnect");
const bleService = '19B10000-E8F2-537E-4F6C-D104768A1214'.toLowerCase();
const bleCharacteristicRandNb = '77a57c66-3bc9-463a-bc52-1ee5763b9c0f'.toLowerCase();
const bleCharacteristicLed = '19B10001-E8F2-537E-4F6C-D104768A1214'.toLowerCase();
let LedOn = true;
let notify = false;
let bluetoothDevice;
let GattServer;
let GattLed;
let GattRandNb;
const deviceName = 'ESP-BLE';
readBtn.disabled = true;
writeBtn.disabled = true;
notifyBtn.disabled = true;
disconnectBtn.disabled = true;
connectBtn.addEventListener('click', function() {
if(isWebBLEAvailable()){
connectGATT();
}
});
readBtn.addEventListener('click',async function() {
if(isWebBLEAvailable() && bluetoothDevice){
console.log("🏷️ Lecture: ",arrayToString(await read(GattLed)));
}
});
writeBtn.addEventListener('click', function() {
if(isWebBLEAvailable() && bluetoothDevice){
if (LedOn) {
write(GattLed, new Uint8Array([42,35]));
LedOn = false;
} else {
write(GattLed, new Uint8Array([42,2]));
LedOn = true;
}
}
});
notifyBtn.addEventListener('click', function () {
if(isWebBLEAvailable() && bluetoothDevice){
toggleNotify(GattRandNb);
}
})
disconnectBtn.addEventListener('click', function() {
if(isWebBLEAvailable() && bluetoothDevice){
disconnect();
}
});
function isWebBLEAvailable() {
if(!navigator.bluetooth){
console.error("🚧 Bluetooth non disponible");
}
return navigator.bluetooth;
}
async function getDeviceInfo() {
const options = {
optionalServices : [bleService,
bleCharacteristicRandNb,
bleCharacteristicLed,
],
filters : [
{namePrefix: deviceName}
]
};
console.log("📄 Choisir un appareil ...");
try {
return await navigator.bluetooth.requestDevice(options);
} catch (error) {
console.error("🚨 Erreur lors de la selection "+error);
return null;
}
}
async function connectGATT() {
bluetoothDevice = await getDeviceInfo();
if (!bluetoothDevice.gatt.connected){
GattServer = await bluetoothDevice.gatt.connect();
}
console.log("📱 Appareil connecté !",bluetoothDevice.gatt)
console.log("👷 Demande du service ...")
service = await GattServer.getPrimaryService(bleService);
console.log("👷 Demande des characteristics ...")
GattLed = await service.getCharacteristic(bleCharacteristicLed);
GattRandNb = await service.getCharacteristic(bleCharacteristicRandNb);
GattRandNb.addEventListener('characteristicvaluechanged', handleChangedValue);
readBtn.disabled = false;
writeBtn.disabled = false;
notifyBtn.disabled = false;
disconnectBtn.disabled = false;
console.log("✅ Tout est prêt")
}
function handleChangedValue(event) {
const value = event.target.value;
console.log("🏷️ Lecture: "+value.getUint8(0));
}
async function read(GATT){
return await GATT.readValue();
}
function write(GATT, value){
console.log("🚀 Envoi de données: "+ value);
GATT.writeValue(value);
}
function toggleNotify(GATT){
if(!notify){
GATT.startNotifications();
console.log("➕ Deamnde de debut de l'écoute");
notify = true;
}else{
GATT.stopNotifications();
console.log("➖ Demande de fin de l'écoute");
notify = false;
}
}
async function disconnect(){
if(bluetoothDevice.gatt.connected){
await bluetoothDevice.gatt.disconnect();
}
console.log("🚩 Appareil déconnecté", bluetoothDevice);
readBtn.disabled = true;
writeBtn.disabled = true;
notifyBtn.disabled = true;
disconnectBtn.disabled = true;
}
function arrayToString(array) {
let values = new Array(array.byteLength);
for (let i = 0; i < array.byteLength; i++) {
values[i] = array.getUint8(i);
}
return values;
}
</script>
</html>