8

I'm wondering if there are some code snippets that can be used to connect an Android device to a WiFi network. The network should be either open or WEP/WPA encypted, and visible to that device. Normally, we use GUI interface to input WiFi passwords and tap the connect button. I want to store the password in a place, and use the password to connect to the network seamlessly without human interaction. Is that possible? Thanks a lot.

Longbiao CHEN
  • 1,053
  • 3
  • 11
  • 21

4 Answers4

15

Thanks guys. With your help, I'm now able to connect to a WPA/PSK encrypted network without pain. Here is my code snippet:

        WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        // setup a wifi configuration
        WifiConfiguration wc = new WifiConfiguration();
        wc.SSID = "\"YOUR_SSID\"";
        wc.preSharedKey = "\"YOUR_PASSWORD\"";
        wc.status = WifiConfiguration.Status.ENABLED;
        wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
        wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
        wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
        wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
        wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
        wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
        // connect to and enable the connection
        int netId = wifiManager.addNetwork(wc);
        wifiManager.enableNetwork(netId, true);
        wifiManager.setWifiEnabled(true);

The tricks are :

  • SSID string should be surrounded with ", which is denoted by \"
  • addNetwork() method DISABLES the added network by default, so you should enable it with the enableNetwork() method.
Longbiao CHEN
  • 1,053
  • 3
  • 11
  • 21
  • Amazing solution! any idea how to find out if the entered password is wrong ? i tried to add a try catch , but its not working, the password is saved into the wifi network, and the only way to connect to it again is to either forget the network or change the password – Zame Mar 13 '16 at 17:24
3

To make OPs sample code work, I had to add one more line:

wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);

Without that line it just won't connect to the network. The configuration is accepted and added, but no connection attempts are made. I actually got the following message in the logcat window:

Event [WPA: Failed to select WPA/RSN] android

which put me to the final solution, figuring out why it didn't work for me.

Wouter
  • 2,623
  • 4
  • 34
  • 43
2

WifiManager - Have you tried looking here. The addNetwork() method looks like it can do what you want it to do. All you have to do is put the information in a WifiConfiguration class and then add the network, then enable that connection. The Documentation is all there.

Nicholas
  • 7,403
  • 10
  • 48
  • 76
1

Checkout the documentation for "WifiManager"

It can be used to enable wifi:

WifiManager wifiManager = (WifiManager)this.context.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(true);

And it can be used to do many other things.

Edit: Don't forget to update your permissions when monitoring and changing wifi state, example:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

etc...

user902691
  • 1,149
  • 4
  • 20
  • 46
Evan
  • 749
  • 6
  • 11