-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb_serial_ble.html
More file actions
251 lines (219 loc) · 7.03 KB
/
web_serial_ble.html
File metadata and controls
251 lines (219 loc) · 7.03 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Web Serial BLE</title>
<style>
h1 { font-size:20pt; background-color:#4F4F4F; color:#FFFFFF; font-weight : normal;}
h2 { font-size:16pt; }
h3 { font-size:8pt; }
body { font-size:12pt; color:#444444; }
footer{ font-size:4pt; }
tarea{width: 100%;height: 6em;}
</style>
</head>
<body>
<h1><span>WebシリアルBLEツール<br/> For BGX / Lyra Series</span></h1>
<div>
<p>Mode</p>
<input type="radio" id="stream" name="mode" value="stream" checked onchange="modeChange();">
<label for="stream">Stream</label>
<input type="radio" id="command" name="mode" value="command" onchange="modeChange();">
<label for="command">Command</label>
</div>
<br/>
<textarea id="console-text" cols="35" rows="10" disabled></textarea>
<br/>
<br/>
<input id="input-text" type="text" size="33"></input>
<button id="send-button" type="button" disabled>送信</button>
<br/>
<br/>
<button id="connect-button" type="button" disabled>接続</button>
<button id="save-button" type="button">保存</button>
<button id="clear-button" type="button">クリア</button>
<h3>
<span>
本ツールは<a href="https://ssci.to/5794">BLE5モジュール変換基板</a>もしくは
<a href="https://ssci.to/8649">BLE5モジュール変換基板V2</a>で<br/>
ブラウザを介してシリアル通信をテストするツールです</span>
</h3>
<h3><span>オフラインやローカル環境でも動作可能です</span></h3>
<h3><span>ブラウザはEdgeもしくはChromeのみ対応しています</span></h3>
<h3><span>一度に送信できる最大文字数は250文字です</span></h3>
<h3><span>「GATT Error: Not paired」エラーが発生する場合は事前に<br/>OS側の設定画面でデバイスを追加してください</span></h3>
<footer>
<p>Copyright (c) 2023 Crescent All Rights Reserved.</p>
</footer>
<script>
/* Web Serial BLE Tool For BGX13
* Copyright (c) 2023
* K.Watanabe,Crescent
* Released under the MIT license
* http://opensource.org/licenses/mit-license.php
*/
const connectButton = document.getElementById ('connect-button');
const saveButton = document.getElementById ('save-button');
const clearButton = document.getElementById ('clear-button');
const sendButton = document.getElementById ('send-button');
const consoleText = document.getElementById ('console-text');
const inputText = document.getElementById ('input-text');
const service_uuid = "331a36f5-2459-45ea-9d95-6142f0c4b307";
const rx_uuid = "a9da6040-0823-4995-94ec-9ce41ca28833";//PC->BGX13(Notify OFF)
const tx_uuid = "a73e9a10-628f-4494-a099-12efaf72258f";//BGX13->PC(Notify ON)
const mode_uuid = "75a9f022-af03-4e41-b4bc-9de90a47d50b";
const bgxMode = {
stream: 1,
local : 2,
remote :3
};
let mode = bgxMode.stream;
let device;
let consoleStr = "";
let connected = false;
let closed;
let mode_characteristic;
let tx_characteristic;
let rx_characteristic;
async function modeChange()
{
if(stream.checked==true)
mode = bgxMode.stream;
else
mode = bgxMode.remote;
if(connected && device && device.gatt.connected)
{
const md = new Uint8Array([mode]);
await mode_characteristic.writeValue(md);
}
}
async function sendString()
{
try
{
if(connected && device && device.gatt.connected)
{
let val_arr = new Uint8Array(inputText.value.length+1)
for (let i = 0; i < inputText.value.length; i++) {
let val = inputText.value[i].charCodeAt(0);
val_arr[i] = val;
}
val_arr[inputText.value.length]=0x0d;//return code
await rx_characteristic.writeValue(val_arr);
console.log(inputText.value);
inputText.value = "";
}
}
catch (error)
{
console.log(error);
}
}
function notification(event)
{
let value = event.target.value;
let str = "";
for (let i = 0; i < value.byteLength; i++)
{
str += String.fromCharCode(value.getUint8(i));
}
consoleStr = consoleStr + str;
consoleText.value = consoleStr;
console.log(str);
}
function disconnected()
{
connectButton.innerText = '接続';
sendButton.disabled = true;
connected = false;
}
//Save Concole
saveButton.addEventListener('click', (event) =>
{
let doc = document.createElement("a");
doc.href = 'data:text/csv,' + encodeURIComponent(consoleStr);
doc.download = "Console.txt";
doc.click();
});
//Clear Console
clearButton.addEventListener('click', (event) =>
{
consoleText.value = "";
consoleStr = "";
});
//Connect BLE Device
async function connectBLE()
{
try
{
console.log("Searching Bluetooth Device");
device = await navigator.bluetooth.requestDevice({
filters: [{ services: [service_uuid] }],
optionalServices: [service_uuid]
//acceptAllDevices: true
//optionalServices: ['battery_service'],
});
//console.log("Connecting to GATT Server");
await device.addEventListener('gattserverdisconnected', disconnected);
const server = await device.gatt.connect();
//console.log("Getting Primary Service");
const service = await server.getPrimaryService(service_uuid);
//console.log("Getting Mode Characteristic");
mode_characteristic = await service.getCharacteristic(mode_uuid);
//console.log("Getting RX Characteristic");
rx_characteristic = await service.getCharacteristic(rx_uuid);
//console.log("Getting TX Characteristic");
tx_characteristic = await service.getCharacteristic(tx_uuid);
//console.log('Enable TX Notification');
await tx_characteristic.startNotifications();
//console.log('Notification Started');
await tx_characteristic.addEventListener('characteristicvaluechanged', notification);
const md = new Uint8Array([mode]);
await mode_characteristic.writeValue(md);
connectButton.innerText = '切断';
sendButton.disabled = false;
connected = true;
//Send Button Event
sendButton.addEventListener('click', sendString);
console.log('Connected');
}
catch (error)
{
console.log(error);
alert(error);
if(device && device.gatt.connected)
device.gatt.disconnect();
connected = false;
}
finally
{
}
}
if (navigator.bluetooth)
{
connectButton.addEventListener('click', async ()=>
{
if (device)
{
connectButton.innerText = '接続';
sendButton.disabled = true;
await closed;
device = undefined;
sendButton.removeEventListener('click', sendString);
rx_characteristic = undefined;
tx_characteristic = undefined;
}
else
{
closed = connectBLE();
}
});
connectButton.disabled = false;
}
else
{
alert('お使いのブラウザはWeb Bluetooth APIに対応していません \nEdgeもしくはchromeを使用してください');
}
</script>
</body>
</html>