diff --git a/src/js/protocols/WebBluetooth.js b/src/js/protocols/WebBluetooth.js index 196708d512..9cd9b22a97 100644 --- a/src/js/protocols/WebBluetooth.js +++ b/src/js/protocols/WebBluetooth.js @@ -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, diff --git a/src/js/protocols/WebSocket.js b/src/js/protocols/WebSocket.js index 0f52b3c860..17b4e8a9cb 100644 --- a/src/js/protocols/WebSocket.js +++ b/src/js/protocols/WebSocket.js @@ -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, + }); + } } }