I'm using following code to scan and connect my desired network. It works fine if my Network is already connected, in that case it disconnects and reconnects while i click my scan button. But when no network is connected and wifi is ON , then it doesn't connect. What could be the possible issue. Any suggestion would be highly appreciated.
WifiBroadCastReceiver:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.SupplicantState;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.util.Log;
import android.widget.Toast;
import java.util.List;
public class WifiBroadReceiver extends BroadcastReceiver {
final static String networkSSID = "myDevice";
final static String networkPass = "password";
Context context;
@Override
public void onReceive(Context context, Intent intent) {
this.context = context;
String action = intent.getAction();
if (WifiManager.SUPPLICANT_STATE_CHANGED_ACTION.equals(action)) {
SupplicantState state = intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE);
if (SupplicantState.isValidState(state)
&& (
state == SupplicantState.COMPLETED
|| state == SupplicantState.DISCONNECTED
|| state == SupplicantState.SCANNING
)) {
boolean connected = checkConnectedToDesiredWifi();
if (!connected) {
try {
addMyNetwork(context);
} catch (Exception e) {
}
}
}
}
}
/**
* Detect you are connected to a specific network.
*/
private boolean checkConnectedToDesiredWifi() {
boolean connected = false;
String desiredMacAddress = networkSSID;
WifiManager wifiManager =
(WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo wifi = wifiManager.getConnectionInfo();
if (wifi != null) {
// get current router Mac address
String bssid = wifi.getBSSID();
connected = desiredMacAddress.equals(bssid);
}
return connected;
}
private void addMyNetwork(Context context) {
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + networkSSID + "\""; // Please note the quotes. String should contain ssid in quotes
//Then, for WEP network you need to do this
conf.wepKeys[0] = "\"" + networkPass + "\"";
conf.wepTxKeyIndex = 0;
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
//For WPA network you need to add passphrase like this:
conf.preSharedKey = "\"" + networkPass + "\"";
//For Open network you need to do this:
//conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
// Then, you need to add it to Android wifi manager settings:
WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
//--------
wifiManager.setWifiEnabled(true);
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;
}
}
}
}
onButtonClick:
public void onWifiClick(View view) {
BroadcastReceiver broadcastReceiver = new WifiBroadcastReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
registerReceiver(broadcastReceiver, intentFilter);
}