community-cordova-plugin-wifi 1.0.1
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/.idea/community-cordova-plugin-cpu.iml +12 -0
- package/.idea/modules.xml +8 -0
- package/.idea/vcs.xml +6 -0
- package/.idea/workspace.xml +171 -0
- package/LICENSE +21 -0
- package/README.md +162 -0
- package/RELEASENOTES.md +4 -0
- package/package.json +18 -0
- package/plugin.xml +67 -0
- package/src/android/IpInfoUtils.java +154 -0
- package/src/android/PingTask.java +56 -0
- package/src/android/WifiDetailsUtils.java +78 -0
- package/src/android/WifiPlugin.java +236 -0
- package/src/android/WifiUtils.java +268 -0
- package/src/ios/WifiPlugin.h +21 -0
- package/src/ios/WifiPlugin.m +345 -0
- package/types/index.d.ts +98 -0
- package/www/plugin.js +101 -0
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
package wifiplugin;
|
|
2
|
+
|
|
3
|
+
import org.apache.cordova.CallbackContext;
|
|
4
|
+
import org.apache.cordova.PluginResult;
|
|
5
|
+
import java.io.BufferedReader;
|
|
6
|
+
import java.io.InputStreamReader;
|
|
7
|
+
import org.json.JSONObject;
|
|
8
|
+
import org.apache.cordova.CordovaInterface;
|
|
9
|
+
|
|
10
|
+
public class PingTask {
|
|
11
|
+
|
|
12
|
+
public static void ping(String address, int count, int timeout, CallbackContext callbackContext, CordovaInterface cordova) {
|
|
13
|
+
cordova.getThreadPool().execute(() -> {
|
|
14
|
+
try {
|
|
15
|
+
String command = "/system/bin/ping -c " + count + " -W " + timeout + " " + address;
|
|
16
|
+
Process process = Runtime.getRuntime().exec(command);
|
|
17
|
+
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
|
18
|
+
|
|
19
|
+
int linesRead = 0;
|
|
20
|
+
String line;
|
|
21
|
+
String fullResponse = "";
|
|
22
|
+
while ((line = reader.readLine()) != null) {
|
|
23
|
+
linesRead++;
|
|
24
|
+
JSONObject progressUpdate = new JSONObject();
|
|
25
|
+
// Ensure progress does not exceed 100
|
|
26
|
+
int progress = (int) (((double) linesRead / count) * 100);
|
|
27
|
+
progress = Math.min(progress, 100); // Cap progress at 100
|
|
28
|
+
progressUpdate.put("line", line);
|
|
29
|
+
if (fullResponse != null && !fullResponse.isEmpty()) {
|
|
30
|
+
fullResponse = fullResponse + "\n" + line;
|
|
31
|
+
} else {
|
|
32
|
+
fullResponse = line;
|
|
33
|
+
}
|
|
34
|
+
progressUpdate.put("line", line);
|
|
35
|
+
progressUpdate.put("fullResponse", fullResponse);
|
|
36
|
+
progressUpdate.put("progress", progress);
|
|
37
|
+
PluginResult progressResult = new PluginResult(PluginResult.Status.OK, progressUpdate);
|
|
38
|
+
progressResult.setKeepCallback(true);
|
|
39
|
+
callbackContext.sendPluginResult(progressResult);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
process.waitFor();
|
|
43
|
+
JSONObject finalResult = new JSONObject();
|
|
44
|
+
finalResult.put("line", "");
|
|
45
|
+
finalResult.put("fullResponse", fullResponse);
|
|
46
|
+
finalResult.put("progress", 100);
|
|
47
|
+
finalResult.put("status", "completed");
|
|
48
|
+
finalResult.put("linesRead", linesRead);
|
|
49
|
+
PluginResult result = new PluginResult(PluginResult.Status.OK, finalResult);
|
|
50
|
+
callbackContext.sendPluginResult(result);
|
|
51
|
+
} catch (Exception e) {
|
|
52
|
+
callbackContext.error("Error during ping: " + e.getMessage());
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
package wifiplugin;
|
|
2
|
+
|
|
3
|
+
import android.content.Context;
|
|
4
|
+
import android.net.ConnectivityManager;
|
|
5
|
+
import android.net.wifi.WifiInfo;
|
|
6
|
+
import android.net.wifi.WifiManager;
|
|
7
|
+
import android.text.format.Formatter;
|
|
8
|
+
import org.apache.cordova.CallbackContext;
|
|
9
|
+
import org.apache.cordova.CordovaInterface;
|
|
10
|
+
import org.apache.cordova.PluginResult;
|
|
11
|
+
import org.json.JSONException;
|
|
12
|
+
import org.json.JSONObject;
|
|
13
|
+
import android.net.DhcpInfo;
|
|
14
|
+
import java.util.Iterator;
|
|
15
|
+
|
|
16
|
+
public class WifiDetailsUtils {
|
|
17
|
+
|
|
18
|
+
public static void getAllWifiDetails(CordovaInterface cordova, CallbackContext callbackContext) {
|
|
19
|
+
cordova.getThreadPool().execute(() -> {
|
|
20
|
+
try {
|
|
21
|
+
WifiManager wifiManager = (WifiManager) cordova.getActivity().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
|
|
22
|
+
if (wifiManager == null) {
|
|
23
|
+
callbackContext.error("WifiManager not available");
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Check if Wi-Fi is enabled and supported
|
|
28
|
+
boolean isWifiEnabled = wifiManager.isWifiEnabled();
|
|
29
|
+
|
|
30
|
+
// Get Wi-Fi and DHCP details
|
|
31
|
+
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
|
|
32
|
+
DhcpInfo dhcpInfo = wifiManager.getDhcpInfo();
|
|
33
|
+
|
|
34
|
+
JSONObject wifiDetails = new JSONObject();
|
|
35
|
+
wifiDetails.put("iswifienabled", isWifiEnabled);
|
|
36
|
+
wifiDetails.put("issupportwifi", true); // If wifiManager isn't null, device supports WiFi
|
|
37
|
+
wifiDetails.put("ssid", wifiInfo.getSSID());
|
|
38
|
+
wifiDetails.put("bssid", wifiInfo.getBSSID());
|
|
39
|
+
wifiDetails.put("ip", Formatter.formatIpAddress(wifiInfo.getIpAddress()));
|
|
40
|
+
wifiDetails.put("mac", wifiInfo.getMacAddress());
|
|
41
|
+
wifiDetails.put("networkid", wifiInfo.getNetworkId());
|
|
42
|
+
wifiDetails.put("linkspeed", wifiInfo.getLinkSpeed() + " Mbps");
|
|
43
|
+
wifiDetails.put("signalstrength", wifiInfo.getRssi());
|
|
44
|
+
wifiDetails.put("gateway", Formatter.formatIpAddress(dhcpInfo.gateway));
|
|
45
|
+
wifiDetails.put("rssi", wifiInfo.getRssi());
|
|
46
|
+
wifiDetails.put("speed", wifiInfo.getLinkSpeed() + " Mbps");
|
|
47
|
+
wifiDetails.put("frequency", wifiInfo.getFrequency() + " MHz");
|
|
48
|
+
wifiDetails.put("channel", getChannelFromFrequency(wifiInfo.getFrequency()));
|
|
49
|
+
wifiDetails.put("dns1", Formatter.formatIpAddress(dhcpInfo.dns1));
|
|
50
|
+
wifiDetails.put("dns2", Formatter.formatIpAddress(dhcpInfo.dns2));
|
|
51
|
+
|
|
52
|
+
// Convert keys to lowercase
|
|
53
|
+
JSONObject lowerCaseWifiDetails = new JSONObject();
|
|
54
|
+
Iterator<String> keys = wifiDetails.keys();
|
|
55
|
+
while (keys.hasNext()) {
|
|
56
|
+
String key = keys.next();
|
|
57
|
+
lowerCaseWifiDetails.put(key.toLowerCase(), wifiDetails.get(key));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
PluginResult result = new PluginResult(PluginResult.Status.OK, lowerCaseWifiDetails);
|
|
61
|
+
callbackContext.sendPluginResult(result);
|
|
62
|
+
} catch (Exception e) {
|
|
63
|
+
callbackContext.error("Error getting Wi-Fi details: " + e.getMessage());
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
// Helper method to get the Wi-Fi channel from frequency
|
|
70
|
+
private static int getChannelFromFrequency(int frequency) {
|
|
71
|
+
if (frequency >= 2412 && frequency <= 2484) {
|
|
72
|
+
return (frequency - 2412) / 5 + 1;
|
|
73
|
+
} else if (frequency >= 5170 && frequency <= 5825) {
|
|
74
|
+
return (frequency - 5170) / 5 + 34;
|
|
75
|
+
}
|
|
76
|
+
return -1; // Unknown channel
|
|
77
|
+
}
|
|
78
|
+
}
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
package wifiplugin;
|
|
2
|
+
|
|
3
|
+
import android.content.Context;
|
|
4
|
+
import android.net.ConnectivityManager;
|
|
5
|
+
import android.net.Network;
|
|
6
|
+
import android.net.NetworkCapabilities;
|
|
7
|
+
import android.net.NetworkRequest;
|
|
8
|
+
import android.net.NetworkInfo;
|
|
9
|
+
import android.net.wifi.WifiConfiguration;
|
|
10
|
+
import android.net.wifi.WifiInfo;
|
|
11
|
+
import android.net.wifi.WifiManager;
|
|
12
|
+
import android.os.Build;
|
|
13
|
+
import androidx.annotation.RequiresApi;
|
|
14
|
+
import android.net.wifi.WifiNetworkSpecifier;
|
|
15
|
+
import android.net.NetworkSpecifier;
|
|
16
|
+
|
|
17
|
+
import org.apache.cordova.CallbackContext;
|
|
18
|
+
import org.apache.cordova.CordovaInterface;
|
|
19
|
+
import org.apache.cordova.CordovaPlugin;
|
|
20
|
+
import org.apache.cordova.PluginResult;
|
|
21
|
+
import org.json.JSONArray;
|
|
22
|
+
import org.json.JSONException;
|
|
23
|
+
import org.json.JSONObject;
|
|
24
|
+
|
|
25
|
+
import java.util.List;
|
|
26
|
+
|
|
27
|
+
public class WifiPlugin extends CordovaPlugin {
|
|
28
|
+
private static final String TAG = "WifiPlugin";
|
|
29
|
+
private static final int MY_PERMISSIONS_REQUEST_WIFI_STATE = 1;
|
|
30
|
+
|
|
31
|
+
@Override
|
|
32
|
+
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
|
|
33
|
+
if ("getWifiList".equals(action)) {
|
|
34
|
+
WifiUtils.getWifiList(cordova, callbackContext);
|
|
35
|
+
return true;
|
|
36
|
+
} else if ("ping".equals(action)) {
|
|
37
|
+
String address = args.getString(0);
|
|
38
|
+
int count = args.getInt(1);
|
|
39
|
+
int timeout = args.getInt(2);
|
|
40
|
+
PingTask.ping(address, count, timeout, callbackContext, cordova);
|
|
41
|
+
return true;
|
|
42
|
+
} else if ("getIpInfo".equals(action)) {
|
|
43
|
+
IpInfoUtils.getIpInfo(cordova, callbackContext);
|
|
44
|
+
return true;
|
|
45
|
+
} else if ("getSignalStrength".equals(action)) {
|
|
46
|
+
getSignalStrength(callbackContext);
|
|
47
|
+
return true;
|
|
48
|
+
} else if ("getWifiStrength".equals(action)) {
|
|
49
|
+
getWifiStrength(callbackContext);
|
|
50
|
+
return true;
|
|
51
|
+
} else if ("getAllWifiDetails".equals(action)) {
|
|
52
|
+
WifiDetailsUtils.getAllWifiDetails(cordova, callbackContext);
|
|
53
|
+
return true;
|
|
54
|
+
} else if ("getConnectedDevices".equals(action)) {
|
|
55
|
+
WifiUtils.getConnectedDevices(cordova.getActivity().getApplicationContext(), cordova, callbackContext);
|
|
56
|
+
return true;
|
|
57
|
+
} else if ("isConnectedToInternet".equals(action)) {
|
|
58
|
+
isConnectedToInternet(callbackContext);
|
|
59
|
+
return true;
|
|
60
|
+
} else if ("canConnectToInternet".equals(action)) {
|
|
61
|
+
canConnectToInternet(callbackContext);
|
|
62
|
+
return true;
|
|
63
|
+
} else if ("canConnectToRouter".equals(action)) {
|
|
64
|
+
canConnectToRouter(callbackContext);
|
|
65
|
+
return true;
|
|
66
|
+
} else if ("connectToNetwork".equals(action)) {
|
|
67
|
+
String ssid = args.optString(0);
|
|
68
|
+
String password = args.optString(1);
|
|
69
|
+
connectToWifi(ssid, password, callbackContext);
|
|
70
|
+
return true;
|
|
71
|
+
} else if ("disconnectFromNetwork".equals(action)) {
|
|
72
|
+
disconnectFromNetwork(callbackContext);
|
|
73
|
+
return true;
|
|
74
|
+
} else if ("isWifiEnabled".equals(action)) {
|
|
75
|
+
isWifiEnabled(callbackContext);
|
|
76
|
+
return true;
|
|
77
|
+
} else if ("wifiToggle".equals(action)) {
|
|
78
|
+
wifiToggle(callbackContext);
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
private void getSignalStrength(CallbackContext callbackContext) {
|
|
85
|
+
WifiUtils.getSignalStrength(cordova.getActivity().getApplicationContext(), callbackContext);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
private void getWifiStrength(CallbackContext callbackContext) {
|
|
89
|
+
WifiUtils.getWifiStrength(cordova.getActivity().getApplicationContext(), callbackContext);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
private void wifiToggle(CallbackContext callbackContext) {
|
|
93
|
+
cordova.getThreadPool().execute(() -> {
|
|
94
|
+
try {
|
|
95
|
+
WifiManager wifiManager = (WifiManager) cordova.getActivity().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
|
|
96
|
+
if (wifiManager != null) {
|
|
97
|
+
wifiManager.setWifiEnabled(!wifiManager.isWifiEnabled());
|
|
98
|
+
callbackContext.success();
|
|
99
|
+
} else {
|
|
100
|
+
callbackContext.error("Error toggling Wi-Fi");
|
|
101
|
+
}
|
|
102
|
+
} catch (Exception e) {
|
|
103
|
+
callbackContext.error("Error toggling Wi-Fi: " + e.getMessage());
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
private void isWifiEnabled(CallbackContext callbackContext) {
|
|
109
|
+
try {
|
|
110
|
+
WifiManager wifiManager = (WifiManager) cordova.getActivity().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
|
|
111
|
+
boolean isEnabled = wifiManager.isWifiEnabled();
|
|
112
|
+
callbackContext.success(isEnabled ? 1 : 0);
|
|
113
|
+
} catch (Exception e) {
|
|
114
|
+
callbackContext.error("Error checking Wi-Fi status: " + e.getMessage());
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
private void isConnectedToInternet(CallbackContext callbackContext) {
|
|
119
|
+
ConnectivityManager connectivityManager = (ConnectivityManager) cordova.getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
120
|
+
if (connectivityManager != null) {
|
|
121
|
+
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
|
|
122
|
+
boolean isConnected = activeNetworkInfo != null && activeNetworkInfo.isConnected();
|
|
123
|
+
callbackContext.success(isConnected ? 1 : 0);
|
|
124
|
+
} else {
|
|
125
|
+
callbackContext.error("ConnectivityManager is null");
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
private void canConnectToInternet(CallbackContext callbackContext) {
|
|
130
|
+
ConnectivityManager connectivityManager = (ConnectivityManager) cordova.getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
131
|
+
if (connectivityManager != null) {
|
|
132
|
+
NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(connectivityManager.getActiveNetwork());
|
|
133
|
+
boolean canConnect = capabilities != null && capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
|
|
134
|
+
callbackContext.success(canConnect ? 1 : 0);
|
|
135
|
+
} else {
|
|
136
|
+
callbackContext.error("ConnectivityManager is null");
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
private void canConnectToRouter(CallbackContext callbackContext) {
|
|
141
|
+
ConnectivityManager connectivityManager = (ConnectivityManager) cordova.getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
142
|
+
if (connectivityManager != null) {
|
|
143
|
+
NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(connectivityManager.getActiveNetwork());
|
|
144
|
+
boolean canConnectToRouter = capabilities != null && capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI);
|
|
145
|
+
callbackContext.success(canConnectToRouter ? 1 : 0);
|
|
146
|
+
} else {
|
|
147
|
+
callbackContext.error("ConnectivityManager is null");
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
public void connectToWifi(String ssid, String password, CallbackContext callbackContext) {
|
|
152
|
+
WifiManager wifiManager = (WifiManager) cordova.getActivity().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
|
|
153
|
+
|
|
154
|
+
if (!wifiManager.isWifiEnabled()) {
|
|
155
|
+
wifiManager.setWifiEnabled(true);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
|
|
159
|
+
connectToWifiPreQ(wifiManager, ssid, password, callbackContext);
|
|
160
|
+
} else {
|
|
161
|
+
connectToWifiQAndAbove(ssid, password, callbackContext);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
private void connectToWifiPreQ(WifiManager wifiManager, String ssid, String password, CallbackContext callbackContext) {
|
|
166
|
+
WifiConfiguration wifiConfig = new WifiConfiguration();
|
|
167
|
+
wifiConfig.SSID = "\"" + ssid + "\"";
|
|
168
|
+
wifiConfig.preSharedKey = "\"" + password + "\"";
|
|
169
|
+
wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
|
|
170
|
+
|
|
171
|
+
// Remove existing configurations for the same SSID
|
|
172
|
+
List<WifiConfiguration> configuredNetworks = wifiManager.getConfiguredNetworks();
|
|
173
|
+
if (configuredNetworks != null) {
|
|
174
|
+
for (WifiConfiguration existingConfig : configuredNetworks) {
|
|
175
|
+
if (existingConfig.SSID != null && existingConfig.SSID.equals(wifiConfig.SSID)) {
|
|
176
|
+
wifiManager.removeNetwork(existingConfig.networkId);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
int networkId = wifiManager.addNetwork(wifiConfig);
|
|
182
|
+
if (networkId != -1) {
|
|
183
|
+
wifiManager.disconnect();
|
|
184
|
+
wifiManager.enableNetwork(networkId, true);
|
|
185
|
+
wifiManager.reconnect();
|
|
186
|
+
|
|
187
|
+
callbackContext.success("Connected to Wi-Fi network: " + ssid);
|
|
188
|
+
} else {
|
|
189
|
+
callbackContext.error("Failed to add Wi-Fi network configuration");
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
@RequiresApi(api = Build.VERSION_CODES.Q)
|
|
194
|
+
private void connectToWifiQAndAbove(String ssid, String password, CallbackContext callbackContext) {
|
|
195
|
+
WifiNetworkSpecifier.Builder builder = new WifiNetworkSpecifier.Builder()
|
|
196
|
+
.setSsid(ssid)
|
|
197
|
+
.setWpa2Passphrase(password);
|
|
198
|
+
WifiManager wifiManager = (WifiManager) cordova.getActivity().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
|
|
199
|
+
NetworkSpecifier specifier = builder.build();
|
|
200
|
+
|
|
201
|
+
NetworkRequest request = new NetworkRequest.Builder()
|
|
202
|
+
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
|
|
203
|
+
.removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
|
|
204
|
+
.setNetworkSpecifier(specifier)
|
|
205
|
+
.build();
|
|
206
|
+
|
|
207
|
+
ConnectivityManager connectivityManager = (ConnectivityManager) cordova.getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
208
|
+
connectivityManager.requestNetwork(request, new ConnectivityManager.NetworkCallback() {
|
|
209
|
+
@Override
|
|
210
|
+
public void onAvailable(Network network) {
|
|
211
|
+
super.onAvailable(network);
|
|
212
|
+
wifiManager.setWifiEnabled(false);
|
|
213
|
+
|
|
214
|
+
callbackContext.success("Connected to Wi-Fi network: " + ssid);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
@Override
|
|
218
|
+
public void onUnavailable() {
|
|
219
|
+
super.onUnavailable();
|
|
220
|
+
callbackContext.error("Failed to connect to Wi-Fi network");
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
private void disconnectFromNetwork(CallbackContext callbackContext) {
|
|
226
|
+
WifiManager wifiManager = (WifiManager) cordova.getActivity().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
|
|
227
|
+
|
|
228
|
+
if (!wifiManager.isWifiEnabled()) {
|
|
229
|
+
callbackContext.error("Wi-Fi is disabled");
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
wifiManager.disconnect();
|
|
234
|
+
callbackContext.success("Disconnected from Wi-Fi network");
|
|
235
|
+
}
|
|
236
|
+
}
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
package wifiplugin;
|
|
2
|
+
|
|
3
|
+
import android.content.Context;
|
|
4
|
+
import android.net.ConnectivityManager;
|
|
5
|
+
import android.net.Network;
|
|
6
|
+
import android.net.NetworkCapabilities;
|
|
7
|
+
import android.net.NetworkInfo;
|
|
8
|
+
import android.net.wifi.WifiInfo;
|
|
9
|
+
import android.net.wifi.WifiManager;
|
|
10
|
+
import android.text.format.Formatter;
|
|
11
|
+
import org.json.JSONObject;
|
|
12
|
+
import org.apache.cordova.PluginResult;
|
|
13
|
+
import org.json.JSONException;
|
|
14
|
+
import java.net.UnknownHostException;
|
|
15
|
+
import java.io.IOException;
|
|
16
|
+
import org.json.JSONArray;
|
|
17
|
+
import org.apache.cordova.CallbackContext;
|
|
18
|
+
import org.apache.cordova.CordovaInterface;
|
|
19
|
+
import org.apache.cordova.CordovaPlugin;
|
|
20
|
+
import org.apache.cordova.PluginResult;
|
|
21
|
+
import android.content.Context;
|
|
22
|
+
import java.net.InetAddress;
|
|
23
|
+
import java.util.concurrent.CountDownLatch;
|
|
24
|
+
import java.util.concurrent.ExecutorService;
|
|
25
|
+
import java.util.concurrent.Executors;
|
|
26
|
+
import android.net.wifi.ScanResult;
|
|
27
|
+
import java.util.List;
|
|
28
|
+
import java.util.Collections;
|
|
29
|
+
import java.util.Comparator;
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
public class WifiUtils {
|
|
33
|
+
public static void getSignalStrength(Context context, CallbackContext callbackContext) {
|
|
34
|
+
try {
|
|
35
|
+
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
|
|
36
|
+
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
|
|
37
|
+
int signalStrength = wifiInfo.getRssi();
|
|
38
|
+
|
|
39
|
+
PluginResult result = new PluginResult(PluginResult.Status.OK, signalStrength);
|
|
40
|
+
callbackContext.sendPluginResult(result);
|
|
41
|
+
} catch (Exception e) {
|
|
42
|
+
callbackContext.error("Error getting signal strength: " + e.getMessage());
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
public static void getWifiStrength(Context context, CallbackContext callbackContext) {
|
|
47
|
+
try {
|
|
48
|
+
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
|
|
49
|
+
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
|
|
50
|
+
int wifiStrength = WifiManager.calculateSignalLevel(wifiInfo.getRssi(), 5);
|
|
51
|
+
|
|
52
|
+
PluginResult result = new PluginResult(PluginResult.Status.OK, wifiStrength);
|
|
53
|
+
callbackContext.sendPluginResult(result);
|
|
54
|
+
} catch (Exception e) {
|
|
55
|
+
callbackContext.error("Error getting WiFi strength: " + e.getMessage());
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
public static void getWifiList(CordovaInterface cordova, CallbackContext callbackContext) {
|
|
60
|
+
WifiManager wifiManager = (WifiManager) cordova.getActivity().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
|
|
61
|
+
|
|
62
|
+
if (!wifiManager.isWifiEnabled()) {
|
|
63
|
+
callbackContext.error("Wi-Fi is disabled");
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
wifiManager.startScan();
|
|
68
|
+
List<ScanResult> scanResults = wifiManager.getScanResults();
|
|
69
|
+
|
|
70
|
+
// Sort the scan results by Level in ascending order
|
|
71
|
+
Collections.sort(scanResults, new Comparator<ScanResult>() {
|
|
72
|
+
@Override
|
|
73
|
+
public int compare(ScanResult o1, ScanResult o2) {
|
|
74
|
+
// Sort in descending order of signal strength (higher values first)
|
|
75
|
+
return Integer.compare(o2.level, o1.level);
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
JSONArray wifiArray = new JSONArray();
|
|
80
|
+
for (ScanResult scanResult : scanResults) {
|
|
81
|
+
JSONObject wifiObject = new JSONObject();
|
|
82
|
+
try {
|
|
83
|
+
wifiObject.put("SSID", scanResult.SSID);
|
|
84
|
+
wifiObject.put("BSSID", scanResult.BSSID);
|
|
85
|
+
wifiObject.put("capabilities", scanResult.capabilities);
|
|
86
|
+
wifiObject.put("frequency", scanResult.frequency / 1000.0 + " GHz"); // Convert frequency to GHz
|
|
87
|
+
wifiObject.put("security", getSecurityType(scanResult)); // Get security type
|
|
88
|
+
wifiObject.put("level", scanResult.level); // Signal strength in dBm
|
|
89
|
+
// Manufacturer is not directly available from ScanResult
|
|
90
|
+
wifiObject.put("channelWidth", getChannelWidth(scanResult.channelWidth)); // Get channel width
|
|
91
|
+
// PHY Mode and PHY Speed capability are not directly available from ScanResult
|
|
92
|
+
wifiObject.put("distance", calculateDistance(scanResult.level, scanResult.frequency)); // Estimate distance
|
|
93
|
+
wifiObject.put("hasPassword", hasPassword(scanResult.capabilities)); // Determine if network is secured
|
|
94
|
+
|
|
95
|
+
wifiArray.put(wifiObject);
|
|
96
|
+
} catch (JSONException e) {
|
|
97
|
+
e.printStackTrace();
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
PluginResult result = new PluginResult(PluginResult.Status.OK, wifiArray);
|
|
102
|
+
callbackContext.sendPluginResult(result);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Utility method to check if network is secured
|
|
106
|
+
private static boolean hasPassword(String capabilities) {
|
|
107
|
+
return capabilities.contains("WEP") || capabilities.contains("WPA") ||
|
|
108
|
+
capabilities.contains("WPA2") || capabilities.contains("WPA3") ||
|
|
109
|
+
capabilities.contains("PSK");
|
|
110
|
+
}
|
|
111
|
+
// Utility method to determine security type from capabilities
|
|
112
|
+
private static String getSecurityType(ScanResult scanResult) {
|
|
113
|
+
if (scanResult.capabilities.contains("WEP")) {
|
|
114
|
+
return "WEP";
|
|
115
|
+
} else if (scanResult.capabilities.contains("WPA")) {
|
|
116
|
+
return "WPA";
|
|
117
|
+
} else if (scanResult.capabilities.contains("WPA2")) {
|
|
118
|
+
return "WPA2";
|
|
119
|
+
} else if (scanResult.capabilities.contains("WPA3")) {
|
|
120
|
+
return "WPA3";
|
|
121
|
+
}
|
|
122
|
+
return "Open";
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
// Utility method to get channel width description
|
|
127
|
+
private static String getChannelWidth(int channelWidth) {
|
|
128
|
+
switch (channelWidth) {
|
|
129
|
+
case ScanResult.CHANNEL_WIDTH_20MHZ:
|
|
130
|
+
return "20 MHz";
|
|
131
|
+
case ScanResult.CHANNEL_WIDTH_40MHZ:
|
|
132
|
+
return "40 MHz";
|
|
133
|
+
case ScanResult.CHANNEL_WIDTH_80MHZ:
|
|
134
|
+
return "80 MHz";
|
|
135
|
+
case ScanResult.CHANNEL_WIDTH_160MHZ:
|
|
136
|
+
return "160 MHz";
|
|
137
|
+
default:
|
|
138
|
+
return "Unknown";
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// Utility method to estimate distance and format it to 2 decimal places
|
|
143
|
+
private static String calculateDistance(double signalLevelInDb, double freqInMHz) {
|
|
144
|
+
double exp = (27.55 - (20 * Math.log10(freqInMHz)) + Math.abs(signalLevelInDb)) / 20.0;
|
|
145
|
+
double distance = Math.pow(10.0, exp);
|
|
146
|
+
return String.format("%.2f", distance);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
private int calculateWifiStrength(WifiManager wifiManager) {
|
|
150
|
+
int numberOfLevels = 5; // You can adjust this based on your preference
|
|
151
|
+
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
|
|
152
|
+
int signalLevel = WifiManager.calculateSignalLevel(wifiInfo.getRssi(), numberOfLevels);
|
|
153
|
+
return (signalLevel * 100) / numberOfLevels;
|
|
154
|
+
}
|
|
155
|
+
public static void getConnectedDevices(Context context,CordovaInterface cordova, CallbackContext callbackContext) {
|
|
156
|
+
cordova.getThreadPool().execute(() -> {
|
|
157
|
+
try {
|
|
158
|
+
JSONArray devicesArray = new JSONArray();
|
|
159
|
+
|
|
160
|
+
// Get the current device's IP address
|
|
161
|
+
WifiManager wifiManager = (WifiManager) cordova.getActivity().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
|
|
162
|
+
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
|
|
163
|
+
String ipAddress = Formatter.formatIpAddress(wifiInfo.getIpAddress());
|
|
164
|
+
String subnet = ipAddress.substring(0, ipAddress.lastIndexOf("."));
|
|
165
|
+
|
|
166
|
+
// Perform IP scanning to find connected devices
|
|
167
|
+
ExecutorService executor = Executors.newFixedThreadPool(20);
|
|
168
|
+
CountDownLatch latch = new CountDownLatch(255);
|
|
169
|
+
|
|
170
|
+
for (int i = 1; i <= 255; i++) {
|
|
171
|
+
final int host = i;
|
|
172
|
+
executor.submit(() -> {
|
|
173
|
+
String ip = subnet + "." + host;
|
|
174
|
+
try {
|
|
175
|
+
if (InetAddress.getByName(ip).isReachable(100)) {
|
|
176
|
+
JSONObject deviceObject = new JSONObject();
|
|
177
|
+
deviceObject.put("ipAddress", ip);
|
|
178
|
+
deviceObject.put("deviceName", getDeviceNameByIp(ip));
|
|
179
|
+
deviceObject.put("localHost", isLocalHost(ip));
|
|
180
|
+
deviceObject.put("loopbackAddress", isLoopbackAddress(ip));
|
|
181
|
+
deviceObject.put("hostAddress", getHostAddress(ip));
|
|
182
|
+
deviceObject.put("canonicalHostName", getCanonicalHostName(ip));
|
|
183
|
+
deviceObject.put("multicastAddress", isMulticastAddress(ip));
|
|
184
|
+
deviceObject.put("siteLocalAddress", isSiteLocalAddress(ip));
|
|
185
|
+
devicesArray.put(deviceObject);
|
|
186
|
+
}
|
|
187
|
+
} catch (UnknownHostException | JSONException e) {
|
|
188
|
+
e.printStackTrace();
|
|
189
|
+
} catch (IOException e) {
|
|
190
|
+
e.printStackTrace();
|
|
191
|
+
} finally {
|
|
192
|
+
latch.countDown();
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
executor.shutdown();
|
|
198
|
+
|
|
199
|
+
// Wait for all threads to finish
|
|
200
|
+
latch.await();
|
|
201
|
+
|
|
202
|
+
PluginResult result = new PluginResult(PluginResult.Status.OK, devicesArray);
|
|
203
|
+
callbackContext.sendPluginResult(result);
|
|
204
|
+
} catch (Exception e) {
|
|
205
|
+
callbackContext.error("Error getting connected devices: " + e.getMessage());
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
private static String getDeviceNameByIp(String ipAddress) {
|
|
210
|
+
try {
|
|
211
|
+
InetAddress inetAddress = InetAddress.getByName(ipAddress);
|
|
212
|
+
return inetAddress.getHostName();
|
|
213
|
+
}
|
|
214
|
+
catch (IOException e) {
|
|
215
|
+
e.printStackTrace();
|
|
216
|
+
return "Unknown";
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
private static boolean isLocalHost(String ipAddress) {
|
|
221
|
+
return InetAddress.getLoopbackAddress().getHostAddress().equals(ipAddress);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
private static boolean isLoopbackAddress(String ipAddress) {
|
|
225
|
+
try {
|
|
226
|
+
return InetAddress.getByName(ipAddress).isLoopbackAddress();
|
|
227
|
+
} catch (UnknownHostException e) {
|
|
228
|
+
e.printStackTrace();
|
|
229
|
+
return false;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
private static String getHostAddress(String ipAddress) {
|
|
234
|
+
try {
|
|
235
|
+
return InetAddress.getByName(ipAddress).getHostAddress();
|
|
236
|
+
} catch (UnknownHostException e) {
|
|
237
|
+
e.printStackTrace();
|
|
238
|
+
return "Unknown";
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
private static String getCanonicalHostName(String ipAddress) {
|
|
243
|
+
try {
|
|
244
|
+
return InetAddress.getByName(ipAddress).getCanonicalHostName();
|
|
245
|
+
} catch (UnknownHostException e) {
|
|
246
|
+
e.printStackTrace();
|
|
247
|
+
return "Unknown";
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
private static boolean isMulticastAddress(String ipAddress) {
|
|
252
|
+
try {
|
|
253
|
+
return InetAddress.getByName(ipAddress).isMulticastAddress();
|
|
254
|
+
} catch (UnknownHostException e) {
|
|
255
|
+
e.printStackTrace();
|
|
256
|
+
return false;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
private static boolean isSiteLocalAddress(String ipAddress) {
|
|
261
|
+
try {
|
|
262
|
+
return InetAddress.getByName(ipAddress).isSiteLocalAddress();
|
|
263
|
+
} catch (UnknownHostException e) {
|
|
264
|
+
e.printStackTrace();
|
|
265
|
+
return false;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
#import <Cordova/CDVPlugin.h>
|
|
2
|
+
|
|
3
|
+
@interface WifiPlugin : CDVPlugin
|
|
4
|
+
|
|
5
|
+
- (void)getWifiList:(CDVInvokedUrlCommand*)command;
|
|
6
|
+
- (void)connectToNetwork:(CDVInvokedUrlCommand*)command;
|
|
7
|
+
- (void)disconnectFromNetwork:(CDVInvokedUrlCommand*)command;
|
|
8
|
+
- (void)isWifiEnabled:(CDVInvokedUrlCommand*)command;
|
|
9
|
+
- (void)wifiToggle:(CDVInvokedUrlCommand*)command;
|
|
10
|
+
- (void)isConnectedToInternet:(CDVInvokedUrlCommand*)command;
|
|
11
|
+
- (void)checkAndRequestWifiPermission:(CDVInvokedUrlCommand*)command;
|
|
12
|
+
- (void)getConnectedDevices:(CDVInvokedUrlCommand*)command;
|
|
13
|
+
- (void)getWifiStrength:(CDVInvokedUrlCommand*)command;
|
|
14
|
+
- (void)getSignalStrength:(CDVInvokedUrlCommand*)command;
|
|
15
|
+
- (void)ping:(CDVInvokedUrlCommand*)command;
|
|
16
|
+
- (void)getIpInfo:(CDVInvokedUrlCommand*)command;
|
|
17
|
+
- (void)getAllWifiDetails:(CDVInvokedUrlCommand*)command;
|
|
18
|
+
- (void)canConnectToInternet:(CDVInvokedUrlCommand*)command;
|
|
19
|
+
- (void)canConnectToRouter:(CDVInvokedUrlCommand*)command;
|
|
20
|
+
|
|
21
|
+
@end
|