Skip to content

Commit e3c6152

Browse files
committed
Address (re)connection issues
1 parent 1848e79 commit e3c6152

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

src/js/protocols/CapacitorSerial.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,8 @@ class CapacitorSerialProtocol extends EventTarget {
4848
}
4949

5050
cleanup() {
51-
UsbSerial.removeAllListeners("data");
52-
UsbSerial.removeAllListeners("connected");
53-
UsbSerial.removeAllListeners("attached");
54-
UsbSerial.removeAllListeners("detached");
55-
UsbSerial.removeAllListeners("error");
51+
// Don't remove listeners - they need to persist for reconnection and device attach/detach events
52+
// Only cleanup is handled in disconnect() by resetting state
5653
}
5754

5855
handleDataEvent(event) {
@@ -68,8 +65,14 @@ class CapacitorSerialProtocol extends EventTarget {
6865
this.connected = true;
6966
}
7067

71-
handleAttachedEvent(event) {
72-
this.handleNewDevice(event);
68+
async handleAttachedEvent(event) {
69+
// Reload device list to ensure we have the latest device info with proper permissions
70+
await this.loadDevices();
71+
72+
const added = this.handleNewDevice(event);
73+
if (added) {
74+
this.dispatchEvent(new CustomEvent("addedDevice", { detail: added }));
75+
}
7376
}
7477

7578
handleDetachedEvent(event) {
@@ -265,7 +268,7 @@ class CapacitorSerialProtocol extends EventTarget {
265268
console.error(`${logHead} Error closing serial connection:`, error);
266269
closeError = error;
267270
} finally {
268-
this.cleanup();
271+
// Reset state but keep listeners active for reconnection
269272
this.isOpen = false;
270273
this.connected = false;
271274
this.port = null;

0 commit comments

Comments
 (0)