@@ -47,6 +47,14 @@ class CapacitorSerialProtocol extends EventTarget {
4747 UsbSerial . addListener ( "error" , this . handleErrorEvent ) ;
4848 }
4949
50+ cleanup ( ) {
51+ UsbSerial . removeAllListeners ( "data" ) ;
52+ UsbSerial . removeAllListeners ( "connected" ) ;
53+ UsbSerial . removeAllListeners ( "attached" ) ;
54+ UsbSerial . removeAllListeners ( "detached" ) ;
55+ UsbSerial . removeAllListeners ( "error" ) ;
56+ }
57+
5058 handleDataEvent ( event ) {
5159 if ( event ?. data ) {
5260 // Convert hex string from plugin to Uint8Array
@@ -61,10 +69,7 @@ class CapacitorSerialProtocol extends EventTarget {
6169 }
6270
6371 handleAttachedEvent ( event ) {
64- const added = this . handleNewDevice ( event ) ;
65- if ( added ) {
66- this . dispatchEvent ( new CustomEvent ( "addedDevice" , { detail : added } ) ) ;
67- }
72+ this . handleNewDevice ( event ) ;
6873 }
6974
7075 handleDetachedEvent ( event ) {
@@ -81,7 +86,7 @@ class CapacitorSerialProtocol extends EventTarget {
8186 const uint8Array = new Uint8Array ( length ) ;
8287
8388 for ( let i = 0 ; i < length ; i ++ ) {
84- uint8Array [ i ] = parseInt ( hexString . substr ( i * 2 , 2 ) , 16 ) ;
89+ uint8Array [ i ] = parseInt ( hexString . slice ( i * 2 , i * 2 + 2 ) , 16 ) ;
8590 }
8691
8792 return uint8Array ;
@@ -260,6 +265,7 @@ class CapacitorSerialProtocol extends EventTarget {
260265 console . error ( `${ logHead } Error closing serial connection:` , error ) ;
261266 closeError = error ;
262267 } finally {
268+ this . cleanup ( ) ;
263269 this . isOpen = false ;
264270 this . connected = false ;
265271 this . port = null ;
0 commit comments