Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions src/js/protocols/WebBluetooth.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,24 +343,44 @@ class WebBluetooth extends EventTarget {
}
}

async send(data) {
async send(data, cb) {
if (!this.writeCharacteristic) {
if (cb) {
cb({
error: "No write characteristic available",
bytesSent: 0,
});
}
console.error(`${this.logHead} No write characteristic available`);
return;
}

// There is no writable stream in the bluetooth API
this.bytesSent += data.byteLength;

const dataBuffer = new Uint8Array(data);

try {
if (this.lastWrite) {
await this.lastWrite;
}
} catch (error) {
console.error(error);
this.lastWrite = this.writeCharacteristic.writeValue(dataBuffer);
await this.lastWrite;
this.bytesSent += data.byteLength;

if (cb) {
cb({
error: null,
bytesSent: data.byteLength,
});
}
} catch (e) {
console.error(`${this.logHead} Failed to send data:`, e);
if (cb) {
cb({
error: e,
bytesSent: 0,
});
}
}
this.lastWrite = this.writeCharacteristic.writeValue(dataBuffer);

return {
bytesSent: data.byteLength,
Expand Down
14 changes: 14 additions & 0 deletions src/js/protocols/WebSocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,22 @@ class Websocket extends EventTarget {
try {
this.ws.send(data);
this.bytesSent += data.byteLength;

if (cb) {
cb({
error: null,
bytesSent: data.byteLength,
});
}
} catch (e) {
console.error(`${this.logHead}Failed to send data e: ${e}`);

if (cb) {
cb({
error: e,
bytesSent: 0,
});
}
}
}

Expand Down