Working BLE on S3

This commit is contained in:
osiu97
2026-07-06 19:26:50 +02:00
parent 2e3780cb0b
commit ea4530a5e4
7 changed files with 327 additions and 239 deletions
+25 -12
View File
@@ -25,16 +25,16 @@
*/
// ── UUIDs must match src/main.cpp exactly ──────────────────────
const SERVICE_UUID = 'a0b1c2d3-e4f5-6789-abcd-ef0123456700';
const CMD_UUID = 'a0b1c2d3-e4f5-6789-abcd-ef0123456701';
const STATUS_UUID = 'a0b1c2d3-e4f5-6789-abcd-ef0123456702';
const DEVICE_NAME = 'WijiBoard';
const SERVICE_UUID = '18f3b235-9831-4c75-8ec0-210469b820a0';
const CMD_UUID = 'cd083b06-4447-4cf3-a7c3-322ecf802ce4';
const STATUS_UUID = '82e38c5b-d3ab-41d1-861c-b84dc6bb1e03';
const DEVICE_NAME = 'WijiBoard';
const BLE = (() => {
// ── Internal state ──────────────────────────────────────────
let device = null;
let server = null;
let cmdChar = null;
let device = null;
let server = null;
let cmdChar = null;
let statusChar = null;
const encoder = new TextEncoder();
@@ -60,9 +60,9 @@ const BLE = (() => {
// ── Disconnect handler ──────────────────────────────────────
function onDisconnected() {
console.log('[BLE] Device disconnected');
device = null;
server = null;
cmdChar = null;
device = null;
server = null;
cmdChar = null;
statusChar = null;
emit('disconnected');
}
@@ -75,34 +75,47 @@ const BLE = (() => {
// ── connect() ───────────────────────────────────────────────
async function connect() {
console.log('[BLE] connect() called');
if (!navigator.bluetooth) {
throw new Error('Web Bluetooth API not available. Use Chrome/Edge over HTTPS or localhost.');
}
emit('connecting');
console.log('[BLE] Requesting device...');
device = await navigator.bluetooth.requestDevice({
filters: [{ name: DEVICE_NAME }],
optionalServices: [SERVICE_UUID],
});
console.log('[BLE] Device selected:', device.name);
device.addEventListener('gattserverdisconnected', onDisconnected);
console.log('[BLE] Connecting to GATT server...');
server = await device.gatt.connect();
console.log('[BLE] GATT server connected');
console.log('[BLE] Getting primary service:', SERVICE_UUID);
const service = await server.getPrimaryService(SERVICE_UUID);
console.log('[BLE] Primary service found');
console.log('[BLE] Getting command characteristic:', CMD_UUID);
cmdChar = await service.getCharacteristic(CMD_UUID);
console.log('[BLE] Command characteristic found');
// Status notifications (optional firmware may not have this char yet)
try {
console.log('[BLE] Getting status characteristic:', STATUS_UUID);
statusChar = await service.getCharacteristic(STATUS_UUID);
console.log('[BLE] Starting notifications...');
await statusChar.startNotifications();
statusChar.addEventListener('characteristicvaluechanged', onStatusNotification);
} catch {
console.warn('[BLE] Status characteristic not available notifications disabled');
console.log('[BLE] Notifications started');
} catch (err) {
console.warn('[BLE] Status characteristic not available notifications disabled', err);
}
console.log('[BLE] Connection fully established, emitting connected');
emit('connected', device.name);
}