Guest User

Untitled

a guest
Jan 21st, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. You need to create WifiConfiguration instance like this:
  2.  
  3. String networkSSID = "test";
  4. String networkPass = "pass";
  5.  
  6. WifiConfiguration conf = new WifiConfiguration();
  7. conf.SSID = """ + networkSSID + """; //
  8. Then, for WEP network you need to do this:
  9.  
  10. conf.wepKeys[0] = """ + networkPass + """;
  11. conf.wepTxKeyIndex = 0;
  12. conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
  13. conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
  14.  
  15. For WPA network you need to add passphrase like this:
  16.  
  17. conf.preSharedKey = """+ networkPass +""";
  18.  
  19. For Open network you need to do this:
  20.  
  21. conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
  22.  
  23. WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
  24. wifiManager.add(conf);
  25.  
  26. And finally, you might need to enable it, so Android conntects to it:
  27.  
  28. List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
  29. for( WifiConfiguration i : list ) {
  30. if(i.SSID != null && i.SSID.equals(""" + networkSSID + """)) {
  31. wm.disconnect();
  32. wm.enableNetwork(i.networkId, true);
  33. wm.reconnect();
  34.  
  35. break;
  36. }
  37. }
  38.  
  39. UPD: In case of WEP, if your password is in hex, you do not need to surround it wit
Add Comment
Please, Sign In to add comment