@cappitolian/network-discovery 0.0.10 → 0.0.11

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/README.md CHANGED
@@ -1,365 +1,365 @@
1
- # @cappitolian/network-discovery
2
-
3
- A Capacitor plugin for network service discovery using mDNS/Bonjour. Allows automatic server-client connection without manual IP configuration.
4
-
5
- ---
6
-
7
- ## Features
8
-
9
- - **Advertise services** on the local network (server mode)
10
- - **Discover services** automatically (client mode)
11
- - Pass custom data via TXT records (like IP addresses, ports, etc.)
12
- - Real-time service found/lost events
13
- - Works on **iOS** (Bonjour) and **Android** (NSD)
14
- - Tested with **Capacitor 7** and **Ionic 8**
15
-
16
- ---
17
-
18
- ## Installation
19
- ```bash
20
- npm install @cappitolian/network-discovery
21
- npx cap sync
22
- ```
23
-
24
- ---
25
-
26
- ## Usage
27
-
28
- ### Import
29
- ```typescript
30
- import { NetworkDiscovery } from '@cappitolian/network-discovery';
31
- ```
32
-
33
- ### Server Mode - Advertise Your Service
34
- ```typescript
35
- // Start advertising your server
36
- await NetworkDiscovery.startAdvertising({
37
- serviceName: 'MyAppServer',
38
- serviceType: '_http._tcp', // or '_myapp._tcp' for custom service
39
- port: 8080,
40
- txtRecord: {
41
- ip: '192.168.1.100', // Your server IP
42
- version: '1.0.0' // Any custom data
43
- }
44
- });
45
-
46
- console.log('Server is now discoverable on the network');
47
-
48
- // Stop advertising when needed
49
- await NetworkDiscovery.stopAdvertising();
50
- ```
51
-
52
- ### Client Mode - Discover Services
53
- ```typescript
54
- // Listen for discovered services
55
- NetworkDiscovery.addListener('serviceFound', (service) => {
56
- console.log('Service discovered:', service);
57
- console.log('Server IP:', service.txtRecord?.ip);
58
- console.log('Server Port:', service.port);
59
- console.log('Server Addresses:', service.addresses);
60
-
61
- // Connect to your server
62
- connectToServer(service.addresses[0], service.port);
63
- });
64
-
65
- // Listen for lost services
66
- NetworkDiscovery.addListener('serviceLost', (service) => {
67
- console.log('Service lost:', service.serviceName);
68
- });
69
-
70
- // Start discovery
71
- await NetworkDiscovery.startDiscovery({
72
- serviceType: '_http._tcp', // Must match the server's serviceType
73
- domain: 'local.' // Optional, defaults to 'local.'
74
- });
75
-
76
- // Stop discovery when needed
77
- await NetworkDiscovery.stopDiscovery();
78
-
79
- // Clean up listeners
80
- await NetworkDiscovery.removeAllListeners();
81
- ```
82
-
83
- ### Complete Example - Auto-Connect Flow
84
-
85
- **Server Side:**
86
- ```typescript
87
- import { NetworkDiscovery } from '@cappitolian/network-discovery';
88
- import { Network } from '@capacitor/network';
89
-
90
- async startServer() {
91
- // Get device IP
92
- const ip = await this.getLocalIP();
93
-
94
- // Advertise server
95
- await NetworkDiscovery.startAdvertising({
96
- serviceName: 'MyAppServer',
97
- serviceType: '_myapp._tcp',
98
- port: 8080,
99
- txtRecord: {
100
- ip: ip,
101
- serverName: 'Production Server'
102
- }
103
- });
104
-
105
- console.log('Server advertising on network');
106
- }
107
-
108
- async getLocalIP(): Promise<string> {
109
- const status = await Network.getStatus();
110
- // Your IP extraction logic here
111
- return '192.168.1.100';
112
- }
113
- ```
114
-
115
- **Client Side:**
116
- ```typescript
117
- import { NetworkDiscovery } from '@cappitolian/network-discovery';
118
-
119
- async findAndConnectToServer() {
120
- return new Promise((resolve, reject) => {
121
-
122
- // Listen for server
123
- NetworkDiscovery.addListener('serviceFound', async (service) => {
124
- if (service.serviceName === 'MyAppServer') {
125
- console.log('Server found!');
126
-
127
- const serverIP = service.txtRecord?.ip || service.addresses[0];
128
- const serverPort = service.port;
129
-
130
- // Stop discovery
131
- await NetworkDiscovery.stopDiscovery();
132
- await NetworkDiscovery.removeAllListeners();
133
-
134
- // Connect to server
135
- resolve({ ip: serverIP, port: serverPort });
136
- }
137
- });
138
-
139
- // Start discovery
140
- NetworkDiscovery.startDiscovery({
141
- serviceType: '_myapp._tcp'
142
- });
143
-
144
- // Timeout after 10 seconds
145
- setTimeout(() => {
146
- reject(new Error('Server not found'));
147
- }, 10000);
148
- });
149
- }
150
-
151
- // Usage
152
- async onLoginClick() {
153
- try {
154
- const server = await this.findAndConnectToServer();
155
- console.log(`Connecting to ${server.ip}:${server.port}`);
156
- // Your connection logic here
157
- } catch (error) {
158
- console.error('Could not find server:', error);
159
- }
160
- }
161
- ```
162
-
163
- ---
164
-
165
- ## API Reference
166
-
167
- ### Methods
168
-
169
- #### `startAdvertising(options: AdvertisingOptions)`
170
-
171
- Publishes a service on the local network.
172
-
173
- **Parameters:**
174
- - `serviceName` (string): Name of your service (e.g., "MyAppServer")
175
- - `serviceType` (string): Service type (e.g., "_http._tcp", "_myapp._tcp")
176
- - `port` (number): Port number your server is listening on
177
- - `txtRecord` (object, optional): Key-value pairs to broadcast (e.g., IP, version)
178
-
179
- **Returns:** `Promise<{ success: boolean }>`
180
-
181
- ---
182
-
183
- #### `stopAdvertising()`
184
-
185
- Stops advertising the service.
186
-
187
- **Returns:** `Promise<{ success: boolean }>`
188
-
189
- ---
190
-
191
- #### `startDiscovery(options: DiscoveryOptions)`
192
-
193
- Starts searching for services on the local network.
194
-
195
- **Parameters:**
196
- - `serviceType` (string): Type of service to search for (must match server's type)
197
- - `domain` (string, optional): Domain to search in (default: "local.")
198
-
199
- **Returns:** `Promise<void>`
200
-
201
- ---
202
-
203
- #### `stopDiscovery()`
204
-
205
- Stops the service discovery.
206
-
207
- **Returns:** `Promise<{ success: boolean }>`
208
-
209
- ---
210
-
211
- ### Events
212
-
213
- #### `serviceFound`
214
-
215
- Fired when a service is discovered.
216
-
217
- **Payload:**
218
- ```typescript
219
- {
220
- serviceName: string;
221
- serviceType: string;
222
- domain: string;
223
- hostName: string;
224
- port: number;
225
- addresses: string[]; // Array of IP addresses
226
- txtRecord?: { // Custom data from server
227
- [key: string]: string;
228
- };
229
- }
230
- ```
231
-
232
- ---
233
-
234
- #### `serviceLost`
235
-
236
- Fired when a previously discovered service is no longer available.
237
-
238
- **Payload:**
239
- ```typescript
240
- {
241
- serviceName: string;
242
- serviceType: string;
243
- }
244
- ```
245
-
246
- ---
247
-
248
- ## Service Type Format
249
-
250
- Service types must follow the format: `_name._protocol`
251
-
252
- **Common examples:**
253
- - `_http._tcp` - HTTP service
254
- - `_https._tcp` - HTTPS service
255
- - `_myapp._tcp` - Custom app service
256
- - `_ssh._tcp` - SSH service
257
-
258
- **Recommended for apps:** Use a custom service type like `_myapp._tcp` to avoid conflicts with other services.
259
-
260
- ---
261
-
262
- ## Permissions
263
-
264
- ### Android
265
-
266
- Add to `AndroidManifest.xml`:
267
- ```xml
268
- <uses-permission android:name="android.permission.INTERNET" />
269
- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
270
- <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
271
- <uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
272
- ```
273
-
274
- ### iOS
275
-
276
- No additional permissions required. Bonjour is enabled by default.
277
-
278
- Optionally, add to `Info.plist` to declare your service:
279
- ```xml
280
- <key>NSBonjourServices</key>
281
- <array>
282
- <string>_myapp._tcp</string>
283
- </array>
284
- ```
285
-
286
- ---
287
-
288
- ## Platforms
289
-
290
- - **iOS** (Bonjour/NetService)
291
- - **Android** (Network Service Discovery)
292
- - **Web** (Not implemented - throws unimplemented error)
293
-
294
- ---
295
-
296
- ## Requirements
297
-
298
- - [Capacitor 7+](https://capacitorjs.com/)
299
- - [Ionic 8+](https://ionicframework.com/) (optional, but tested)
300
- - iOS 12.0+
301
- - Android API 16+ (Android 4.1+)
302
-
303
- ---
304
-
305
- ## Troubleshooting
306
-
307
- ### Services not being discovered
308
-
309
- 1. **Check both devices are on the same WiFi network**
310
- 2. **Verify service types match** exactly between server and client
311
- 3. **Check firewall settings** - some networks block mDNS
312
- 4. **Android:** Ensure multicast is enabled on your network
313
- 5. **iOS:** Make sure device is not in Low Power Mode
314
-
315
- ### Server not advertising
316
-
317
- 1. **Verify port is not in use** by another service
318
- 2. **Check network permissions** are granted
319
- 3. **Restart the app** after installing the plugin
320
-
321
- ### General debugging
322
-
323
- Enable verbose logging:
324
- ```typescript
325
- // Check plugin is loaded
326
- console.log('Plugin available:', NetworkDiscovery);
327
-
328
- // Log all events
329
- NetworkDiscovery.addListener('serviceFound', (s) => console.log('Found:', s));
330
- NetworkDiscovery.addListener('serviceLost', (s) => console.log('Lost:', s));
331
- ```
332
-
333
- ---
334
-
335
- ## Use Cases
336
-
337
- - **Auto-connect apps** - Client automatically finds and connects to server
338
- - **Local multiplayer games** - Discover game hosts on LAN
339
- - **IoT device discovery** - Find smart devices without configuration
340
- - **File sharing apps** - Discover peers for file transfer
341
- - **Remote control apps** - Find controllable devices automatically
342
-
343
- ---
344
-
345
- ## License
346
-
347
- MIT
348
-
349
- ---
350
-
351
- ## Support
352
-
353
- If you encounter any issues or have feature requests, please open an issue on the [GitHub repository](https://github.com/alessandrycruz1987/network-discovery).
354
-
355
- ---
356
-
357
- ## Contributing
358
-
359
- Contributions are welcome! Please feel free to submit a Pull Request.
360
-
361
- ---
362
-
363
- ## Credits
364
-
1
+ # @cappitolian/network-discovery
2
+
3
+ A Capacitor plugin for network service discovery using mDNS/Bonjour. Allows automatic server-client connection without manual IP configuration.
4
+
5
+ ---
6
+
7
+ ## Features
8
+
9
+ - **Advertise services** on the local network (server mode)
10
+ - **Discover services** automatically (client mode)
11
+ - Pass custom data via TXT records (like IP addresses, ports, etc.)
12
+ - Real-time service found/lost events
13
+ - Works on **iOS** (Bonjour) and **Android** (NSD)
14
+ - Tested with **Capacitor 7** and **Ionic 8**
15
+
16
+ ---
17
+
18
+ ## Installation
19
+ ```bash
20
+ npm install @cappitolian/network-discovery
21
+ npx cap sync
22
+ ```
23
+
24
+ ---
25
+
26
+ ## Usage
27
+
28
+ ### Import
29
+ ```typescript
30
+ import { NetworkDiscovery } from '@cappitolian/network-discovery';
31
+ ```
32
+
33
+ ### Server Mode - Advertise Your Service
34
+ ```typescript
35
+ // Start advertising your server
36
+ await NetworkDiscovery.startAdvertising({
37
+ serviceName: 'MyAppServer',
38
+ serviceType: '_http._tcp', // or '_myapp._tcp' for custom service
39
+ port: 8080,
40
+ txtRecord: {
41
+ ip: '192.168.1.100', // Your server IP
42
+ version: '1.0.0' // Any custom data
43
+ }
44
+ });
45
+
46
+ console.log('Server is now discoverable on the network');
47
+
48
+ // Stop advertising when needed
49
+ await NetworkDiscovery.stopAdvertising();
50
+ ```
51
+
52
+ ### Client Mode - Discover Services
53
+ ```typescript
54
+ // Listen for discovered services
55
+ NetworkDiscovery.addListener('serviceFound', (service) => {
56
+ console.log('Service discovered:', service);
57
+ console.log('Server IP:', service.txtRecord?.ip);
58
+ console.log('Server Port:', service.port);
59
+ console.log('Server Addresses:', service.addresses);
60
+
61
+ // Connect to your server
62
+ connectToServer(service.addresses[0], service.port);
63
+ });
64
+
65
+ // Listen for lost services
66
+ NetworkDiscovery.addListener('serviceLost', (service) => {
67
+ console.log('Service lost:', service.serviceName);
68
+ });
69
+
70
+ // Start discovery
71
+ await NetworkDiscovery.startDiscovery({
72
+ serviceType: '_http._tcp', // Must match the server's serviceType
73
+ domain: 'local.' // Optional, defaults to 'local.'
74
+ });
75
+
76
+ // Stop discovery when needed
77
+ await NetworkDiscovery.stopDiscovery();
78
+
79
+ // Clean up listeners
80
+ await NetworkDiscovery.removeAllListeners();
81
+ ```
82
+
83
+ ### Complete Example - Auto-Connect Flow
84
+
85
+ **Server Side:**
86
+ ```typescript
87
+ import { NetworkDiscovery } from '@cappitolian/network-discovery';
88
+ import { Network } from '@capacitor/network';
89
+
90
+ async startServer() {
91
+ // Get device IP
92
+ const ip = await this.getLocalIP();
93
+
94
+ // Advertise server
95
+ await NetworkDiscovery.startAdvertising({
96
+ serviceName: 'MyAppServer',
97
+ serviceType: '_myapp._tcp',
98
+ port: 8080,
99
+ txtRecord: {
100
+ ip: ip,
101
+ serverName: 'Production Server'
102
+ }
103
+ });
104
+
105
+ console.log('Server advertising on network');
106
+ }
107
+
108
+ async getLocalIP(): Promise<string> {
109
+ const status = await Network.getStatus();
110
+ // Your IP extraction logic here
111
+ return '192.168.1.100';
112
+ }
113
+ ```
114
+
115
+ **Client Side:**
116
+ ```typescript
117
+ import { NetworkDiscovery } from '@cappitolian/network-discovery';
118
+
119
+ async findAndConnectToServer() {
120
+ return new Promise((resolve, reject) => {
121
+
122
+ // Listen for server
123
+ NetworkDiscovery.addListener('serviceFound', async (service) => {
124
+ if (service.serviceName === 'MyAppServer') {
125
+ console.log('Server found!');
126
+
127
+ const serverIP = service.txtRecord?.ip || service.addresses[0];
128
+ const serverPort = service.port;
129
+
130
+ // Stop discovery
131
+ await NetworkDiscovery.stopDiscovery();
132
+ await NetworkDiscovery.removeAllListeners();
133
+
134
+ // Connect to server
135
+ resolve({ ip: serverIP, port: serverPort });
136
+ }
137
+ });
138
+
139
+ // Start discovery
140
+ NetworkDiscovery.startDiscovery({
141
+ serviceType: '_myapp._tcp'
142
+ });
143
+
144
+ // Timeout after 10 seconds
145
+ setTimeout(() => {
146
+ reject(new Error('Server not found'));
147
+ }, 10000);
148
+ });
149
+ }
150
+
151
+ // Usage
152
+ async onLoginClick() {
153
+ try {
154
+ const server = await this.findAndConnectToServer();
155
+ console.log(`Connecting to ${server.ip}:${server.port}`);
156
+ // Your connection logic here
157
+ } catch (error) {
158
+ console.error('Could not find server:', error);
159
+ }
160
+ }
161
+ ```
162
+
163
+ ---
164
+
165
+ ## API Reference
166
+
167
+ ### Methods
168
+
169
+ #### `startAdvertising(options: AdvertisingOptions)`
170
+
171
+ Publishes a service on the local network.
172
+
173
+ **Parameters:**
174
+ - `serviceName` (string): Name of your service (e.g., "MyAppServer")
175
+ - `serviceType` (string): Service type (e.g., "_http._tcp", "_myapp._tcp")
176
+ - `port` (number): Port number your server is listening on
177
+ - `txtRecord` (object, optional): Key-value pairs to broadcast (e.g., IP, version)
178
+
179
+ **Returns:** `Promise<{ success: boolean }>`
180
+
181
+ ---
182
+
183
+ #### `stopAdvertising()`
184
+
185
+ Stops advertising the service.
186
+
187
+ **Returns:** `Promise<{ success: boolean }>`
188
+
189
+ ---
190
+
191
+ #### `startDiscovery(options: DiscoveryOptions)`
192
+
193
+ Starts searching for services on the local network.
194
+
195
+ **Parameters:**
196
+ - `serviceType` (string): Type of service to search for (must match server's type)
197
+ - `domain` (string, optional): Domain to search in (default: "local.")
198
+
199
+ **Returns:** `Promise<void>`
200
+
201
+ ---
202
+
203
+ #### `stopDiscovery()`
204
+
205
+ Stops the service discovery.
206
+
207
+ **Returns:** `Promise<{ success: boolean }>`
208
+
209
+ ---
210
+
211
+ ### Events
212
+
213
+ #### `serviceFound`
214
+
215
+ Fired when a service is discovered.
216
+
217
+ **Payload:**
218
+ ```typescript
219
+ {
220
+ serviceName: string;
221
+ serviceType: string;
222
+ domain: string;
223
+ hostName: string;
224
+ port: number;
225
+ addresses: string[]; // Array of IP addresses
226
+ txtRecord?: { // Custom data from server
227
+ [key: string]: string;
228
+ };
229
+ }
230
+ ```
231
+
232
+ ---
233
+
234
+ #### `serviceLost`
235
+
236
+ Fired when a previously discovered service is no longer available.
237
+
238
+ **Payload:**
239
+ ```typescript
240
+ {
241
+ serviceName: string;
242
+ serviceType: string;
243
+ }
244
+ ```
245
+
246
+ ---
247
+
248
+ ## Service Type Format
249
+
250
+ Service types must follow the format: `_name._protocol`
251
+
252
+ **Common examples:**
253
+ - `_http._tcp` - HTTP service
254
+ - `_https._tcp` - HTTPS service
255
+ - `_myapp._tcp` - Custom app service
256
+ - `_ssh._tcp` - SSH service
257
+
258
+ **Recommended for apps:** Use a custom service type like `_myapp._tcp` to avoid conflicts with other services.
259
+
260
+ ---
261
+
262
+ ## Permissions
263
+
264
+ ### Android
265
+
266
+ Add to `AndroidManifest.xml`:
267
+ ```xml
268
+ <uses-permission android:name="android.permission.INTERNET" />
269
+ <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
270
+ <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
271
+ <uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
272
+ ```
273
+
274
+ ### iOS
275
+
276
+ No additional permissions required. Bonjour is enabled by default.
277
+
278
+ Optionally, add to `Info.plist` to declare your service:
279
+ ```xml
280
+ <key>NSBonjourServices</key>
281
+ <array>
282
+ <string>_myapp._tcp</string>
283
+ </array>
284
+ ```
285
+
286
+ ---
287
+
288
+ ## Platforms
289
+
290
+ - **iOS** (Bonjour/NetService)
291
+ - **Android** (Network Service Discovery)
292
+ - **Web** (Not implemented - throws unimplemented error)
293
+
294
+ ---
295
+
296
+ ## Requirements
297
+
298
+ - [Capacitor 7+](https://capacitorjs.com/)
299
+ - [Ionic 8+](https://ionicframework.com/) (optional, but tested)
300
+ - iOS 12.0+
301
+ - Android API 16+ (Android 4.1+)
302
+
303
+ ---
304
+
305
+ ## Troubleshooting
306
+
307
+ ### Services not being discovered
308
+
309
+ 1. **Check both devices are on the same WiFi network**
310
+ 2. **Verify service types match** exactly between server and client
311
+ 3. **Check firewall settings** - some networks block mDNS
312
+ 4. **Android:** Ensure multicast is enabled on your network
313
+ 5. **iOS:** Make sure device is not in Low Power Mode
314
+
315
+ ### Server not advertising
316
+
317
+ 1. **Verify port is not in use** by another service
318
+ 2. **Check network permissions** are granted
319
+ 3. **Restart the app** after installing the plugin
320
+
321
+ ### General debugging
322
+
323
+ Enable verbose logging:
324
+ ```typescript
325
+ // Check plugin is loaded
326
+ console.log('Plugin available:', NetworkDiscovery);
327
+
328
+ // Log all events
329
+ NetworkDiscovery.addListener('serviceFound', (s) => console.log('Found:', s));
330
+ NetworkDiscovery.addListener('serviceLost', (s) => console.log('Lost:', s));
331
+ ```
332
+
333
+ ---
334
+
335
+ ## Use Cases
336
+
337
+ - **Auto-connect apps** - Client automatically finds and connects to server
338
+ - **Local multiplayer games** - Discover game hosts on LAN
339
+ - **IoT device discovery** - Find smart devices without configuration
340
+ - **File sharing apps** - Discover peers for file transfer
341
+ - **Remote control apps** - Find controllable devices automatically
342
+
343
+ ---
344
+
345
+ ## License
346
+
347
+ MIT
348
+
349
+ ---
350
+
351
+ ## Support
352
+
353
+ If you encounter any issues or have feature requests, please open an issue on the [GitHub repository](https://github.com/alessandrycruz1987/network-discovery).
354
+
355
+ ---
356
+
357
+ ## Contributing
358
+
359
+ Contributions are welcome! Please feel free to submit a Pull Request.
360
+
361
+ ---
362
+
363
+ ## Credits
364
+
365
365
  Created for use with Capacitor 7 and Ionic 8 applications requiring automatic network service discovery.
@@ -132,20 +132,12 @@ public class NetworkDiscovery {
132
132
 
133
133
  @Override
134
134
  public void onServiceResolved(NsdServiceInfo serviceInfo) {
135
- Log.d(TAG, "Android: Service RESOLVED");
136
- Log.d(TAG, " Name: " + serviceInfo.getServiceName());
137
- Log.d(TAG, " Host: " + (serviceInfo.getHost() != null ? serviceInfo.getHost().getHostAddress() : "null"));
138
- Log.d(TAG, " Port: " + serviceInfo.getPort());
139
-
135
+ Log.d(TAG, "Service resolved: " + serviceInfo);
136
+
140
137
  JSObject serviceData = buildServiceObject(serviceInfo);
141
-
142
- Log.d(TAG, "Android: Calling callback.onServiceFound with data: " + serviceData.toString());
143
-
144
138
  callback.onServiceFound(serviceData);
145
-
146
- Log.d(TAG, "Android: callback.onServiceFound COMPLETED");
147
- }
148
- });
139
+ }
140
+ });
149
141
  }
150
142
 
151
143
  @Override
@@ -1,129 +1,116 @@
1
- package com.cappitolian.plugins.networkdiscovery;
2
-
3
- import com.getcapacitor.JSObject;
4
- import com.getcapacitor.Plugin;
5
- import com.getcapacitor.PluginCall;
6
- import com.getcapacitor.PluginMethod;
7
- import com.getcapacitor.annotation.CapacitorPlugin;
8
-
9
- @CapacitorPlugin(name = "NetworkDiscovery")
10
- public class NetworkDiscoveryPlugin extends Plugin {
11
-
12
- private NetworkDiscovery implementation;
13
-
14
- @Override
15
- public void load() {
16
- implementation = new NetworkDiscovery(this, getContext());
17
- }
18
-
19
- @PluginMethod
20
- public void startAdvertising(PluginCall call) {
21
- String serviceName = call.getString("serviceName");
22
- String serviceType = call.getString("serviceType");
23
- Integer port = call.getInt("port");
24
- JSObject txtRecord = call.getObject("txtRecord");
25
-
26
- if (serviceName == null || serviceType == null || port == null) {
27
- call.reject("Missing required parameters");
28
- return;
29
- }
30
-
31
- implementation.startAdvertising(
32
- serviceName,
33
- serviceType,
34
- port,
35
- txtRecord,
36
- new NetworkDiscovery.AdvertisingCallback() {
37
- @Override
38
- public void onSuccess() {
39
- JSObject ret = new JSObject();
40
- ret.put("success", true);
41
- call.resolve(ret);
42
- }
43
-
44
- @Override
45
- public void onError(String error) {
46
- call.reject(error);
47
- }
48
- }
49
- );
50
- }
51
-
52
- @PluginMethod
53
- public void stopAdvertising(PluginCall call) {
54
- implementation.stopAdvertising(new NetworkDiscovery.StopCallback() {
55
- @Override
56
- public void onSuccess() {
57
- JSObject ret = new JSObject();
58
- ret.put("success", true);
59
- call.resolve(ret);
60
- }
61
-
62
- @Override
63
- public void onError(String error) {
64
- call.reject(error);
65
- }
66
- });
67
- }
68
-
69
- @PluginMethod
70
- public void startDiscovery(PluginCall call) {
71
- String serviceType = call.getString("serviceType");
72
-
73
- if (serviceType == null) {
74
- call.reject("Missing serviceType parameter");
75
- return;
76
- }
77
-
78
- implementation.startDiscovery(serviceType, new NetworkDiscovery.DiscoveryCallback() {
79
- @Override
80
- public void onDiscoveryStarted() {
81
- call.resolve();
82
- }
83
-
84
- @Override
85
- public void onServiceFound(JSObject service) {
86
- // ✅ IMPORTANTE: Usar getBridge().executeOnMainThread
87
- getBridge().executeOnMainThread(new Runnable() {
88
- @Override
89
- public void run() {
90
- notifyListeners("serviceFound", service);
91
- }
92
- });
93
- }
94
-
95
- @Override
96
- public void onServiceLost(JSObject service) {
97
- // ✅ IMPORTANTE: Usar getBridge().executeOnMainThread
98
- getBridge().executeOnMainThread(new Runnable() {
99
- @Override
100
- public void run() {
101
- notifyListeners("serviceLost", service);
102
- }
103
- });
104
- }
105
-
106
- @Override
107
- public void onError(String error) {
108
- call.reject(error);
109
- }
110
- });
111
- }
112
-
113
- @PluginMethod
114
- public void stopDiscovery(PluginCall call) {
115
- implementation.stopDiscovery(new NetworkDiscovery.StopCallback() {
116
- @Override
117
- public void onSuccess() {
118
- JSObject ret = new JSObject();
119
- ret.put("success", true);
120
- call.resolve(ret);
121
- }
122
-
123
- @Override
124
- public void onError(String error) {
125
- call.reject(error);
126
- }
127
- });
128
- }
1
+ package com.cappitolian.plugins.networkdiscovery;
2
+
3
+ import com.getcapacitor.JSObject;
4
+ import com.getcapacitor.Plugin;
5
+ import com.getcapacitor.PluginCall;
6
+ import com.getcapacitor.PluginMethod;
7
+ import com.getcapacitor.annotation.CapacitorPlugin;
8
+
9
+ @CapacitorPlugin(name = "NetworkDiscovery")
10
+ public class NetworkDiscoveryPlugin extends Plugin {
11
+ private NetworkDiscovery implementation;
12
+
13
+ @Override
14
+ public void load() {
15
+ implementation = new NetworkDiscovery(this, getContext());
16
+ }
17
+
18
+ @PluginMethod
19
+ public void startAdvertising(PluginCall call) {
20
+ String serviceName = call.getString("serviceName");
21
+ String serviceType = call.getString("serviceType");
22
+ Integer port = call.getInt("port");
23
+ JSObject txtRecord = call.getObject("txtRecord");
24
+
25
+ if (serviceName == null || serviceType == null || port == null) {
26
+ call.reject("Missing required parameters");
27
+ return;
28
+ }
29
+
30
+ implementation.startAdvertising(
31
+ serviceName,
32
+ serviceType,
33
+ port,
34
+ txtRecord,
35
+ new NetworkDiscovery.AdvertisingCallback() {
36
+ @Override
37
+ public void onSuccess() {
38
+ JSObject ret = new JSObject();
39
+ ret.put("success", true);
40
+ call.resolve(ret);
41
+ }
42
+
43
+ @Override
44
+ public void onError(String error) {
45
+ call.reject(error);
46
+ }
47
+ }
48
+ );
49
+ }
50
+
51
+ @PluginMethod
52
+ public void stopAdvertising(PluginCall call) {
53
+ implementation.stopAdvertising(new NetworkDiscovery.StopCallback() {
54
+ @Override
55
+ public void onSuccess() {
56
+ JSObject ret = new JSObject();
57
+ ret.put("success", true);
58
+ call.resolve(ret);
59
+ }
60
+
61
+ @Override
62
+ public void onError(String error) {
63
+ call.reject(error);
64
+ }
65
+ });
66
+ }
67
+
68
+ @PluginMethod
69
+ public void startDiscovery(PluginCall call) {
70
+ String serviceType = call.getString("serviceType");
71
+
72
+ if (serviceType == null) {
73
+ call.reject("Missing serviceType parameter");
74
+ return;
75
+ }
76
+
77
+ implementation.startDiscovery(serviceType, new NetworkDiscovery.DiscoveryCallback() {
78
+ @Override
79
+ public void onDiscoveryStarted() {
80
+ call.resolve();
81
+ }
82
+
83
+ @Override
84
+ public void onServiceFound(JSObject service) {
85
+ notifyListeners("serviceFound", service);
86
+ }
87
+
88
+ @Override
89
+ public void onServiceLost(JSObject service) {
90
+ notifyListeners("serviceLost", service);
91
+ }
92
+
93
+ @Override
94
+ public void onError(String error) {
95
+ call.reject(error);
96
+ }
97
+ });
98
+ }
99
+
100
+ @PluginMethod
101
+ public void stopDiscovery(PluginCall call) {
102
+ implementation.stopDiscovery(new NetworkDiscovery.StopCallback() {
103
+ @Override
104
+ public void onSuccess() {
105
+ JSObject ret = new JSObject();
106
+ ret.put("success", true);
107
+ call.resolve(ret);
108
+ }
109
+
110
+ @Override
111
+ public void onError(String error) {
112
+ call.reject(error);
113
+ }
114
+ });
115
+ }
129
116
  }
package/package.json CHANGED
@@ -1,86 +1,86 @@
1
- {
2
- "name": "@cappitolian/network-discovery",
3
- "version": "0.0.10",
4
- "description": "A Capacitor plugin for network service discovery using mDNS/Bonjour. Allows automatic server-client connection without manual IP configuration.",
5
- "main": "dist/plugin.cjs.js",
6
- "module": "dist/esm/index.js",
7
- "types": "dist/esm/index.d.ts",
8
- "unpkg": "dist/plugin.js",
9
- "files": [
10
- "android/src/main/",
11
- "android/build.gradle",
12
- "dist/",
13
- "ios/Sources",
14
- "ios/Tests",
15
- "Package.swift",
16
- "CappitolianNetworkDiscovery.podspec"
17
- ],
18
- "author": "Cappitolian",
19
- "license": "MIT",
20
- "repository": {
21
- "type": "git",
22
- "url": "git+https://github.com/alessandrycruz1987/network-discovery.git"
23
- },
24
- "bugs": {
25
- "url": "https://github.com/alessandrycruz1987/network-discovery.git/issues"
26
- },
27
- "keywords": [
28
- "capacitor",
29
- "plugin",
30
- "native",
31
- "network",
32
- "discovery",
33
- "mdns",
34
- "bonjour",
35
- "nsd",
36
- "zeroconf"
37
- ],
38
- "scripts": {
39
- "verify": "npm run verify:ios && npm run verify:android && npm run verify:web",
40
- "verify:ios": "xcodebuild -scheme CappitolianNetworkDiscovery -destination generic/platform=iOS",
41
- "verify:android": "cd android && ./gradlew clean build test && cd ..",
42
- "verify:web": "npm run build",
43
- "lint": "npm run eslint && npm run prettier -- --check && npm run swiftlint -- lint",
44
- "fmt": "npm run eslint -- --fix && npm run prettier -- --write && npm run swiftlint -- --fix --format",
45
- "eslint": "eslint . --ext ts",
46
- "prettier": "prettier \"**/*.{css,html,ts,js,java}\" --plugin=prettier-plugin-java",
47
- "swiftlint": "node-swiftlint",
48
- "docgen": "docgen --api NetworkDiscoveryPlugin --output-readme README.md --output-json dist/docs.json",
49
- "build": "npm run clean && npm run docgen && tsc && rollup -c rollup.config.mjs",
50
- "clean": "rimraf ./dist",
51
- "watch": "tsc --watch",
52
- "prepublishOnly": "npm run build"
53
- },
54
- "devDependencies": {
55
- "@capacitor/android": "^7.0.0",
56
- "@capacitor/core": "^7.0.0",
57
- "@capacitor/docgen": "^0.3.1",
58
- "@capacitor/ios": "^7.0.0",
59
- "@ionic/eslint-config": "^0.4.0",
60
- "@ionic/prettier-config": "^4.0.0",
61
- "@ionic/swiftlint-config": "^2.0.0",
62
- "eslint": "^8.57.1",
63
- "prettier": "^3.6.2",
64
- "prettier-plugin-java": "^2.7.7",
65
- "rimraf": "^6.1.0",
66
- "rollup": "^4.53.2",
67
- "swiftlint": "^2.0.0",
68
- "typescript": "^5.9.3"
69
- },
70
- "peerDependencies": {
71
- "@capacitor/core": "^7.0.0"
72
- },
73
- "prettier": "@ionic/prettier-config",
74
- "swiftlint": "@ionic/swiftlint-config",
75
- "eslintConfig": {
76
- "extends": "@ionic/eslint-config/recommended"
77
- },
78
- "capacitor": {
79
- "ios": {
80
- "src": "ios"
81
- },
82
- "android": {
83
- "src": "android"
84
- }
85
- }
1
+ {
2
+ "name": "@cappitolian/network-discovery",
3
+ "version": "0.0.11",
4
+ "description": "A Capacitor plugin for network service discovery using mDNS/Bonjour. Allows automatic server-client connection without manual IP configuration.",
5
+ "main": "dist/plugin.cjs.js",
6
+ "module": "dist/esm/index.js",
7
+ "types": "dist/esm/index.d.ts",
8
+ "unpkg": "dist/plugin.js",
9
+ "files": [
10
+ "android/src/main/",
11
+ "android/build.gradle",
12
+ "dist/",
13
+ "ios/Sources",
14
+ "ios/Tests",
15
+ "Package.swift",
16
+ "CappitolianNetworkDiscovery.podspec"
17
+ ],
18
+ "author": "Cappitolian",
19
+ "license": "MIT",
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "git+https://github.com/alessandrycruz1987/network-discovery.git"
23
+ },
24
+ "bugs": {
25
+ "url": "https://github.com/alessandrycruz1987/network-discovery.git/issues"
26
+ },
27
+ "keywords": [
28
+ "capacitor",
29
+ "plugin",
30
+ "native",
31
+ "network",
32
+ "discovery",
33
+ "mdns",
34
+ "bonjour",
35
+ "nsd",
36
+ "zeroconf"
37
+ ],
38
+ "scripts": {
39
+ "verify": "npm run verify:ios && npm run verify:android && npm run verify:web",
40
+ "verify:ios": "xcodebuild -scheme CappitolianNetworkDiscovery -destination generic/platform=iOS",
41
+ "verify:android": "cd android && ./gradlew clean build test && cd ..",
42
+ "verify:web": "npm run build",
43
+ "lint": "npm run eslint && npm run prettier -- --check && npm run swiftlint -- lint",
44
+ "fmt": "npm run eslint -- --fix && npm run prettier -- --write && npm run swiftlint -- --fix --format",
45
+ "eslint": "eslint . --ext ts",
46
+ "prettier": "prettier \"**/*.{css,html,ts,js,java}\" --plugin=prettier-plugin-java",
47
+ "swiftlint": "node-swiftlint",
48
+ "docgen": "docgen --api NetworkDiscoveryPlugin --output-readme README.md --output-json dist/docs.json",
49
+ "build": "npm run clean && npm run docgen && tsc && rollup -c rollup.config.mjs",
50
+ "clean": "rimraf ./dist",
51
+ "watch": "tsc --watch",
52
+ "prepublishOnly": "npm run build"
53
+ },
54
+ "devDependencies": {
55
+ "@capacitor/android": "^7.0.0",
56
+ "@capacitor/core": "^7.0.0",
57
+ "@capacitor/docgen": "^0.3.1",
58
+ "@capacitor/ios": "^7.0.0",
59
+ "@ionic/eslint-config": "^0.4.0",
60
+ "@ionic/prettier-config": "^4.0.0",
61
+ "@ionic/swiftlint-config": "^2.0.0",
62
+ "eslint": "^8.57.1",
63
+ "prettier": "^3.6.2",
64
+ "prettier-plugin-java": "^2.7.7",
65
+ "rimraf": "^6.1.0",
66
+ "rollup": "^4.53.2",
67
+ "swiftlint": "^2.0.0",
68
+ "typescript": "^5.9.3"
69
+ },
70
+ "peerDependencies": {
71
+ "@capacitor/core": "^7.0.0"
72
+ },
73
+ "prettier": "@ionic/prettier-config",
74
+ "swiftlint": "@ionic/swiftlint-config",
75
+ "eslintConfig": {
76
+ "extends": "@ionic/eslint-config/recommended"
77
+ },
78
+ "capacitor": {
79
+ "ios": {
80
+ "src": "ios"
81
+ },
82
+ "android": {
83
+ "src": "android"
84
+ }
85
+ }
86
86
  }