2

I need to make my app connect automatically to specific SSID with password. I'm trying this but this is what I get:

error:

non-static method addNetwork(WifiConfiguration) cannot be referenced from a static context

error:

non-static method enableNetwork(int,boolean) cannot be referenced from a static context

final WifiManager wifiManager = (WifiManager) MainActivity.this.getSystemService(Context.WIFI_SERVICE);
final WifiConfiguration config = new WifiConfiguration();
config.SSID = "\"xxx\"";
config.preSharedKey = "\"123\"";
if (!wifiManager.isWifiEnabled()){
    wifiManager.setWifiEnabled(true);
    int networkId = WifiManager.addNetwork(config);
    WifiManager.enableNetwork(networkId, true);
}
JoshDM
  • 4,939
  • 7
  • 43
  • 72
Haroun
  • 21
  • 1
  • 2
  • 8

2 Answers2

6

Call enableNetwork() function on your wifiManager object, not WifiManager class.

Be careful: the w letter should be lowercase.

Do the same for addNetwork().

EDIT:

In your manifest add these permissions:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

EDIT 2:

For WPA, update your config like this:

config.status = WifiConfiguration.Status.ENABLED;

config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);

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

EDIT3:

Add this line below wifiManager.setWifiEnabled(true) line:

wifiManager.startScan();

EDIT 4:

If you need additional help, read this question and this article. Good Luck.

Community
  • 1
  • 1
Behnam
  • 6,510
  • 6
  • 35
  • 65
  • thanks that's true and im fix it, but that's not work ! do you know function that require the SSID and Password to connect to this access point ? thx again. – Haroun Jul 23 '14 at 12:40
  • @Haroun: Is your Wifi WEP, WPA, or Open? – Behnam Jul 23 '14 at 12:44
  • and this is what im use in manifest : android:name="android.permission.INTERNET" android:name="android.permission.ACCESS_WIFI_STATE" android:name="android.permission.CHANGE_WIFI_STATE" all what i get is turning on wif !! – Haroun Jul 23 '14 at 12:51
  • thank you very much, the question is very helpful. may i ask you about how can i make the user have choise any time to change home button ? thx :) – Haroun Jul 23 '14 at 14:34
1

The fact is you are trying to a static method/variable or resource from a non-static source, that is why this error.

Remove the static declaration from the method/class from which this content is being accessed:

public wifiAccess(){
final WifiManager wifiManager = (WifiManager) MainActivity.this.getSystemService(Context.WIFI_SERVICE);
final WifiConfiguration config = new WifiConfiguration();
config.SSID = "\"xxx\"";
config.preSharedKey = "\"123\"";
if (!wifiManager.isWifiEnabled()){
    wifiManager.setWifiEnabled(true);
    int networkId = WifiManager.addNetwork(config);
    WifiManager.enableNetwork(networkId, true);
}
 }

Further Reference: "Non-static method cannot be referenced from a static context" error

Community
  • 1
  • 1
Kailas
  • 7,350
  • 3
  • 47
  • 63