@cappitolian/network-discovery 0.0.6 → 0.0.7

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.
@@ -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;
6
7
  import android.util.Log;
7
8
 
8
9
  import com.getcapacitor.JSObject;
9
10
  import com.getcapacitor.Plugin;
10
11
 
11
12
  import org.json.JSONArray;
12
- import org.json.JSONException;
13
13
 
14
14
  import java.net.InetAddress;
15
15
  import java.util.Map;
@@ -18,6 +18,7 @@ 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;
21
22
  private NsdManager.RegistrationListener registrationListener;
22
23
  private NsdManager.DiscoveryListener discoveryListener;
23
24
  private NsdServiceInfo serviceInfo;
@@ -26,6 +27,14 @@ public class NetworkDiscovery {
26
27
  public NetworkDiscovery(Plugin plugin, Context context) {
27
28
  this.plugin = plugin;
28
29
  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
+ }
29
38
  }
30
39
 
31
40
  public void startAdvertising(
@@ -35,6 +44,9 @@ public class NetworkDiscovery {
35
44
  JSObject txtRecord,
36
45
  AdvertisingCallback callback
37
46
  ) {
47
+ // 1. Adquirir el lock antes de publicar
48
+ acquireMulticastLock();
49
+
38
50
  serviceInfo = new NsdServiceInfo();
39
51
  serviceInfo.setServiceName(serviceName);
40
52
  serviceInfo.setServiceType(serviceType);
@@ -44,7 +56,7 @@ public class NetworkDiscovery {
44
56
  if (txtRecord != null) {
45
57
  Iterator<String> keys = txtRecord.keys();
46
58
  while (keys.hasNext()) {
47
- String key = keys.next();
59
+ String key = keys.next();
48
60
  try {
49
61
  String value = txtRecord.getString(key);
50
62
  serviceInfo.setAttribute(key, value);
@@ -58,6 +70,7 @@ public class NetworkDiscovery {
58
70
  @Override
59
71
  public void onRegistrationFailed(NsdServiceInfo serviceInfo, int errorCode) {
60
72
  Log.e(TAG, "Service registration failed: " + errorCode);
73
+ releaseMulticastLock(); // Liberar si falla
61
74
  callback.onError("Registration failed with error code: " + errorCode);
62
75
  }
63
76
 
@@ -75,6 +88,7 @@ public class NetworkDiscovery {
75
88
  @Override
76
89
  public void onServiceUnregistered(NsdServiceInfo serviceInfo) {
77
90
  Log.d(TAG, "Service unregistered");
91
+ releaseMulticastLock();
78
92
  }
79
93
  };
80
94
 
@@ -86,8 +100,10 @@ public class NetworkDiscovery {
86
100
  try {
87
101
  nsdManager.unregisterService(registrationListener);
88
102
  registrationListener = null;
103
+ // El lock se libera en el callback onServiceUnregistered
89
104
  callback.onSuccess();
90
105
  } catch (Exception e) {
106
+ releaseMulticastLock();
91
107
  callback.onError("Error stopping advertising: " + e.getMessage());
92
108
  }
93
109
  } else {
@@ -96,11 +112,15 @@ public class NetworkDiscovery {
96
112
  }
97
113
 
98
114
  public void startDiscovery(String serviceType, DiscoveryCallback callback) {
115
+ // También es recomendable adquirir el lock durante el discovery
116
+ acquireMulticastLock();
117
+
99
118
  discoveryListener = new NsdManager.DiscoveryListener() {
100
119
  @Override
101
120
  public void onStartDiscoveryFailed(String serviceType, int errorCode) {
102
121
  Log.e(TAG, "Discovery start failed: " + errorCode);
103
122
  nsdManager.stopServiceDiscovery(this);
123
+ releaseMulticastLock();
104
124
  callback.onError("Discovery failed with error code: " + errorCode);
105
125
  }
106
126
 
@@ -118,6 +138,7 @@ public class NetworkDiscovery {
118
138
  @Override
119
139
  public void onDiscoveryStopped(String serviceType) {
120
140
  Log.d(TAG, "Service discovery stopped");
141
+ releaseMulticastLock();
121
142
  }
122
143
 
123
144
  @Override
@@ -133,7 +154,6 @@ public class NetworkDiscovery {
133
154
  @Override
134
155
  public void onServiceResolved(NsdServiceInfo serviceInfo) {
135
156
  Log.d(TAG, "Service resolved: " + serviceInfo);
136
-
137
157
  JSObject serviceData = buildServiceObject(serviceInfo);
138
158
  callback.onServiceFound(serviceData);
139
159
  }
@@ -143,11 +163,9 @@ public class NetworkDiscovery {
143
163
  @Override
144
164
  public void onServiceLost(NsdServiceInfo service) {
145
165
  Log.d(TAG, "Service lost: " + service.getServiceName());
146
-
147
166
  JSObject serviceData = new JSObject();
148
167
  serviceData.put("serviceName", service.getServiceName());
149
168
  serviceData.put("serviceType", service.getServiceType());
150
-
151
169
  callback.onServiceLost(serviceData);
152
170
  }
153
171
  };
@@ -162,6 +180,7 @@ public class NetworkDiscovery {
162
180
  discoveryListener = null;
163
181
  callback.onSuccess();
164
182
  } catch (Exception e) {
183
+ releaseMulticastLock();
165
184
  callback.onError("Error stopping discovery: " + e.getMessage());
166
185
  }
167
186
  } else {
@@ -169,6 +188,20 @@ public class NetworkDiscovery {
169
188
  }
170
189
  }
171
190
 
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
+
172
205
  private JSObject buildServiceObject(NsdServiceInfo serviceInfo) {
173
206
  JSObject serviceData = new JSObject();
174
207
  serviceData.put("serviceName", serviceInfo.getServiceName());
@@ -176,7 +209,6 @@ public class NetworkDiscovery {
176
209
  serviceData.put("hostName", serviceInfo.getHost() != null ? serviceInfo.getHost().getHostName() : "");
177
210
  serviceData.put("port", serviceInfo.getPort());
178
211
 
179
- // Agregar direcciones IP
180
212
  InetAddress host = serviceInfo.getHost();
181
213
  if (host != null) {
182
214
  JSONArray addresses = new JSONArray();
@@ -184,7 +216,6 @@ public class NetworkDiscovery {
184
216
  serviceData.put("addresses", addresses);
185
217
  }
186
218
 
187
- // Agregar TXT records
188
219
  Map<String, byte[]> attributes = serviceInfo.getAttributes();
189
220
  if (attributes != null && !attributes.isEmpty()) {
190
221
  JSObject txtRecordObj = new JSObject();
@@ -197,7 +228,6 @@ public class NetworkDiscovery {
197
228
  return serviceData;
198
229
  }
199
230
 
200
- // Callbacks interfaces
201
231
  public interface AdvertisingCallback {
202
232
  void onSuccess();
203
233
  void onError(String error);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cappitolian/network-discovery",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
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",