Connect to the Wi-Fi network in android, return if the password is incorrect

I would like to make an application that allows the user to connect to the Wi-Fi network, however, I am having problems connecting to the network.

My current code is:

    WifiManager wifi = (WifiManager) getSystemService(WIFI_SERVICE);
    wifi.setWifiEnabled(true);
    WifiConfiguration wc = new WifiConfiguration();
    wifi.startScan();
    List<ScanResult> l=wifi.getScanResults();
    wc.SSID = l.get(NUMBER).SSID;
    post(wc.SSID);
    /*This is the bit that I think is failing, my network does not have these properties.. but I can't see how to get them from the Scan Result*/
    wc.preSharedKey  = "\"passw0rd123\"";
    wc.hiddenSSID = false;
    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);
    int res = wifi.addNetwork(wc);
    post("add Network returned " + res);
    boolean b = wifi.enableNetwork(res, true);        
    post("enableNetwork returned " + b);

I think this is due to settings (after my comment) that do not match my network settings, but I don’t know how to get these settings from ScanResult ..

EDIT: I would also like to know if it is connected correctly.

+5
source share
2 answers

make sure you set the correct permissions in the AndroidManifest.xml file

-1
source

, WIFI ur ,

WIFI onclick , Wifi- settings, ​​

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

           if ((wifi.isWifiEnabled() == true)) {

      Toast.makeText(RegisterActivity.this,"MOBILE Is Connected TO WI-FI!",
              }

              else {

                AlertDialog.Builder WIFIOFF = new Builder(MyTestglobe.this);
                WIFIOFF.setCancelable(false);
                WIFIOFF.setTitle("Connection Error");
                WIFIOFF.setMessage(" Please Enable Your WIFI/INTERNET !");
                WIFIOFF.setPositiveButton("Ok",
                new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int which) {
             startActivity(new Intent( Settings.ACTION_WIFI_SETTINGS));

                            }
                        });
                WIFIOFF.show();

            }

ur Manifest.xml

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

All Articles