1
1
package betaflight .configurator .plugin ;
2
2
3
- import android .util .Log ;
4
-
5
3
import com .getcapacitor .JSObject ;
6
4
import com .getcapacitor .Plugin ;
7
5
import com .getcapacitor .PluginCall ;
10
8
import java .io .*;
11
9
import java .net .Socket ;
12
10
11
+ /**
12
+ * Capacitor plugin that provides raw TCP socket functionality.
13
+ * Implements methods to connect, send, receive, and disconnect.
14
+ */
13
15
@ CapacitorPlugin (name = "SocketPlugin" )
14
16
public class SocketPlugin extends Plugin {
15
17
private Socket socket ;
16
18
private BufferedReader reader ;
17
19
private BufferedWriter writer ;
20
+ private boolean isConnected = false ;
18
21
19
22
@ PluginMethod
20
23
public void connect (PluginCall call ) {
21
24
String ip = call .getString ("ip" );
22
25
int port = call .getInt ("port" );
26
+
27
+ // Validate inputs
28
+ if (ip == null || ip .isEmpty ()) {
29
+ call .reject ("IP address is required" );
30
+ return ;
31
+ }
32
+
33
+ if (port <= 0 || port > 65535 ) {
34
+ call .reject ("Invalid port number" );
35
+ return ;
36
+ }
37
+
38
+ // Prevent duplicate connections
39
+ if (socket != null && !socket .isClosed ()) {
40
+ call .reject ("Already connected; please disconnect first" );
41
+ return ;
42
+ }
43
+
23
44
try {
24
45
socket = new Socket (ip , port );
46
+ socket .setSoTimeout (30_000 ); // 30s timeout
25
47
reader = new BufferedReader (new InputStreamReader (socket .getInputStream ()));
26
48
writer = new BufferedWriter (new OutputStreamWriter (socket .getOutputStream ()));
27
49
JSObject ret = new JSObject ();
28
50
ret .put ("success" , true );
29
51
call .resolve (ret );
30
52
} catch (Exception e ) {
53
+ closeResources ();
31
54
call .reject ("Connection failed: " + e .getMessage ());
32
55
}
33
56
}
34
57
35
- @ PluginMethod
58
+ @ PluginMethod
36
59
public void send (PluginCall call ) {
37
60
String data = call .getString ("data" );
61
+
62
+ // Validate input
63
+ if (data == null ) {
64
+ call .reject ("Data is required" );
65
+ return ;
66
+ }
67
+
68
+ // Check connection state
69
+ if (socket == null || socket .isClosed () || !isConnected ) {
70
+ call .reject ("Not connected to any server" );
71
+ return ;
72
+ }
73
+
38
74
try {
39
75
writer .write (data );
40
76
writer .flush ();
41
77
JSObject ret = new JSObject ();
42
78
ret .put ("success" , true );
43
79
call .resolve (ret );
44
80
} catch (Exception e ) {
81
+ closeResources ();
82
+ isConnected = false ;
45
83
call .reject ("Send failed: " + e .getMessage ());
46
84
}
47
85
}
@@ -61,12 +99,36 @@ public void receive(PluginCall call) {
61
99
@ PluginMethod
62
100
public void disconnect (PluginCall call ) {
63
101
try {
64
- if (socket != null ) socket .close ();
102
+ closeResources ();
103
+ isConnected = false ;
65
104
JSObject ret = new JSObject ();
66
105
ret .put ("success" , true );
67
106
call .resolve (ret );
68
107
} catch (Exception e ) {
69
108
call .reject ("Disconnect failed: " + e .getMessage ());
70
109
}
71
110
}
111
+
112
+ /**
113
+ * Helper method to close all resources and clean up state
114
+ */
115
+ private void closeResources () {
116
+ try {
117
+ if (reader != null ) {
118
+ reader .close ();
119
+ reader = null ;
120
+ }
121
+ if (writer != null ) {
122
+ writer .close ();
123
+ writer = null ;
124
+ }
125
+ if (socket != null ) {
126
+ socket .close ();
127
+ socket = null ;
128
+ }
129
+ } catch (IOException e ) {
130
+ // Log but continue cleanup
131
+ getContext ().getActivity ().runOnUiThread (() ->
132
+ Log .e ("SocketPlugin" , "Error closing resources" , e ));
133
+ }
72
134
}
0 commit comments