-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmobile-test.html
More file actions
485 lines (414 loc) · 18.6 KB
/
mobile-test.html
File metadata and controls
485 lines (414 loc) · 18.6 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<title>Mobile Bluetooth Test</title>
<style>
* {
box-sizing: border-box;
-webkit-tap-highlight-color: transparent;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
margin: 0;
padding: 20px;
background-color: #f5f5f5;
touch-action: manipulation;
}
.container {
max-width: 100%;
margin: 0 auto;
}
.card {
background: white;
border-radius: 12px;
padding: 20px;
margin-bottom: 20px;
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}
.status {
padding: 15px;
margin: 15px 0;
border-radius: 8px;
font-weight: 500;
text-align: center;
}
.success { background-color: #d4edda; color: #155724; border: 1px solid #c3e6cb; }
.error { background-color: #f8d7da; color: #721c24; border: 1px solid #f5c6cb; }
.info { background-color: #d1ecf1; color: #0c5460; border: 1px solid #bee5eb; }
.warning { background-color: #fff3cd; color: #856404; border: 1px solid #ffeaa7; }
button {
width: 100%;
padding: 16px;
margin: 8px 0;
border: none;
border-radius: 8px;
cursor: pointer;
font-size: 16px;
font-weight: 600;
touch-action: manipulation;
-webkit-appearance: none;
transition: all 0.2s ease;
}
button:active {
transform: scale(0.98);
}
.btn-primary { background-color: #007bff; color: white; }
.btn-secondary { background-color: #6c757d; color: white; }
.btn-success { background-color: #28a745; color: white; }
.btn-danger { background-color: #dc3545; color: white; }
.btn-warning { background-color: #ffc107; color: #212529; }
.log {
background-color: #f8f9fa;
border: 1px solid #dee2e6;
padding: 15px;
margin: 15px 0;
border-radius: 8px;
max-height: 200px;
overflow-y: auto;
font-family: 'SF Mono', Monaco, 'Cascadia Code', monospace;
font-size: 12px;
line-height: 1.4;
}
h1 {
color: #333;
text-align: center;
margin-bottom: 20px;
}
h2 {
color: #495057;
margin-top: 0;
font-size: 18px;
}
.info-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
margin: 15px 0;
}
.info-item {
background: #f8f9fa;
padding: 10px;
border-radius: 6px;
text-align: center;
font-size: 12px;
}
.info-label {
font-weight: 600;
color: #6c757d;
}
.info-value {
color: #495057;
margin-top: 4px;
}
.tips {
background: #e7f3ff;
border: 1px solid #b3d9ff;
padding: 15px;
border-radius: 8px;
margin: 15px 0;
}
.tips h3 {
margin-top: 0;
color: #0056b3;
}
.tips ul {
margin: 10px 0;
padding-left: 20px;
}
.tips li {
margin: 5px 0;
font-size: 14px;
}
</style>
</head>
<body>
<div class="container">
<h1>📱 Mobile Bluetooth Test</h1>
<div id="status" class="status info">Checking system...</div>
<div class="card">
<h2>🔧 System Information</h2>
<div class="info-grid">
<div class="info-item">
<div class="info-label">Protocol</div>
<div class="info-value" id="protocolInfo">-</div>
</div>
<div class="info-item">
<div class="info-label">Secure Context</div>
<div class="info-value" id="secureInfo">-</div>
</div>
<div class="info-item">
<div class="info-label">Browser</div>
<div class="info-value" id="browserInfo">-</div>
</div>
<div class="info-item">
<div class="info-label">Platform</div>
<div class="info-value" id="platformInfo">-</div>
</div>
<div class="info-item">
<div class="info-label">Bluetooth</div>
<div class="info-value" id="bluetoothInfo">-</div>
</div>
<div class="info-item">
<div class="info-label">Mobile</div>
<div class="info-value" id="mobileInfo">-</div>
</div>
</div>
<button class="btn-warning" onclick="checkSystem()">Refresh System Info</button>
</div>
<div class="card">
<h2>📱 Device Discovery</h2>
<button class="btn-primary" onclick="discoverDevices()">🔍 Find Gprinter Devices</button>
<button class="btn-secondary" onclick="discoverAllDevices()">🔍 Find All Devices</button>
</div>
<div class="card">
<h2>🔗 Connection Test</h2>
<button class="btn-success" onclick="testConnection()">🔗 Connect to Device</button>
<button class="btn-danger" onclick="disconnect()">🔌 Disconnect</button>
<button class="btn-warning" onclick="testPrint()">🖨️ Test Print</button>
</div>
<div class="card">
<h2>📋 Debug Log</h2>
<button class="btn-secondary" onclick="clearLog()">Clear Log</button>
<div class="log" id="log"></div>
</div>
<div class="tips">
<h3>💡 Mobile Setup Tips</h3>
<ul>
<li><strong>HTTPS Required:</strong> Bluetooth only works over HTTPS or localhost</li>
<li><strong>Printer Setup:</strong> Turn ON your Gprinter GP-M322 and ensure it's in pairing mode</li>
<li><strong>Bluetooth:</strong> Make sure Bluetooth is enabled in your device settings</li>
<li><strong>Browser:</strong> Use Chrome or Samsung Internet on Android</li>
<li><strong>iOS:</strong> Web Bluetooth is not supported on iOS Safari</li>
<li><strong>Permissions:</strong> Allow Bluetooth permissions when prompted</li>
<li><strong>Touch:</strong> Tap buttons firmly and wait for response</li>
</ul>
</div>
</div>
<script>
let bluetoothDevice = null;
let gattServer = null;
let writeCharacteristic = null;
const log = document.getElementById('log');
const status = document.getElementById('status');
const protocolInfo = document.getElementById('protocolInfo');
const secureInfo = document.getElementById('secureInfo');
const browserInfo = document.getElementById('browserInfo');
const platformInfo = document.getElementById('platformInfo');
const bluetoothInfo = document.getElementById('bluetoothInfo');
const mobileInfo = document.getElementById('mobileInfo');
function updateStatus(message, type = 'info') {
status.textContent = message;
status.className = `status ${type}`;
addLog(message);
}
function addLog(message) {
const timestamp = new Date().toLocaleTimeString();
log.innerHTML += `[${timestamp}] ${message}\n`;
log.scrollTop = log.scrollHeight;
}
function clearLog() {
log.innerHTML = '';
updateStatus('Log cleared', 'info');
}
function updateSystemInfo() {
const userAgent = navigator.userAgent;
const isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(userAgent);
const isSecure = window.isSecureContext;
const hasBluetooth = !!navigator.bluetooth;
const protocol = window.location.protocol;
// Browser detection
let browser = 'Unknown';
if (userAgent.includes('Chrome')) browser = 'Chrome';
else if (userAgent.includes('SamsungBrowser')) browser = 'Samsung Internet';
else if (userAgent.includes('Firefox')) browser = 'Firefox';
else if (userAgent.includes('Safari')) browser = 'Safari';
else if (userAgent.includes('Edge')) browser = 'Edge';
// Platform detection
let platform = 'Unknown';
if (userAgent.includes('Android')) platform = 'Android';
else if (userAgent.includes('iPhone') || userAgent.includes('iPad')) platform = 'iOS';
else if (userAgent.includes('Windows')) platform = 'Windows';
else if (userAgent.includes('Mac')) platform = 'macOS';
else if (userAgent.includes('Linux')) platform = 'Linux';
protocolInfo.textContent = protocol;
secureInfo.textContent = isSecure ? 'Yes' : 'No';
browserInfo.textContent = browser;
platformInfo.textContent = platform;
bluetoothInfo.textContent = hasBluetooth ? 'Available' : 'Not Available';
mobileInfo.textContent = isMobile ? 'Yes' : 'No';
addLog(`Protocol: ${protocol}`);
addLog(`Secure Context: ${isSecure}`);
addLog(`Browser: ${browser}`);
addLog(`Platform: ${platform}`);
addLog(`Mobile: ${isMobile}`);
addLog(`Bluetooth: ${hasBluetooth}`);
}
function checkSystem() {
addLog('=== System Check ===');
updateSystemInfo();
const isSecure = window.isSecureContext;
const hasBluetooth = !!navigator.bluetooth;
const isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
const platform = navigator.userAgent.includes('Android') ? 'Android' :
navigator.userAgent.includes('iPhone') || navigator.userAgent.includes('iPad') ? 'iOS' : 'Other';
if (!isSecure) {
updateStatus('❌ HTTPS required for Bluetooth', 'error');
return;
}
if (!hasBluetooth) {
updateStatus('❌ Web Bluetooth not supported', 'error');
return;
}
if (platform === 'iOS') {
updateStatus('❌ Web Bluetooth not supported on iOS', 'error');
return;
}
if (isMobile) {
updateStatus('✅ Mobile system ready for Bluetooth', 'success');
} else {
updateStatus('✅ Desktop system ready for Bluetooth', 'success');
}
}
async function discoverDevices() {
try {
updateStatus('🔍 Searching for Gprinter devices...', 'info');
addLog('Starting device discovery with name filters...');
bluetoothDevice = await navigator.bluetooth.requestDevice({
filters: [
{ namePrefix: 'GP-' },
{ namePrefix: 'Gprinter' },
{ namePrefix: 'GP' },
{ namePrefix: 'Printer' },
{ namePrefix: 'BLE' }
],
optionalServices: [0xffe0, 0xff00, 0x1800]
});
addLog(`✅ Device found: ${bluetoothDevice.name || 'Unknown'} (${bluetoothDevice.id})`);
updateStatus(`Found device: ${bluetoothDevice.name || 'Unknown'}`, 'success');
} catch (error) {
const errorMessage = error.message || 'Unknown error';
updateStatus(`❌ Device discovery failed: ${errorMessage}`, 'error');
addLog(`ERROR: ${errorMessage}`);
if (error.name === 'NotFoundError') {
addLog('No devices found matching the filters. Try "Find All Devices" instead.');
}
}
}
async function discoverAllDevices() {
try {
updateStatus('🔍 Searching for all Bluetooth devices...', 'info');
addLog('Starting broad device discovery...');
bluetoothDevice = await navigator.bluetooth.requestDevice({
acceptAllDevices: true,
optionalServices: [0xffe0, 0xff00, 0x1800]
});
addLog(`✅ Device found: ${bluetoothDevice.name || 'Unknown'} (${bluetoothDevice.id})`);
updateStatus(`Found device: ${bluetoothDevice.name || 'Unknown'}`, 'success');
} catch (error) {
const errorMessage = error.message || 'Unknown error';
updateStatus(`❌ Device discovery failed: ${errorMessage}`, 'error');
addLog(`ERROR: ${errorMessage}`);
}
}
async function testConnection() {
if (!bluetoothDevice) {
updateStatus('❌ No device selected. Find devices first.', 'error');
return;
}
try {
updateStatus('🔗 Connecting to device...', 'info');
addLog(`Connecting to ${bluetoothDevice.name}...`);
if (!bluetoothDevice.gatt) {
throw new Error('Selected device does not support GATT');
}
gattServer = await bluetoothDevice.gatt.connect();
addLog('✅ GATT server connected');
// Try to find a compatible service
const serviceUUIDs = [0xffe0, 0xff00, 0x1800];
let service = null;
for (const serviceUUID of serviceUUIDs) {
try {
service = await gattServer.getPrimaryService(serviceUUID);
addLog(`✅ Found service: 0x${serviceUUID.toString(16)}`);
break;
} catch (error) {
addLog(`❌ Service 0x${serviceUUID.toString(16)} not found`);
}
}
if (!service) {
throw new Error('No compatible printer service found');
}
// Try to find a compatible characteristic
const charUUIDs = [0xffe1, 0xff01];
for (const charUUID of charUUIDs) {
try {
writeCharacteristic = await service.getCharacteristic(charUUID);
addLog(`✅ Found characteristic: 0x${charUUID.toString(16)}`);
break;
} catch (error) {
addLog(`❌ Characteristic 0x${charUUID.toString(16)} not found`);
}
}
if (!writeCharacteristic) {
throw new Error('No compatible write characteristic found');
}
updateStatus(`✅ Connected to ${bluetoothDevice.name || 'Unknown'}`, 'success');
addLog('🎉 Connection successful!');
} catch (error) {
const errorMessage = error.message || 'Unknown error';
updateStatus(`❌ Connection failed: ${errorMessage}`, 'error');
addLog(`ERROR: ${errorMessage}`);
}
}
async function disconnect() {
try {
if (gattServer && gattServer.connected) {
gattServer.disconnect();
}
bluetoothDevice = null;
gattServer = null;
writeCharacteristic = null;
updateStatus('🔌 Disconnected', 'info');
addLog('Disconnected from printer');
} catch (error) {
addLog(`ERROR during disconnect: ${error.message}`);
}
}
async function testPrint() {
if (!writeCharacteristic) {
updateStatus('❌ Not connected to printer', 'error');
return;
}
try {
updateStatus('🖨️ Sending test print...', 'info');
addLog('Sending test print...');
// Initialize printer
await writeCharacteristic.writeValueWithoutResponse(new Uint8Array([0x1b, 0x40]));
addLog('✅ Printer initialized');
// Print test text
const testText = 'Test Print\nGprinter GP-M322\n' + new Date().toLocaleString() + '\n\n\n';
const encoder = new TextEncoder();
await writeCharacteristic.writeValueWithoutResponse(encoder.encode(testText));
addLog('✅ Test text sent');
// Feed and cut
await writeCharacteristic.writeValueWithoutResponse(new Uint8Array([0x1d, 0x56, 0x42, 0x10]));
addLog('✅ Feed and cut command sent');
updateStatus('✅ Test print sent successfully', 'success');
addLog('🎉 Test print completed successfully!');
} catch (error) {
const errorMessage = error.message || 'Unknown error';
updateStatus(`❌ Print failed: ${errorMessage}`, 'error');
addLog(`ERROR: ${errorMessage}`);
}
}
// Initialize on page load
window.addEventListener('load', () => {
updateStatus('Ready to test mobile Bluetooth', 'info');
addLog('Page loaded. Click "Refresh System Info" to begin.');
updateSystemInfo();
});
</script>
</body>
</html>