diff --git a/index.html b/index.html index a9b0eed..0273fdb 100644 --- a/index.html +++ b/index.html @@ -142,18 +142,21 @@ async askForSerialPort() { this.connection = await SerialConnection.open(); this.connection.on("connected", () => this.onConnected()); + this.connection.on("disconnected", () => this.onDisconnected()); // this.connection.on("tx", (data) => console.log("tx", data)); // this.connection.on("rx", (data) => console.log("rx", data)); }, async askForBleDevice() { this.connection = await BleConnection.open(); this.connection.on("connected", () => this.onConnected()); + this.connection.on("disconnected", () => this.onDisconnected()); // this.connection.on("tx", (data) => console.log("tx", data)); // this.connection.on("rx", (data) => console.log("rx", data)); }, async disconnect() { if(this.connection){ await this.connection.close(); + this.onDisconnected(); this.connection = null; } }, @@ -166,6 +169,10 @@ await this.loadContacts(); }); }, + onDisconnected() { + console.log("disconnected"); + this.connection = null; + }, async loadSelfInfo() { this.selfInfo = await this.connection.getSelfInfo(); }, diff --git a/src/connection/serial_connection.js b/src/connection/serial_connection.js index 3e90d92..c941875 100644 --- a/src/connection/serial_connection.js +++ b/src/connection/serial_connection.js @@ -15,6 +15,11 @@ class SerialConnection extends Connection { this.readBuffer = []; this.readLoop(); + // listen for disconnect + this.serialPort.addEventListener("disconnect", () => { + this.emit("disconnected"); + }); + // fire connected callback after constructor has returned setTimeout(() => { this.onConnected();