Skip to content

Commit eb322fa

Browse files
committed
Add custom Capacitor plugin-socket
1 parent 16c3b2e commit eb322fa

File tree

6 files changed

+127
-0
lines changed

6 files changed

+127
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package betaflight.configurator;
22

33
import com.getcapacitor.BridgeActivity;
4+
import betaflight.configurator.plugin.SocketPlugin;
45

56
public class MainActivity extends BridgeActivity {}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package betaflight.configurator.plugin;
2+
3+
import android.util.Log;
4+
5+
import com.getcapacitor.JSObject;
6+
import com.getcapacitor.Plugin;
7+
import com.getcapacitor.PluginCall;
8+
import com.getcapacitor.annotation.CapacitorPlugin;
9+
10+
import java.io.*;
11+
import java.net.Socket;
12+
13+
@CapacitorPlugin(name = "SocketPlugin")
14+
public class SocketPlugin extends Plugin {
15+
private Socket socket;
16+
private BufferedReader reader;
17+
private BufferedWriter writer;
18+
19+
@PluginMethod
20+
public void connect(PluginCall call) {
21+
String ip = call.getString("ip");
22+
int port = call.getInt("port");
23+
try {
24+
socket = new Socket(ip, port);
25+
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
26+
writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
27+
JSObject ret = new JSObject();
28+
ret.put("success", true);
29+
call.resolve(ret);
30+
} catch (Exception e) {
31+
call.reject("Connection failed: " + e.getMessage());
32+
}
33+
}
34+
35+
@PluginMethod
36+
public void send(PluginCall call) {
37+
String data = call.getString("data");
38+
try {
39+
writer.write(data);
40+
writer.flush();
41+
JSObject ret = new JSObject();
42+
ret.put("success", true);
43+
call.resolve(ret);
44+
} catch (Exception e) {
45+
call.reject("Send failed: " + e.getMessage());
46+
}
47+
}
48+
49+
@PluginMethod
50+
public void receive(PluginCall call) {
51+
try {
52+
String data = reader.readLine();
53+
JSObject ret = new JSObject();
54+
ret.put("data", data);
55+
call.resolve(ret);
56+
} catch (Exception e) {
57+
call.reject("Receive failed: " + e.getMessage());
58+
}
59+
}
60+
61+
@PluginMethod
62+
public void disconnect(PluginCall call) {
63+
try {
64+
if (socket != null) socket.close();
65+
JSObject ret = new JSObject();
66+
ret.put("success", true);
67+
call.resolve(ret);
68+
} catch (Exception e) {
69+
call.reject("Disconnect failed: " + e.getMessage());
70+
}
71+
}
72+
}

capacitor-plugin-socket/package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "capacitor-plugin-socket",
3+
"version": "1.0.0",
4+
"description": "A Capacitor plugin for handling raw TCP sockets.",
5+
"main": "index.js",
6+
"scripts": {
7+
"build": "tsc"
8+
},
9+
"keywords": ["capacitor", "plugin", "tcp", "socket"],
10+
"author": "Your Name",
11+
"license": "MIT",
12+
"devDependencies": {
13+
"@capacitor/core": "latest",
14+
"typescript": "^5.0.0"
15+
}
16+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export interface SocketPlugin {
2+
connect(options: { ip: string; port: number }): Promise<{ success: boolean }>;
3+
send(options: { data: string }): Promise<{ success: boolean }>;
4+
receive(): Promise<{ data: string }>;
5+
disconnect(): Promise<{ success: boolean }>;
6+
}

capacitor-plugin-socket/src/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { registerPlugin } from '@capacitor/core';
2+
3+
const SocketPlugin = registerPlugin('SocketPlugin', {
4+
web: () => import('./web').then(m => new m.SocketPluginWeb()),
5+
});
6+
7+
export * from './definitions';
8+
export { SocketPlugin };

capacitor-plugin-socket/src/web.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { WebPlugin } from '@capacitor/core';
2+
import { SocketPlugin } from './definitions';
3+
4+
export class SocketPluginWeb extends WebPlugin implements SocketPlugin {
5+
async connect(options: { ip: string; port: number }): Promise<{ success: boolean }> {
6+
console.log('Web implementation does not support raw TCP sockets.', options);
7+
return { success: false };
8+
}
9+
10+
async send(options: { data: string }): Promise<{ success: boolean }> {
11+
console.log('Web implementation does not support raw TCP sockets.', options);
12+
return { success: false };
13+
}
14+
15+
async receive(): Promise<{ data: string }> {
16+
console.log('Web implementation does not support raw TCP sockets.');
17+
return { data: '' };
18+
}
19+
20+
async disconnect(): Promise<{ success: boolean }> {
21+
console.log('Web implementation does not support raw TCP sockets.');
22+
return { success: false };
23+
}
24+
}

0 commit comments

Comments
 (0)