Skip to content

Commit 2db2c69

Browse files
authored
Merge pull request #64 from hassanmojab/master
Added connectToHiddenNetwork method
2 parents 51bcd99 + cec13a2 commit 2db2c69

1 file changed

Lines changed: 73 additions & 0 deletions

File tree

android/src/main/java/com/devstepbcn/wifi/AndroidWifiModule.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,79 @@ public Boolean connectTo(ScanResult result, String password, String ssid) {
288288

289289
return true;
290290
}
291+
292+
//add configuration of hidden network and return it's networkId
293+
public int setWifiConfig(String ssid, String sharedKey) {
294+
WifiConfiguration conf = new WifiConfiguration();
295+
296+
conf.SSID = "\"" + ssid + "\"";
297+
conf.preSharedKey = "\"" + sharedKey + "\"";
298+
299+
conf.hiddenSSID = true;
300+
conf.status = WifiConfiguration.Status.ENABLED;
301+
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
302+
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
303+
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
304+
conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
305+
conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
306+
conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
307+
conf.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
308+
309+
return wifi.addNetwork(conf);
310+
}
311+
312+
//Add a hidden wifi network and connect to it
313+
//Example: wifi.connectToHiddenNetwork(ssid, password, (networkAdded) => {});
314+
//Callback returns true if network added and tried to connect to it successfully
315+
//It may take up to 15s to connect to hidden networks
316+
@ReactMethod
317+
public void connectToHiddenNetwork(String ssid, String password, Callback networkAdded) {
318+
List<WifiConfiguration> list = wifi.getConfiguredNetworks();
319+
int updateNetwork = -1;
320+
321+
// check if network config exists and it's hidden
322+
for (WifiConfiguration wifiConfig : list) {
323+
if (wifiConfig.SSID.equals("\"" + ssid + "\"") && wifiConfig.hiddenSSID) {
324+
updateNetwork = wifiConfig.networkId;
325+
}
326+
}
327+
328+
// If network not already in configured networks add new network
329+
if (updateNetwork == -1) {
330+
updateNetwork = setWifiConfig(ssid, password);
331+
}
332+
333+
// if network not added return false
334+
if (updateNetwork == -1) {
335+
networkAdded.invoke(false);
336+
return;
337+
}
338+
339+
// disconnect current network
340+
boolean disconnect = wifi.disconnect();
341+
if (!disconnect) {
342+
networkAdded.invoke(false);
343+
return;
344+
}
345+
346+
// enable new network
347+
boolean enableNetwork = wifi.enableNetwork(updateNetwork, true);
348+
if (!enableNetwork) {
349+
networkAdded.invoke(false);
350+
return;
351+
}
352+
353+
// reconnect to new network
354+
boolean reconnect = wifi.reconnect();
355+
if (!reconnect) {
356+
networkAdded.invoke(false);
357+
return;
358+
}
359+
360+
wifi.saveConfiguration();
361+
362+
networkAdded.invoke(true);
363+
}
291364

292365
//Disconnect current Wifi.
293366
@ReactMethod

0 commit comments

Comments
 (0)