-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
280 lines (266 loc) · 10.5 KB
/
index.html
File metadata and controls
280 lines (266 loc) · 10.5 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<link rel="icon" href="favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content="BLE蓝牙调试工具" />
<meta name="theme-color" content="#fafafa" />
<link rel="apple-touch-icon" href="logo192.png" />
<link rel="manifest" href="manifest.json" />
<link rel="stylesheet" href="light.min.css"> <!-- https://watercss.kognise.dev/ -->
<title>BLE蓝牙调试工具</title>
<style>
body {
display: flex;
flex-direction: column;
align-items: center;
margin: 0;
padding: 20px;
font-family: Arial, sans-serif;
width: 100%;
height: 100vh;
box-sizing: border-box;
}
.container {
display: flex;
flex-direction: column;
width: 100%;
height: 100%;
}
.input-group {
display: flex;
margin-bottom: 10px;
width: 100%;
}
.input-group input {
flex: 1;
padding: 10px;
font-size: 16px;
box-sizing: border-box;
}
.input-group button {
padding: 10px;
font-size: 16px;
box-sizing: border-box;
}
.textarea-group {
display: flex;
margin-bottom: 10px;
width: 100%;
}
.textarea-group textarea {
flex: 1;
padding: 10px;
font-size: 16px;
height: 100px;
box-sizing: border-box;
}
.textarea-group button {
padding: 10px;
font-size: 16px;
margin-left: 10px;
box-sizing: border-box;
}
#recvinfo {
width: 100%;
height: 150px;
padding: 10px;
font-size: 16px;
margin-top: 10px;
box-sizing: border-box;
}
@media (max-width: 600px) {
.input-group, .textarea-group {
flex-direction: column;
}
.textarea-group button {
margin-left: 0;
margin-top: 10px;
}
}
.misc {
position: absolute;
bottom: 0;
left: 0;
margin: 10px;
}
</style>
</head>
<body>
<div class="container">
<div class="input-group">
<input type="text" id="device-name" placeholder="设备名称" readonly>
<button id="main-button" onclick="handleButtonClick()">连接</button>
</div>
<div class="textarea-group">
<textarea id="send-text" placeholder="输入要发送的内容">68 17 00 43 45 AA AA AA AA AA AA 00 5B 4F 05 01 05 40 01 02 00 00 6A 17 16</textarea>
<button id="send-button" onclick="senddata()" disabled>发送</button>
</div>
<div style="display: flex; align-items: center; justify-content: space-between;">
<label for="loginfo">日志信息</label>
<button onclick="clearLog()">清除</button>
</div>
<textarea id="loginfo" readonly style="flex-grow: 1; width: 100%;"></textarea>
<div class="misc">
<button id="install-button" hidden>将蓝牙调试工具安装到系统</button>
<p style="font-size: smaller; color: gray; margin: 0%">
<span style="font-size: smaller">蓝牙检查:edge://bluetooth-internals/#adapter</span>
<br />
<span style="font-size: smaller">copyright (c) 2024 etmedia, licensed under MIT License</span>
</p>
</div>
</div>
<dialog id="dialog">
<header>出现错误</header>
<form method="dialog">
<p id="dialog-content"></p>
<menu style="display: flex; justify-content: flex-end;">
<button onclick="succeeded = false">好</button>
</menu>
</form>
</dialog>
</body>
<script>
function clearLog() {
document.getElementById('loginfo').value = '';
}
function makePayload() {
const text = document.getElementById('send-text').value;
const cleanedText = text.replace(/\s+/g, '');
const uint8Array = new Uint8Array(cleanedText.length / 2);
for (let i = 0; i < cleanedText.length; i += 2) {
uint8Array[i / 2] = parseInt(cleanedText.substr(i, 2), 16);
}
return uint8Array;
}
function log(message) {
const logInfo = document.getElementById("loginfo");
const currentTime = new Date().toLocaleString();
logInfo.value = `[${currentTime}] ${message}\n` + logInfo.value;
}
let bluetoothDevice;
let Characteristic;
let CharacteristicRx;
let isStarted = false;
function updateUi(stage) {
const mainButton = document.getElementById("main-button");
const sendButton = document.getElementById("send-button");
const deviceName = document.getElementById("device-name");
const status = document.getElementById("status");
const recvinfo = document.getElementById("recvinfo");
switch (stage) {
case "pending":
mainButton.innerText = "请稍后";
mainButton.disabled = true;
sendButton.disabled = true;
deviceName.value = "已连接:" + bluetoothDevice.name;
break;
case "ok":
mainButton.innerText = "结束";
mainButton.disabled = false;
sendButton.disabled = false;
break;
case "standby":
mainButton.innerText = "开启";
mainButton.disabled = false;
sendButton.disabled = false;
deviceName.value = "未连接";
break;
}
}
async function handleBluetoothError(error) {
console.log(error)
// this is so fucking ugly but i have no choice
// you would never know how those shitty browsers behave
if (error.toString().match(/User cancelled/) || error.toString() == "2") return; // "2" is a weird behavior of Bluefy browser on iOS
const dialogContent = document.getElementById("dialog-content");
if (!navigator.bluetooth || error.toString().match(/Bluetooth adapter not available/)) {
dialogContent.innerText = "找不到蓝牙硬件,或浏览器不支持。\n\n限于篇幅,详情请参考源代码仓库内的“疑难解答”。";
} else if (error.toString().match(/User denied the browser permission/)) {
dialogContent.innerText = "蓝牙权限遭拒。\n\n请前往手机设置,授予浏览器“位置信息”权限。\n此权限不会用于定位,详情请参考源代码仓库内的“疑难解答”。";
} else if (error.toString().match(/NetworkError/)) {
dialogContent.innerText = "连接不稳定,无法与蓝牙设备建立连接。\n请重试。";
} else {
dialogContent.innerText = error + "\n\n是什么呢\n\n(这可能是一个Bug,请截图并反馈给开发者。)";
}
document.getElementById("dialog").showModal();
if (bluetoothDevice) await bluetoothDevice.gatt.disconnect();
isStarted = false;
updateUi("standby");
}
function bufferToHexString(array) {
// explanation: [0x12, 0x34, 0xAB, 0xCD] => "1234ABCD"
return Array.from(new Uint8Array(array))
.map((x) => x.toString(16).padStart(2, "0"))
.join("")
.toUpperCase();
}
async function handleRxdNotifications(event) {
const value = event.target.value;
log("RX:"+bufferToHexString(value.buffer));
}
async function senddata() {
log("TX:"+bufferToHexString(makePayload(bluetoothDevice.name)));
await Characteristic.writeValue(makePayload(bluetoothDevice.name));
}
async function start() {
try {
bluetoothDevice = await navigator.bluetooth.requestDevice({
// filters: [{ namePrefix: "2" }],
acceptAllDevices: true,//http://localhost:63342/klbc-bluetooth-water-control/manifest.json?_ijt=ji272376ot7avl396u716kucb1
// optionalServices: [0xFF59] // actually it's better to use 0xF1F0 instead of "generic_access" but it will trigger a bug on Bluefy
optionalServices: ['6e400001-b5a3-f393-e0a9-e50e24dc4179', window.navigator.userAgent.match(/Bluefy/) ? "generic_access" : 0xFFE0]
});
updateUi("pending");
const server = await bluetoothDevice.gatt.connect();
const services = await server.getPrimaryServices();
if(services.length == 0){
throw new Error('No services found');
return;
}
// 获取蓝牙uuid相关内容
const service = services[0];
// console.log(`service`, service.uuid);
// 获取可以读写字符流的服务
const Characteristics = await service.getCharacteristics();
if(Characteristics.length == 0){
throw new Error('No characteristics found');
return;
}
if(Characteristics.length == 1)
{
Characteristic = Characteristics[0];
CharacteristicRx = Characteristics[0];
}
else
{
Characteristic = Characteristics[0];
CharacteristicRx = Characteristics[1];
}
await CharacteristicRx.startNotifications();
CharacteristicRx.addEventListener("characteristicvaluechanged", handleRxdNotifications);
// // 写入字节(括号中的方法为把字符串转为字符流,传输给蓝牙)
// await Characteristic.writeValue(makePayload(bluetoothDevice.name));
isStarted = true;
updateUi("ok");
} catch (error) {
handleBluetoothError(error);
}
}
async function end() {
try {
// const endPayload = new Uint8Array([0xFE, 0xFE, 0x09, 0xB3, 0x00, 0x00])
// await txdCharacteristic.writeValue(endPayload)
await bluetoothDevice.gatt.disconnect();
isStarted = false;
updateUi("standby");
} catch (error) {
handleBluetoothError(error);
}
}
function handleButtonClick() {
isStarted ? end() : start();
}
</script>
</html>