Longbiao CHEN
Longbiao CHEN

Reputation: 1053

Android connect to WiFi without human interaction

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 encrypted, 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?

Upvotes: 8

Views: 18273

Answers (4)

Evan
Evan

Reputation: 749

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...

Upvotes: 1

Wouter
Wouter

Reputation: 2653

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.

Upvotes: 3

Longbiao CHEN
Longbiao CHEN

Reputation: 1053

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.

Upvotes: 15

Nicholas
Nicholas

Reputation: 7521

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.

Upvotes: 2

Related Questions