@@ -13,6 +13,30 @@ function base64ToUint8Array(b64) {
1313 return bytes ;
1414}
1515
16+ function uint8ArrayToBase64 ( bytes ) {
17+ let binary = "" ;
18+ for ( let i = 0 ; i < bytes . byteLength ; i ++ ) {
19+ binary += String . fromCharCode ( bytes [ i ] ) ;
20+ }
21+ return btoa ( binary ) ;
22+ }
23+
24+ function normalizeToUint8Array ( data ) {
25+ if ( data instanceof Uint8Array ) {
26+ return data ;
27+ }
28+ if ( ArrayBuffer . isView ( data ) ) {
29+ return new Uint8Array ( data . buffer , data . byteOffset , data . byteLength ) ;
30+ }
31+ if ( data instanceof ArrayBuffer ) {
32+ return new Uint8Array ( data ) ;
33+ }
34+ if ( Array . isArray ( data ) ) {
35+ return Uint8Array . from ( data ) ;
36+ }
37+ throw new TypeError ( "Unsupported data type for TCP send" ) ;
38+ }
39+
1640class CapacitorSocket extends EventTarget {
1741 constructor ( ) {
1842 super ( ) ;
@@ -95,16 +119,31 @@ class CapacitorSocket extends EventTarget {
95119 }
96120 */
97121
98- const url = new URL ( path ) ;
99- const host = url . hostname ;
100- const port = Number . parseInt ( url . port , 10 ) ;
122+ let host ;
123+ let port ;
101124
102- console . log ( `${ this . logHead } Connecting to ${ path } ` ) ;
125+ try {
126+ const normalizedPath = path . includes ( "://" ) ? path : `tcp://${ path } ` ;
127+ const url = new URL ( normalizedPath ) ;
128+ host = url . hostname ;
129+ const parsedPort = url . port ? Number . parseInt ( url . port , 10 ) : Number . NaN ;
130+ const fallbackPort = Number . isNaN ( parsedPort ) ? Number . parseInt ( options ?. port , 10 ) : parsedPort ;
131+ if ( Number . isNaN ( fallbackPort ) ) {
132+ throw new Error ( `Invalid port in path: ${ path } ` ) ;
133+ }
134+ port = fallbackPort ;
135+ } catch ( parseError ) {
136+ console . error ( `${ this . logHead } Invalid TCP address: ${ path } ` , parseError ) ;
137+ this . dispatchEvent ( new CustomEvent ( "connect" , { detail : false } ) ) ;
138+ return ;
139+ }
140+
141+ console . log ( `${ this . logHead } Connecting to ${ host } :${ port } ` ) ;
103142
104143 try {
105144 const result = await Capacitor . Plugins . BetaflightTcp . connect ( { ip : host , port } ) ;
106145 if ( result ?. success ) {
107- this . address = path ;
146+ this . address = ` ${ host } : ${ port } ` ;
108147 this . connected = true ;
109148 } else {
110149 throw new Error ( "Connect failed" ) ;
@@ -151,15 +190,17 @@ class CapacitorSocket extends EventTarget {
151190 */
152191
153192 if ( this . connected ) {
193+ const bytes = normalizeToUint8Array ( data ) ;
154194 try {
155- const res = await Capacitor . Plugins . BetaflightTcp . send ( { data } ) ;
195+ const payload = uint8ArrayToBase64 ( bytes ) ;
196+ const res = await Capacitor . Plugins . BetaflightTcp . send ( { data : payload } ) ;
156197
157198 if ( res . success ) {
158- this . bytesSent += data . byteLength ;
199+ this . bytesSent += bytes . byteLength ;
159200 if ( cb ) {
160201 cb ( {
161202 error : null ,
162- bytesSent : data . byteLength ,
203+ bytesSent : bytes . byteLength ,
163204 } ) ;
164205 }
165206 } else {
@@ -175,10 +216,14 @@ class CapacitorSocket extends EventTarget {
175216 } ) ;
176217 }
177218 }
219+
220+ return {
221+ bytesSent : bytes . byteLength ,
222+ } ;
178223 }
179224
180225 return {
181- bytesSent : this . connected ? data . byteLength : 0 ,
226+ bytesSent : 0 ,
182227 } ;
183228 }
184229}
0 commit comments