0

I am new to android i want to connect the WiFi by giving the username and password in program itself.i am using the below code.. it only enable the default network..how can i authenticate and get connected by using the code.

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toggleWiFi(true);
        Toast.makeText(getApplicationContext(), "Wi-Fi Enabled!", Toast.LENGTH_LONG).show();
    }

    public void toggleWiFi(boolean status) {
        WifiManager wifiManager = (WifiManager) this
                .getSystemService(Context.WIFI_SERVICE);
        if (status == true && !wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(true);
        } else if (status == false && wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(false);
        }
    }

}
harini
  • 73
  • 1
  • 1
  • 7

1 Answers1

0

Three steps to do, but first have a look at the official doc of wifi Manager: http://developer.android.com/reference/android/net/wifi/WifiManager.html

  1. Scan the network:

WifiManager wifi= (WifiManager) getSystemService(Context.WIFI_SERVICE);

wifi.startScan();
// get list of the results in object format ( like an array )
List<ScanResult> results = wifi.getScanResults();`

// loop that goes through list
for (ScanResult result : results) {
  Toast.makeText(this, result.SSID + " " + result.level,
  Toast.LENGTH_SHORT).show();
}

Thier are are some links that helped to understand the scan well, How can I get Android Wifi Scan Results into a list?

Note: There are three types of WIFI networks, ScanResult gives the rssi (level)that can be used to select the appropriate network:http://developer.android.com/reference/android/net/wifi/ScanResult.html

  1. You need permsissions in your manifest.xml:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
  1. //First, instantiate a WifiConfiguration object and fill in the network’s SSID
    
    WifiConfiguration wfc = new WifiConfiguration();
    
    wfc.SSID = "\"".concat(ssid).concat("\"");
    wfc.status = WifiConfiguration.Status.DISABLED;
    wfc.priority = 40;
    
    //For Open Networks: we need to fill members of WifiConfiguration to specify the network’s security mode.
    
    wfc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
    wfc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
    wfc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
    wfc.allowedAuthAlgorithms.clear();
    wfc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
    wfc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
    wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
    wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
    wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
    wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
    
    //For WEP
    
    wfc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
    wfc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
    wfc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
    wfc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
    wfc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
    wfc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
    wfc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
    wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
    wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
    
    if (isHexString(password)) wfc.wepKeys[0] = password;
    else wfc.wepKeys[0] = "\"".concat(password).concat("\"");
    wfc.wepTxKeyIndex = 0;
    
    //For WPA, we can set the same values for either
    
    wfc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
    wfc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
    wfc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
    wfc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
    wfc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
    wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
    wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
    wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
    wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
    
    wfc.preSharedKey = "\"".concat(password).concat("\"");
    
    //At last, 
    
    WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE); 
    wifiManager.addNetwork(conf);
    
    if (networkId != -1) {
     // add some logs for failure
    } 
    else
    {
      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;
        }           
     }
    }
Community
  • 1
  • 1
user3278897
  • 984
  • 10
  • 23