@capgo/capacitor-wifi 8.2.0 → 8.3.0
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.
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
package ee.forgr.plugin.capacitor_wifi;
|
|
2
2
|
|
|
3
|
+
import static android.app.Activity.RESULT_OK;
|
|
4
|
+
|
|
3
5
|
import android.Manifest;
|
|
4
6
|
import android.content.BroadcastReceiver;
|
|
5
7
|
import android.content.Context;
|
|
@@ -18,7 +20,9 @@ import android.net.wifi.WifiNetworkSpecifier;
|
|
|
18
20
|
import android.net.wifi.WifiNetworkSuggestion;
|
|
19
21
|
import android.os.Build;
|
|
20
22
|
import android.provider.Settings;
|
|
23
|
+
import androidx.activity.result.ActivityResult;
|
|
21
24
|
import androidx.annotation.NonNull;
|
|
25
|
+
import androidx.annotation.Nullable;
|
|
22
26
|
import androidx.annotation.RequiresApi;
|
|
23
27
|
import com.getcapacitor.JSArray;
|
|
24
28
|
import com.getcapacitor.JSObject;
|
|
@@ -26,6 +30,7 @@ import com.getcapacitor.PermissionState;
|
|
|
26
30
|
import com.getcapacitor.Plugin;
|
|
27
31
|
import com.getcapacitor.PluginCall;
|
|
28
32
|
import com.getcapacitor.PluginMethod;
|
|
33
|
+
import com.getcapacitor.annotation.ActivityCallback;
|
|
29
34
|
import com.getcapacitor.annotation.CapacitorPlugin;
|
|
30
35
|
import com.getcapacitor.annotation.Permission;
|
|
31
36
|
import com.getcapacitor.annotation.PermissionCallback;
|
|
@@ -44,7 +49,7 @@ import java.util.List;
|
|
|
44
49
|
)
|
|
45
50
|
public class CapacitorWifiPlugin extends Plugin {
|
|
46
51
|
|
|
47
|
-
private final String pluginVersion = "8.
|
|
52
|
+
private final String pluginVersion = "8.3.0";
|
|
48
53
|
|
|
49
54
|
private WifiManager wifiManager;
|
|
50
55
|
private ConnectivityManager connectivityManager;
|
|
@@ -119,13 +124,50 @@ public class CapacitorWifiPlugin extends Plugin {
|
|
|
119
124
|
suggestionsList.add(suggestionBuilder.build());
|
|
120
125
|
|
|
121
126
|
intent.putParcelableArrayListExtra(Settings.EXTRA_WIFI_NETWORK_LIST, suggestionsList);
|
|
122
|
-
|
|
123
|
-
call.resolve();
|
|
127
|
+
startActivityForResult(call, intent, "handleAddNetworkModernResult");
|
|
124
128
|
} catch (Exception e) {
|
|
125
129
|
call.reject("Failed to add network: " + e.getMessage(), e);
|
|
126
130
|
}
|
|
127
131
|
}
|
|
128
132
|
|
|
133
|
+
@ActivityCallback
|
|
134
|
+
private void handleAddNetworkModernResult(@Nullable PluginCall call, ActivityResult result) {
|
|
135
|
+
if (call == null) {
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (result.getResultCode() != RESULT_OK) {
|
|
140
|
+
call.reject("Adding network was canceled");
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
Intent data = result.getData();
|
|
145
|
+
if (data == null || !data.hasExtra(Settings.EXTRA_WIFI_NETWORK_RESULT_LIST)) {
|
|
146
|
+
call.reject("Failed to add network");
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
ArrayList<Integer> codes = data.getIntegerArrayListExtra(Settings.EXTRA_WIFI_NETWORK_RESULT_LIST);
|
|
151
|
+
Integer firstCode = codes != null && !codes.isEmpty() ? codes.get(0) : null;
|
|
152
|
+
|
|
153
|
+
if (firstCode == null) {
|
|
154
|
+
call.reject("Failed to add network");
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
switch (firstCode) {
|
|
159
|
+
case Settings.ADD_WIFI_RESULT_SUCCESS:
|
|
160
|
+
case Settings.ADD_WIFI_RESULT_ALREADY_EXISTS:
|
|
161
|
+
call.resolve();
|
|
162
|
+
return;
|
|
163
|
+
case Settings.ADD_WIFI_RESULT_ADD_OR_UPDATE_FAILED:
|
|
164
|
+
call.reject("Failed to add network");
|
|
165
|
+
return;
|
|
166
|
+
default:
|
|
167
|
+
call.reject("Failed to add network");
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
129
171
|
private void addNetworkLegacy(PluginCall call) {
|
|
130
172
|
if (getPermissionState("location") != PermissionState.GRANTED) {
|
|
131
173
|
requestPermissionForAlias("location", call, "addNetworkCallback");
|
|
@@ -426,16 +468,14 @@ public class CapacitorWifiPlugin extends Plugin {
|
|
|
426
468
|
@PluginMethod
|
|
427
469
|
public void getIpAddress(PluginCall call) {
|
|
428
470
|
try {
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(network);
|
|
434
|
-
if (capabilities != null && capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
|
|
435
|
-
ipAddress = getWifiIpAddress();
|
|
436
|
-
}
|
|
471
|
+
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
|
|
472
|
+
if (wifiInfo == null) {
|
|
473
|
+
call.reject("Failed to get WiFi info");
|
|
474
|
+
return;
|
|
437
475
|
}
|
|
438
476
|
|
|
477
|
+
String ipAddress = resolveWifiIpAddress(wifiInfo);
|
|
478
|
+
|
|
439
479
|
if (ipAddress != null && !ipAddress.isEmpty()) {
|
|
440
480
|
JSObject ret = new JSObject();
|
|
441
481
|
ret.put("ipAddress", ipAddress);
|
|
@@ -550,7 +590,7 @@ public class CapacitorWifiPlugin extends Plugin {
|
|
|
550
590
|
}
|
|
551
591
|
|
|
552
592
|
// Get IP Address
|
|
553
|
-
String ipAddress =
|
|
593
|
+
String ipAddress = resolveWifiIpAddress(wifiInfo);
|
|
554
594
|
if (ipAddress != null && !ipAddress.isEmpty()) {
|
|
555
595
|
ret.put("ip", ipAddress);
|
|
556
596
|
} else {
|
|
@@ -579,6 +619,21 @@ public class CapacitorWifiPlugin extends Plugin {
|
|
|
579
619
|
}
|
|
580
620
|
}
|
|
581
621
|
|
|
622
|
+
private String resolveWifiIpAddress(@NonNull WifiInfo wifiInfo) {
|
|
623
|
+
String ipAddress = formatIpv4Address(wifiInfo.getIpAddress());
|
|
624
|
+
if (ipAddress != null) {
|
|
625
|
+
return ipAddress;
|
|
626
|
+
}
|
|
627
|
+
return getWifiIpAddress();
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
private String formatIpv4Address(int ipAddress) {
|
|
631
|
+
if (ipAddress == 0) {
|
|
632
|
+
return null;
|
|
633
|
+
}
|
|
634
|
+
return (ipAddress & 0xff) + "." + ((ipAddress >> 8) & 0xff) + "." + ((ipAddress >> 16) & 0xff) + "." + ((ipAddress >> 24) & 0xff);
|
|
635
|
+
}
|
|
636
|
+
|
|
582
637
|
/**
|
|
583
638
|
* Calculate signal strength percentage (0-100) from RSSI
|
|
584
639
|
* RSSI typically ranges from -100 (weak) to -50 (strong)
|
|
@@ -4,8 +4,8 @@ import NetworkExtension
|
|
|
4
4
|
import CoreLocation
|
|
5
5
|
|
|
6
6
|
@objc(CapacitorWifiPlugin)
|
|
7
|
-
public class CapacitorWifiPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
8
|
-
private let pluginVersion: String = "8.
|
|
7
|
+
public class CapacitorWifiPlugin: CAPPlugin, CAPBridgedPlugin, CLLocationManagerDelegate {
|
|
8
|
+
private let pluginVersion: String = "8.3.0"
|
|
9
9
|
public let identifier = "CapacitorWifiPlugin"
|
|
10
10
|
public let jsName = "CapacitorWifi"
|
|
11
11
|
public let pluginMethods: [CAPPluginMethod] = [
|
|
@@ -26,9 +26,13 @@ public class CapacitorWifiPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
26
26
|
]
|
|
27
27
|
|
|
28
28
|
private var hotspotManager: NEHotspotConfigurationManager?
|
|
29
|
+
private var locationManager: CLLocationManager?
|
|
30
|
+
private var permissionCalls: [CAPPluginCall] = []
|
|
29
31
|
|
|
30
32
|
override public func load() {
|
|
31
33
|
hotspotManager = NEHotspotConfigurationManager.shared
|
|
34
|
+
locationManager = CLLocationManager()
|
|
35
|
+
locationManager?.delegate = self
|
|
32
36
|
}
|
|
33
37
|
|
|
34
38
|
@objc func addNetwork(_ call: CAPPluginCall) {
|
|
@@ -194,10 +198,20 @@ public class CapacitorWifiPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
194
198
|
}
|
|
195
199
|
|
|
196
200
|
@objc override public func requestPermissions(_ call: CAPPluginCall) {
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
+
DispatchQueue.main.async {
|
|
202
|
+
let status = self.getLocationPermissionStatus()
|
|
203
|
+
if status != "prompt" {
|
|
204
|
+
call.resolve(["location": status])
|
|
205
|
+
return
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
self.permissionCalls.append(call)
|
|
209
|
+
if self.permissionCalls.count > 1 {
|
|
210
|
+
return
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
self.locationManager?.requestWhenInUseAuthorization()
|
|
214
|
+
}
|
|
201
215
|
}
|
|
202
216
|
|
|
203
217
|
@objc func isNetworkSaved(_ call: CAPPluginCall) {
|
|
@@ -276,4 +290,36 @@ public class CapacitorWifiPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
276
290
|
return "prompt"
|
|
277
291
|
}
|
|
278
292
|
}
|
|
293
|
+
|
|
294
|
+
public func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
|
|
295
|
+
resolvePermissionCallIfNeeded()
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
public func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
|
|
299
|
+
resolvePermissionCallIfNeeded()
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
private func resolvePermissionCallIfNeeded() {
|
|
303
|
+
if !Thread.isMainThread {
|
|
304
|
+
DispatchQueue.main.async {
|
|
305
|
+
self.resolvePermissionCallIfNeeded()
|
|
306
|
+
}
|
|
307
|
+
return
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
if permissionCalls.isEmpty {
|
|
311
|
+
return
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
let status = getLocationPermissionStatus()
|
|
315
|
+
if status == "prompt" {
|
|
316
|
+
return
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
let pendingCalls = permissionCalls
|
|
320
|
+
permissionCalls.removeAll()
|
|
321
|
+
for pendingCall in pendingCalls {
|
|
322
|
+
pendingCall.resolve(["location": status])
|
|
323
|
+
}
|
|
324
|
+
}
|
|
279
325
|
}
|