@cappitolian/network-discovery 0.0.8 β†’ 0.0.9

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.
@@ -9,13 +9,13 @@ import com.getcapacitor.JSObject;
9
9
  import com.getcapacitor.Plugin;
10
10
 
11
11
  import org.json.JSONArray;
12
- import org.json.JSONException;
13
12
 
14
13
  import java.net.InetAddress;
15
- import java.util.Map;
16
14
  import java.util.Iterator;
15
+ import java.util.Map;
17
16
 
18
17
  public class NetworkDiscovery {
18
+
19
19
  private static final String TAG = "NetworkDiscovery";
20
20
  private NsdManager nsdManager;
21
21
  private NsdManager.RegistrationListener registrationListener;
@@ -35,19 +35,28 @@ public class NetworkDiscovery {
35
35
  JSObject txtRecord,
36
36
  AdvertisingCallback callback
37
37
  ) {
38
+ // βœ… Normalizar service type (agregar punto final si no lo tiene)
39
+ String normalizedServiceType = serviceType.endsWith(".") ? serviceType : serviceType + ".";
40
+
41
+ Log.d(TAG, "πŸ“‘ Android: Starting advertising");
42
+ Log.d(TAG, " Service name: " + serviceName);
43
+ Log.d(TAG, " Service type (normalized): " + normalizedServiceType);
44
+ Log.d(TAG, " Port: " + port);
45
+
38
46
  serviceInfo = new NsdServiceInfo();
39
47
  serviceInfo.setServiceName(serviceName);
40
- serviceInfo.setServiceType(serviceType);
48
+ serviceInfo.setServiceType(normalizedServiceType);
41
49
  serviceInfo.setPort(port);
42
50
 
43
51
  // Agregar TXT records
44
52
  if (txtRecord != null) {
45
53
  Iterator<String> keys = txtRecord.keys();
46
54
  while (keys.hasNext()) {
47
- String key = keys.next();
55
+ String key = keys.next();
48
56
  try {
49
57
  String value = txtRecord.getString(key);
50
58
  serviceInfo.setAttribute(key, value);
59
+ Log.d(TAG, " TXT Record: " + key + " = " + value);
51
60
  } catch (Exception e) {
52
61
  Log.e(TAG, "Error setting attribute: " + key, e);
53
62
  }
@@ -57,24 +66,27 @@ public class NetworkDiscovery {
57
66
  registrationListener = new NsdManager.RegistrationListener() {
58
67
  @Override
59
68
  public void onRegistrationFailed(NsdServiceInfo serviceInfo, int errorCode) {
60
- Log.e(TAG, "Service registration failed: " + errorCode);
69
+ Log.e(TAG, "❌ Android: Service registration FAILED");
70
+ Log.e(TAG, " Error code: " + errorCode);
61
71
  callback.onError("Registration failed with error code: " + errorCode);
62
72
  }
63
73
 
64
74
  @Override
65
75
  public void onUnregistrationFailed(NsdServiceInfo serviceInfo, int errorCode) {
66
- Log.e(TAG, "Service unregistration failed: " + errorCode);
76
+ Log.e(TAG, "❌ Android: Service unregistration FAILED");
77
+ Log.e(TAG, " Error code: " + errorCode);
67
78
  }
68
79
 
69
80
  @Override
70
81
  public void onServiceRegistered(NsdServiceInfo serviceInfo) {
71
- Log.d(TAG, "Service registered: " + serviceInfo.getServiceName());
82
+ Log.d(TAG, "βœ… Android: Service registered successfully");
83
+ Log.d(TAG, " Service name: " + serviceInfo.getServiceName());
72
84
  callback.onSuccess();
73
85
  }
74
86
 
75
87
  @Override
76
88
  public void onServiceUnregistered(NsdServiceInfo serviceInfo) {
77
- Log.d(TAG, "Service unregistered");
89
+ Log.d(TAG, "β›” Android: Service unregistered");
78
90
  }
79
91
  };
80
92
 
@@ -84,55 +96,75 @@ public class NetworkDiscovery {
84
96
  public void stopAdvertising(StopCallback callback) {
85
97
  if (registrationListener != null) {
86
98
  try {
99
+ Log.d(TAG, "β›” Android: Stopping advertising");
87
100
  nsdManager.unregisterService(registrationListener);
88
101
  registrationListener = null;
89
102
  callback.onSuccess();
90
103
  } catch (Exception e) {
104
+ Log.e(TAG, "❌ Android: Error stopping advertising", e);
91
105
  callback.onError("Error stopping advertising: " + e.getMessage());
92
106
  }
93
107
  } else {
108
+ Log.w(TAG, "⚠️ Android: No active advertising to stop");
94
109
  callback.onError("No active advertising to stop");
95
110
  }
96
111
  }
97
112
 
98
113
  public void startDiscovery(String serviceType, DiscoveryCallback callback) {
114
+ // βœ… Normalizar service type
115
+ String normalizedServiceType = serviceType.endsWith(".") ? serviceType : serviceType + ".";
116
+
117
+ Log.d(TAG, "πŸ” Android: Starting discovery");
118
+ Log.d(TAG, " Service type (normalized): " + normalizedServiceType);
119
+
99
120
  discoveryListener = new NsdManager.DiscoveryListener() {
100
121
  @Override
101
122
  public void onStartDiscoveryFailed(String serviceType, int errorCode) {
102
- Log.e(TAG, "Discovery start failed: " + errorCode);
123
+ Log.e(TAG, "❌ Android: Discovery start FAILED");
124
+ Log.e(TAG, " Service type: " + serviceType);
125
+ Log.e(TAG, " Error code: " + errorCode);
103
126
  nsdManager.stopServiceDiscovery(this);
104
127
  callback.onError("Discovery failed with error code: " + errorCode);
105
128
  }
106
129
 
107
130
  @Override
108
131
  public void onStopDiscoveryFailed(String serviceType, int errorCode) {
109
- Log.e(TAG, "Discovery stop failed: " + errorCode);
132
+ Log.e(TAG, "❌ Android: Discovery stop FAILED");
133
+ Log.e(TAG, " Error code: " + errorCode);
110
134
  }
111
135
 
112
136
  @Override
113
137
  public void onDiscoveryStarted(String serviceType) {
114
- Log.d(TAG, "Service discovery started");
138
+ Log.d(TAG, "βœ… Android: Service discovery STARTED");
139
+ Log.d(TAG, " Service type: " + serviceType);
115
140
  callback.onDiscoveryStarted();
116
141
  }
117
142
 
118
143
  @Override
119
144
  public void onDiscoveryStopped(String serviceType) {
120
- Log.d(TAG, "Service discovery stopped");
145
+ Log.d(TAG, "β›” Android: Service discovery STOPPED");
121
146
  }
122
147
 
123
148
  @Override
124
149
  public void onServiceFound(NsdServiceInfo service) {
125
- Log.d(TAG, "Service found: " + service.getServiceName());
150
+ Log.d(TAG, "βœ… Android: Service FOUND!");
151
+ Log.d(TAG, " Name: " + service.getServiceName());
152
+ Log.d(TAG, " Type: " + service.getServiceType());
126
153
 
127
154
  nsdManager.resolveService(service, new NsdManager.ResolveListener() {
128
155
  @Override
129
156
  public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) {
130
- Log.e(TAG, "Resolve failed: " + errorCode);
157
+ Log.e(TAG, "❌ Android: Resolve FAILED");
158
+ Log.e(TAG, " Service: " + serviceInfo.getServiceName());
159
+ Log.e(TAG, " Error code: " + errorCode);
131
160
  }
132
161
 
133
162
  @Override
134
163
  public void onServiceResolved(NsdServiceInfo serviceInfo) {
135
- Log.d(TAG, "Service resolved: " + serviceInfo);
164
+ Log.d(TAG, "βœ… Android: Service RESOLVED");
165
+ Log.d(TAG, " Name: " + serviceInfo.getServiceName());
166
+ Log.d(TAG, " Host: " + (serviceInfo.getHost() != null ? serviceInfo.getHost().getHostAddress() : "null"));
167
+ Log.d(TAG, " Port: " + serviceInfo.getPort());
136
168
 
137
169
  JSObject serviceData = buildServiceObject(serviceInfo);
138
170
  callback.onServiceFound(serviceData);
@@ -142,7 +174,8 @@ public class NetworkDiscovery {
142
174
 
143
175
  @Override
144
176
  public void onServiceLost(NsdServiceInfo service) {
145
- Log.d(TAG, "Service lost: " + service.getServiceName());
177
+ Log.d(TAG, "β›” Android: Service LOST");
178
+ Log.d(TAG, " Name: " + service.getServiceName());
146
179
 
147
180
  JSObject serviceData = new JSObject();
148
181
  serviceData.put("serviceName", service.getServiceName());
@@ -152,19 +185,22 @@ public class NetworkDiscovery {
152
185
  }
153
186
  };
154
187
 
155
- nsdManager.discoverServices(serviceType, NsdManager.PROTOCOL_DNS_SD, discoveryListener);
188
+ nsdManager.discoverServices(normalizedServiceType, NsdManager.PROTOCOL_DNS_SD, discoveryListener);
156
189
  }
157
190
 
158
191
  public void stopDiscovery(StopCallback callback) {
159
192
  if (discoveryListener != null) {
160
193
  try {
194
+ Log.d(TAG, "β›” Android: Stopping discovery");
161
195
  nsdManager.stopServiceDiscovery(discoveryListener);
162
196
  discoveryListener = null;
163
197
  callback.onSuccess();
164
198
  } catch (Exception e) {
199
+ Log.e(TAG, "❌ Android: Error stopping discovery", e);
165
200
  callback.onError("Error stopping discovery: " + e.getMessage());
166
201
  }
167
202
  } else {
203
+ Log.w(TAG, "⚠️ Android: No active discovery to stop");
168
204
  callback.onError("No active discovery to stop");
169
205
  }
170
206
  }
@@ -173,23 +209,20 @@ public class NetworkDiscovery {
173
209
  JSObject serviceData = new JSObject();
174
210
  serviceData.put("serviceName", serviceInfo.getServiceName());
175
211
  serviceData.put("serviceType", serviceInfo.getServiceType());
212
+ serviceData.put("domain", "local.");
176
213
  serviceData.put("hostName", serviceInfo.getHost() != null ? serviceInfo.getHost().getHostName() : "");
177
214
  serviceData.put("port", serviceInfo.getPort());
178
215
 
179
- // Agregar direcciones IP
180
- InetAddress host = serviceInfo.getHost();
181
- if (host != null) {
182
- JSONArray addresses = new JSONArray();
183
- addresses.put(host.getHostAddress());
184
- serviceData.put("addresses", addresses);
185
- }
216
+ // βœ… NO incluir addresses - Solo usar txtRecord para la IP
186
217
 
187
- // Agregar TXT records
218
+ // Agregar TXT records (aquΓ­ estΓ‘ tu IP correcta)
188
219
  Map<String, byte[]> attributes = serviceInfo.getAttributes();
189
220
  if (attributes != null && !attributes.isEmpty()) {
190
221
  JSObject txtRecordObj = new JSObject();
191
222
  for (Map.Entry<String, byte[]> entry : attributes.entrySet()) {
192
- txtRecordObj.put(entry.getKey(), new String(entry.getValue()));
223
+ String value = new String(entry.getValue());
224
+ txtRecordObj.put(entry.getKey(), value);
225
+ Log.d(TAG, " TXT Record retrieved: " + entry.getKey() + " = " + value);
193
226
  }
194
227
  serviceData.put("txtRecord", txtRecordObj);
195
228
  }
@@ -13,13 +13,22 @@ import Foundation
13
13
  port: Int,
14
14
  txtRecord: [String: String]?
15
15
  ) {
16
- netService = NetService(domain: "local.", type: serviceType, name: serviceName, port: Int32(port))
16
+ // βœ… Normalizar service type (agregar punto final si no lo tiene)
17
+ let normalizedServiceType = serviceType.hasSuffix(".") ? serviceType : serviceType + "."
18
+
19
+ print("πŸ“‘ iOS: Starting advertising")
20
+ print(" Service name: \(serviceName)")
21
+ print(" Service type (normalized): \(normalizedServiceType)")
22
+ print(" Port: \(port)")
23
+
24
+ netService = NetService(domain: "local.", type: normalizedServiceType, name: serviceName, port: Int32(port))
17
25
 
18
26
  // Configurar TXT record
19
27
  if let txtRecord = txtRecord, !txtRecord.isEmpty {
20
28
  var txtData: [String: Data] = [:]
21
29
  for (key, value) in txtRecord {
22
30
  txtData[key] = value.data(using: .utf8)
31
+ print(" TXT Record: \(key) = \(value)")
23
32
  }
24
33
  let txtRecordData = NetService.data(fromTXTRecord: txtData)
25
34
  netService?.setTXTRecord(txtRecordData)
@@ -30,17 +39,26 @@ import Foundation
30
39
  }
31
40
 
32
41
  @objc public func stopAdvertising() {
42
+ print("β›” iOS: Stopping advertising")
33
43
  netService?.stop()
34
44
  netService = nil
35
45
  }
36
46
 
37
47
  @objc public func startDiscovery(serviceType: String, domain: String = "local.") {
48
+ // βœ… Normalizar service type
49
+ let normalizedServiceType = serviceType.hasSuffix(".") ? serviceType : serviceType + "."
50
+
51
+ print("πŸ” iOS: Starting discovery")
52
+ print(" Service type (normalized): \(normalizedServiceType)")
53
+ print(" Domain: \(domain)")
54
+
38
55
  netServiceBrowser = NetServiceBrowser()
39
56
  netServiceBrowser?.delegate = self
40
- netServiceBrowser?.searchForServices(ofType: serviceType, inDomain: domain)
57
+ netServiceBrowser?.searchForServices(ofType: normalizedServiceType, inDomain: domain)
41
58
  }
42
59
 
43
60
  @objc public func stopDiscovery() {
61
+ print("β›” iOS: Stopping discovery")
44
62
  netServiceBrowser?.stop()
45
63
  netServiceBrowser = nil
46
64
  discoveredServices.removeAll()
@@ -49,39 +67,60 @@ import Foundation
49
67
 
50
68
  // MARK: - NetServiceDelegate
51
69
  extension NetworkDiscovery: NetServiceDelegate {
70
+ public func netServiceWillPublish(_ sender: NetService) {
71
+ print("πŸ”„ iOS: Service WILL publish: \(sender.name)")
72
+ }
73
+
52
74
  public func netServiceDidPublish(_ sender: NetService) {
53
- print("Service published: \(sender.name)")
75
+ print("βœ… iOS: Service published successfully")
76
+ print(" Name: \(sender.name)")
77
+ print(" Type: \(sender.type)")
78
+ print(" Port: \(sender.port)")
54
79
  delegate?.advertisingDidStart()
55
80
  }
56
81
 
57
82
  public func netService(_ sender: NetService, didNotPublish errorDict: [String : NSNumber]) {
58
- print("Service did not publish: \(errorDict)")
83
+ print("❌ iOS: Service did NOT publish")
84
+ print(" Error dict: \(errorDict)")
59
85
  let errorCode = errorDict[NetService.errorCode]?.intValue ?? -1
86
+ print(" Error code: \(errorCode)")
60
87
  delegate?.advertisingDidFail(error: "Failed to publish service. Error code: \(errorCode)")
61
88
  }
62
89
  }
63
90
 
64
91
  // MARK: - NetServiceBrowserDelegate
65
92
  extension NetworkDiscovery: NetServiceBrowserDelegate {
93
+ public func netServiceBrowserWillSearch(_ browser: NetServiceBrowser) {
94
+ print("πŸ”„ iOS: Browser WILL search")
95
+ }
96
+
66
97
  public func netServiceBrowserDidStopSearch(_ browser: NetServiceBrowser) {
67
- print("Service discovery stopped")
98
+ print("β›” iOS: Browser DID stop search")
68
99
  }
69
100
 
70
101
  public func netServiceBrowser(_ browser: NetServiceBrowser, didNotSearch errorDict: [String : NSNumber]) {
71
- print("Service discovery failed: \(errorDict)")
102
+ print("❌ iOS: Browser did NOT search")
103
+ print(" Error dict: \(errorDict)")
72
104
  let errorCode = errorDict[NetService.errorCode]?.intValue ?? -1
105
+ print(" Error code: \(errorCode)")
73
106
  delegate?.discoveryDidFail(error: "Discovery failed. Error code: \(errorCode)")
74
107
  }
75
108
 
76
109
  public func netServiceBrowser(_ browser: NetServiceBrowser, didFind service: NetService, moreComing: Bool) {
77
- print("Service found: \(service.name)")
110
+ print("βœ… iOS: Service FOUND!")
111
+ print(" Name: \(service.name)")
112
+ print(" Type: \(service.type)")
113
+ print(" Domain: \(service.domain)")
114
+ print(" More coming: \(moreComing)")
115
+
78
116
  discoveredServices.append(service)
79
117
  service.delegate = self
80
118
  service.resolve(withTimeout: 5.0)
81
119
  }
82
120
 
83
121
  public func netServiceBrowser(_ browser: NetServiceBrowser, didRemove service: NetService, moreComing: Bool) {
84
- print("Service lost: \(service.name)")
122
+ print("β›” iOS: Service LOST")
123
+ print(" Name: \(service.name)")
85
124
 
86
125
  let serviceData: [String: Any] = [
87
126
  "serviceName": service.name,
@@ -95,46 +134,33 @@ extension NetworkDiscovery: NetServiceBrowserDelegate {
95
134
  }
96
135
  }
97
136
 
137
+ public func netServiceWillResolve(_ sender: NetService) {
138
+ print("πŸ”„ iOS: Service WILL resolve: \(sender.name)")
139
+ }
140
+
98
141
  public func netServiceDidResolveAddress(_ sender: NetService) {
99
- print("Service resolved: \(sender.name)")
100
-
101
- var addresses: [String] = []
102
-
103
- if let addressesData = sender.addresses {
104
- for addressData in addressesData {
105
- let address = addressData.withUnsafeBytes { (pointer: UnsafeRawBufferPointer) -> String? in
106
- guard let baseAddress = pointer.baseAddress else { return nil }
107
- let data = baseAddress.assumingMemoryBound(to: sockaddr.self)
108
-
109
- var hostname = [CChar](repeating: 0, count: Int(NI_MAXHOST))
110
- if getnameinfo(data, socklen_t(addressData.count), &hostname, socklen_t(hostname.count), nil, 0, NI_NUMERICHOST) == 0 {
111
- return String(cString: hostname)
112
- }
113
- return nil
114
- }
115
-
116
- if let addr = address {
117
- addresses.append(addr)
118
- }
119
- }
120
- }
142
+ print("βœ… iOS: Service RESOLVED")
143
+ print(" Name: \(sender.name)")
144
+ print(" Host: \(sender.hostName ?? "nil")")
145
+ print(" Port: \(sender.port)")
121
146
 
147
+ // βœ… NO incluir addresses - Solo usar txtRecord
122
148
  var serviceData: [String: Any] = [
123
149
  "serviceName": sender.name,
124
150
  "serviceType": sender.type,
125
151
  "domain": sender.domain,
126
152
  "hostName": sender.hostName ?? "",
127
- "port": sender.port,
128
- "addresses": addresses
153
+ "port": sender.port
129
154
  ]
130
155
 
131
- // Agregar TXT record
156
+ // βœ… SOLO agregar TXT record (aquΓ­ estΓ‘ tu IP correcta)
132
157
  if let txtData = sender.txtRecordData() {
133
158
  let txtRecord = NetService.dictionary(fromTXTRecord: txtData)
134
159
  var txtRecordDict: [String: String] = [:]
135
160
  for (key, value) in txtRecord {
136
161
  if let strValue = String(data: value, encoding: .utf8) {
137
162
  txtRecordDict[key] = strValue
163
+ print(" TXT Record retrieved: \(key) = \(strValue)")
138
164
  }
139
165
  }
140
166
  serviceData["txtRecord"] = txtRecordDict
@@ -144,7 +170,9 @@ extension NetworkDiscovery: NetServiceBrowserDelegate {
144
170
  }
145
171
 
146
172
  public func netService(_ sender: NetService, didNotResolve errorDict: [String : NSNumber]) {
147
- print("Service did not resolve: \(errorDict)")
173
+ print("❌ iOS: Service did NOT resolve")
174
+ print(" Service: \(sender.name)")
175
+ print(" Error dict: \(errorDict)")
148
176
  }
149
177
  }
150
178
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cappitolian/network-discovery",
3
- "version": "0.0.8",
3
+ "version": "0.0.9",
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",