Skip to content

Commit acfa06d

Browse files
committed
Rabbit fixes
1 parent eb322fa commit acfa06d

File tree

3 files changed

+84
-7
lines changed

3 files changed

+84
-7
lines changed
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
package betaflight.configurator;
22

3+
import android.os.Bundle;
34
import com.getcapacitor.BridgeActivity;
45
import betaflight.configurator.plugin.SocketPlugin;
56

6-
public class MainActivity extends BridgeActivity {}
7+
public class MainActivity extends BridgeActivity {
8+
@Override
9+
public void onCreate(Bundle savedInstanceState) {
10+
super.onCreate(savedInstanceState);
11+
registerPlugin(SocketPlugin.class);
12+
}
13+
}

android/app/src/main/java/betaflight/configurator/plugin/SocketPlugin.java

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,38 +10,79 @@
1010
import java.io.*;
1111
import java.net.Socket;
1212

13+
/**
14+
* Capacitor plugin that provides raw TCP socket functionality.
15+
* Implements methods to connect, send, receive, and disconnect.
16+
*/
1317
@CapacitorPlugin(name = "SocketPlugin")
1418
public class SocketPlugin extends Plugin {
1519
private Socket socket;
1620
private BufferedReader reader;
1721
private BufferedWriter writer;
22+
private boolean isConnected = false;
1823

1924
@PluginMethod
2025
public void connect(PluginCall call) {
2126
String ip = call.getString("ip");
2227
int port = call.getInt("port");
28+
29+
// Validate inputs
30+
if (ip == null || ip.isEmpty()) {
31+
call.reject("IP address is required");
32+
return;
33+
}
34+
35+
if (port <= 0 || port > 65535) {
36+
call.reject("Invalid port number");
37+
return;
38+
}
39+
40+
// Prevent duplicate connections
41+
if (socket != null && !socket.isClosed()) {
42+
call.reject("Already connected; please disconnect first");
43+
return;
44+
}
45+
2346
try {
2447
socket = new Socket(ip, port);
48+
socket.setSoTimeout(30_000); // 30s timeout
2549
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
2650
writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
51+
isConnected = true;
2752
JSObject ret = new JSObject();
2853
ret.put("success", true);
2954
call.resolve(ret);
3055
} catch (Exception e) {
56+
closeResources();
3157
call.reject("Connection failed: " + e.getMessage());
3258
}
3359
}
3460

35-
@PluginMethod
61+
@PluginMethod
3662
public void send(PluginCall call) {
3763
String data = call.getString("data");
64+
65+
// Validate input
66+
if (data == null) {
67+
call.reject("Data is required");
68+
return;
69+
}
70+
71+
// Check connection state
72+
if (socket == null || socket.isClosed() || !isConnected) {
73+
call.reject("Not connected to any server");
74+
return;
75+
}
76+
3877
try {
3978
writer.write(data);
4079
writer.flush();
4180
JSObject ret = new JSObject();
4281
ret.put("success", true);
4382
call.resolve(ret);
4483
} catch (Exception e) {
84+
closeResources();
85+
isConnected = false;
4586
call.reject("Send failed: " + e.getMessage());
4687
}
4788
}
@@ -61,12 +102,36 @@ public void receive(PluginCall call) {
61102
@PluginMethod
62103
public void disconnect(PluginCall call) {
63104
try {
64-
if (socket != null) socket.close();
105+
closeResources();
106+
isConnected = false;
65107
JSObject ret = new JSObject();
66108
ret.put("success", true);
67109
call.resolve(ret);
68110
} catch (Exception e) {
69111
call.reject("Disconnect failed: " + e.getMessage());
70112
}
71113
}
114+
115+
/**
116+
* Helper method to close all resources and clean up state
117+
*/
118+
private void closeResources() {
119+
try {
120+
if (reader != null) {
121+
reader.close();
122+
reader = null;
123+
}
124+
if (writer != null) {
125+
writer.close();
126+
writer = null;
127+
}
128+
if (socket != null) {
129+
socket.close();
130+
socket = null;
131+
}
132+
} catch (IOException e) {
133+
// Log but continue cleanup
134+
getContext().getActivity().runOnUiThread(() ->
135+
Log.e("SocketPlugin", "Error closing resources", e));
136+
}
72137
}

capacitor-plugin-socket/package.json

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,20 @@
22
"name": "capacitor-plugin-socket",
33
"version": "1.0.0",
44
"description": "A Capacitor plugin for handling raw TCP sockets.",
5-
"main": "index.js",
5+
"main": "dist/index.js",
6+
"types": "dist/index.d.ts",
7+
"files": ["dist/*", "package.json", "README.md"],
68
"scripts": {
7-
"build": "tsc"
9+
"clean": "rimraf dist",
10+
"build": "npm run clean && tsc -p tsconfig.json"
811
},
912
"keywords": ["capacitor", "plugin", "tcp", "socket"],
10-
"author": "Your Name",
13+
"author": "Betaflight <dev.betaflight.com>",
1114
"license": "MIT",
15+
"peerDependencies": {
16+
"@capacitor/core": "^5.0.0"
17+
},
1218
"devDependencies": {
13-
"@capacitor/core": "latest",
1419
"typescript": "^5.0.0"
1520
}
1621
}

0 commit comments

Comments
 (0)