@cappitolian/network-discovery 0.0.7 → 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/android/src/main/java/com/cappitolian/plugins/networkdiscovery/NetworkDiscovery.java
CHANGED
|
@@ -3,13 +3,13 @@ package com.cappitolian.plugins.networkdiscovery;
|
|
|
3
3
|
import android.content.Context;
|
|
4
4
|
import android.net.nsd.NsdManager;
|
|
5
5
|
import android.net.nsd.NsdServiceInfo;
|
|
6
|
-
import android.net.wifi.WifiManager;
|
|
7
6
|
import android.util.Log;
|
|
8
7
|
|
|
9
8
|
import com.getcapacitor.JSObject;
|
|
10
9
|
import com.getcapacitor.Plugin;
|
|
11
10
|
|
|
12
11
|
import org.json.JSONArray;
|
|
12
|
+
import org.json.JSONException;
|
|
13
13
|
|
|
14
14
|
import java.net.InetAddress;
|
|
15
15
|
import java.util.Map;
|
|
@@ -18,7 +18,6 @@ import java.util.Iterator;
|
|
|
18
18
|
public class NetworkDiscovery {
|
|
19
19
|
private static final String TAG = "NetworkDiscovery";
|
|
20
20
|
private NsdManager nsdManager;
|
|
21
|
-
private WifiManager.MulticastLock multicastLock;
|
|
22
21
|
private NsdManager.RegistrationListener registrationListener;
|
|
23
22
|
private NsdManager.DiscoveryListener discoveryListener;
|
|
24
23
|
private NsdServiceInfo serviceInfo;
|
|
@@ -27,14 +26,6 @@ public class NetworkDiscovery {
|
|
|
27
26
|
public NetworkDiscovery(Plugin plugin, Context context) {
|
|
28
27
|
this.plugin = plugin;
|
|
29
28
|
this.nsdManager = (NsdManager) context.getSystemService(Context.NSD_SERVICE);
|
|
30
|
-
|
|
31
|
-
// Configuración del MulticastLock
|
|
32
|
-
// Es vital para que Android responda a las peticiones mDNS de iOS
|
|
33
|
-
WifiManager wifi = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
|
|
34
|
-
if (wifi != null) {
|
|
35
|
-
this.multicastLock = wifi.createMulticastLock("ssspos_discovery_lock");
|
|
36
|
-
this.multicastLock.setReferenceCounted(true);
|
|
37
|
-
}
|
|
38
29
|
}
|
|
39
30
|
|
|
40
31
|
public void startAdvertising(
|
|
@@ -44,9 +35,6 @@ public class NetworkDiscovery {
|
|
|
44
35
|
JSObject txtRecord,
|
|
45
36
|
AdvertisingCallback callback
|
|
46
37
|
) {
|
|
47
|
-
// 1. Adquirir el lock antes de publicar
|
|
48
|
-
acquireMulticastLock();
|
|
49
|
-
|
|
50
38
|
serviceInfo = new NsdServiceInfo();
|
|
51
39
|
serviceInfo.setServiceName(serviceName);
|
|
52
40
|
serviceInfo.setServiceType(serviceType);
|
|
@@ -56,7 +44,7 @@ public class NetworkDiscovery {
|
|
|
56
44
|
if (txtRecord != null) {
|
|
57
45
|
Iterator<String> keys = txtRecord.keys();
|
|
58
46
|
while (keys.hasNext()) {
|
|
59
|
-
|
|
47
|
+
String key = keys.next();
|
|
60
48
|
try {
|
|
61
49
|
String value = txtRecord.getString(key);
|
|
62
50
|
serviceInfo.setAttribute(key, value);
|
|
@@ -70,7 +58,6 @@ public class NetworkDiscovery {
|
|
|
70
58
|
@Override
|
|
71
59
|
public void onRegistrationFailed(NsdServiceInfo serviceInfo, int errorCode) {
|
|
72
60
|
Log.e(TAG, "Service registration failed: " + errorCode);
|
|
73
|
-
releaseMulticastLock(); // Liberar si falla
|
|
74
61
|
callback.onError("Registration failed with error code: " + errorCode);
|
|
75
62
|
}
|
|
76
63
|
|
|
@@ -88,7 +75,6 @@ public class NetworkDiscovery {
|
|
|
88
75
|
@Override
|
|
89
76
|
public void onServiceUnregistered(NsdServiceInfo serviceInfo) {
|
|
90
77
|
Log.d(TAG, "Service unregistered");
|
|
91
|
-
releaseMulticastLock();
|
|
92
78
|
}
|
|
93
79
|
};
|
|
94
80
|
|
|
@@ -100,10 +86,8 @@ public class NetworkDiscovery {
|
|
|
100
86
|
try {
|
|
101
87
|
nsdManager.unregisterService(registrationListener);
|
|
102
88
|
registrationListener = null;
|
|
103
|
-
// El lock se libera en el callback onServiceUnregistered
|
|
104
89
|
callback.onSuccess();
|
|
105
90
|
} catch (Exception e) {
|
|
106
|
-
releaseMulticastLock();
|
|
107
91
|
callback.onError("Error stopping advertising: " + e.getMessage());
|
|
108
92
|
}
|
|
109
93
|
} else {
|
|
@@ -112,15 +96,11 @@ public class NetworkDiscovery {
|
|
|
112
96
|
}
|
|
113
97
|
|
|
114
98
|
public void startDiscovery(String serviceType, DiscoveryCallback callback) {
|
|
115
|
-
// También es recomendable adquirir el lock durante el discovery
|
|
116
|
-
acquireMulticastLock();
|
|
117
|
-
|
|
118
99
|
discoveryListener = new NsdManager.DiscoveryListener() {
|
|
119
100
|
@Override
|
|
120
101
|
public void onStartDiscoveryFailed(String serviceType, int errorCode) {
|
|
121
102
|
Log.e(TAG, "Discovery start failed: " + errorCode);
|
|
122
103
|
nsdManager.stopServiceDiscovery(this);
|
|
123
|
-
releaseMulticastLock();
|
|
124
104
|
callback.onError("Discovery failed with error code: " + errorCode);
|
|
125
105
|
}
|
|
126
106
|
|
|
@@ -138,7 +118,6 @@ public class NetworkDiscovery {
|
|
|
138
118
|
@Override
|
|
139
119
|
public void onDiscoveryStopped(String serviceType) {
|
|
140
120
|
Log.d(TAG, "Service discovery stopped");
|
|
141
|
-
releaseMulticastLock();
|
|
142
121
|
}
|
|
143
122
|
|
|
144
123
|
@Override
|
|
@@ -154,6 +133,7 @@ public class NetworkDiscovery {
|
|
|
154
133
|
@Override
|
|
155
134
|
public void onServiceResolved(NsdServiceInfo serviceInfo) {
|
|
156
135
|
Log.d(TAG, "Service resolved: " + serviceInfo);
|
|
136
|
+
|
|
157
137
|
JSObject serviceData = buildServiceObject(serviceInfo);
|
|
158
138
|
callback.onServiceFound(serviceData);
|
|
159
139
|
}
|
|
@@ -163,9 +143,11 @@ public class NetworkDiscovery {
|
|
|
163
143
|
@Override
|
|
164
144
|
public void onServiceLost(NsdServiceInfo service) {
|
|
165
145
|
Log.d(TAG, "Service lost: " + service.getServiceName());
|
|
146
|
+
|
|
166
147
|
JSObject serviceData = new JSObject();
|
|
167
148
|
serviceData.put("serviceName", service.getServiceName());
|
|
168
149
|
serviceData.put("serviceType", service.getServiceType());
|
|
150
|
+
|
|
169
151
|
callback.onServiceLost(serviceData);
|
|
170
152
|
}
|
|
171
153
|
};
|
|
@@ -180,7 +162,6 @@ public class NetworkDiscovery {
|
|
|
180
162
|
discoveryListener = null;
|
|
181
163
|
callback.onSuccess();
|
|
182
164
|
} catch (Exception e) {
|
|
183
|
-
releaseMulticastLock();
|
|
184
165
|
callback.onError("Error stopping discovery: " + e.getMessage());
|
|
185
166
|
}
|
|
186
167
|
} else {
|
|
@@ -188,20 +169,6 @@ public class NetworkDiscovery {
|
|
|
188
169
|
}
|
|
189
170
|
}
|
|
190
171
|
|
|
191
|
-
private void acquireMulticastLock() {
|
|
192
|
-
if (multicastLock != null && !multicastLock.isHeld()) {
|
|
193
|
-
multicastLock.acquire();
|
|
194
|
-
Log.d(TAG, "MulticastLock acquired");
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
private void releaseMulticastLock() {
|
|
199
|
-
if (multicastLock != null && multicastLock.isHeld()) {
|
|
200
|
-
multicastLock.release();
|
|
201
|
-
Log.d(TAG, "MulticastLock released");
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
|
|
205
172
|
private JSObject buildServiceObject(NsdServiceInfo serviceInfo) {
|
|
206
173
|
JSObject serviceData = new JSObject();
|
|
207
174
|
serviceData.put("serviceName", serviceInfo.getServiceName());
|
|
@@ -209,6 +176,7 @@ public class NetworkDiscovery {
|
|
|
209
176
|
serviceData.put("hostName", serviceInfo.getHost() != null ? serviceInfo.getHost().getHostName() : "");
|
|
210
177
|
serviceData.put("port", serviceInfo.getPort());
|
|
211
178
|
|
|
179
|
+
// Agregar direcciones IP
|
|
212
180
|
InetAddress host = serviceInfo.getHost();
|
|
213
181
|
if (host != null) {
|
|
214
182
|
JSONArray addresses = new JSONArray();
|
|
@@ -216,6 +184,7 @@ public class NetworkDiscovery {
|
|
|
216
184
|
serviceData.put("addresses", addresses);
|
|
217
185
|
}
|
|
218
186
|
|
|
187
|
+
// Agregar TXT records
|
|
219
188
|
Map<String, byte[]> attributes = serviceInfo.getAttributes();
|
|
220
189
|
if (attributes != null && !attributes.isEmpty()) {
|
|
221
190
|
JSObject txtRecordObj = new JSObject();
|
|
@@ -228,6 +197,7 @@ public class NetworkDiscovery {
|
|
|
228
197
|
return serviceData;
|
|
229
198
|
}
|
|
230
199
|
|
|
200
|
+
// Callbacks interfaces
|
|
231
201
|
public interface AdvertisingCallback {
|
|
232
202
|
void onSuccess();
|
|
233
203
|
void onError(String error);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cappitolian/network-discovery",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.8",
|
|
4
4
|
"description": "A Capacitor plugin for network service discovery using mDNS/Bonjour. Allows automatic server-client connection without manual IP configuration.",
|
|
5
5
|
"main": "dist/plugin.cjs.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|