1

I can use the following code to get the name of Wifi, I hope to select WiFi programatically, how can I do?

It seems that wifiInfo.ssid is val , and it can't be assigned!

I set the required permission as

<!-- in AndroidManifest.xml -->
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

Then in the code

<!-- in Activity class -->
var wifiManager = mContext.applicationContext.getSystemService(WIFI_SERVICE) as WifiManager
var wifiInfo = wifiManager.connectionInfo

var name=wifiInfo.ssid
var isEnabled=wifiManager.isWifiEnabled

wifiInfo.ssid="MyNewWifi"  //It cause error

BTW,

I have read the artical How do I connect to a specific Wi-Fi network in Android programmatically?

It seems that I need to provide passsword in the above code when I reconnect the WIFI again.

In my mind, the password will be saved to configuration if I have connected to the wifi successfully, I hope that I needn't provide password in my code if I want to reconnect the WiFi again, how can I do?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
HelloCW
  • 843
  • 22
  • 125
  • 310
  • Normally if you want to change the ssid or password, you have to log in to the router console. And I think it cannot be done from client side – Tam Huynh Jun 24 '18 at 02:28
  • Can you elaborate on what you are trying to do? Are you trying to manually connect to a different access point ? – Carlos Paulino Jun 24 '18 at 03:22
  • Thanks! I hope to connect to a WiFi point programmatically! There are many points in my office. I hope to connect to my point for my phone – HelloCW Jun 24 '18 at 03:34

1 Answers1

1

You need to create a Wifi configuration like this.

String networkSSID = "testwifi";
String networkPass = "password";

WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + networkSSID + "\"";
conf.preSharedKey = "\""+ networkPass +"\"";
WifiManager wifiManager = 

(WifiManager)context.getSystemService(Context.WIFI_SERVICE); 
wifiManager.addNetwork(conf);

List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for( WifiConfiguration i : list ) {
    if(i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
         wifiManager.disconnect();
         wifiManager.enableNetwork(i.networkId, true);
         wifiManager.reconnect();               

         break;
    }           
 }

This code should work for WPA security settings.

Reference: Another similar question link

Zim
  • 182
  • 1
  • 1
  • 10
  • Thanks! How can I get the password of current connected WIFI programmatically? – HelloCW Jun 24 '18 at 05:48
  • That is not possible I think as the password is kept encrypted and you can't extract it unless you have a rooted phone. – Zim Jun 24 '18 at 05:49
  • Do I need to password in your code if I reconnect to the WIFI which I have connected successfully before? – HelloCW Jun 24 '18 at 05:55
  • Yes! every time you connect a Wifi network you must provide the password to connect. Android OS actually saves these configuration to reconnect later once you connect to a Wifi network. If the Wifi host changes the password, your phone will not connect and show some kind of authentication error! So you must provide the password every time you connect. – Zim Jun 24 '18 at 06:04
  • Thanks! If I have connected to the WIFI successfully before, the password must be saved in configuration. So I think I don't need to provide password in the code when I try to reconnect the WIFI again – HelloCW Jun 24 '18 at 06:08
  • Can you please accept the answer if it solves your problem? – Zim Jun 26 '18 at 04:10