简体   繁体   中英

Java in Android, Help on connecting to a wifi network, and trying to reconnect if unsuccessful

I'm implementing a method that if unusuccessful in connecting to a wifi network, it tries to reconnect by using an alert dialog, here is the code I have so far

public boolean autoConnect() {
      String networkSSID = "xxxxx";
      boolean connected = false;
      WifiConfiguration conf = new WifiConfiguration();

      conf.SSID = "\"" + networkSSID + "\"";
      conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
      WifiManager wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE); 
      wifiManager.addNetwork(conf);
      wifiManager.startScan();
      List<ScanResult> list = wifiManager.getScanResults();
      AlertDialog alertDialog = new AlertDialog.Builder(CarNannyv3Activity.this).create();
      alertDialog.setTitle("xxxxxxxx Not Available");  
      alertDialog.setMessage("Please make sure xxxxxx is turned on ");  
      alertDialog.setButton("Reconnect", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                autoConnect();
                dialog.dismiss();


            } });
      for( ScanResult i : list ) {
          if(i.SSID.equals("\"" + networkSSID + "\"")) {

               //wifiManager.enableNetwork(i.networkId, true);
               wifiManager.reconnect();               
               return connected = true;


          }else{

                alertDialog.show();
          }
       } 
      return connected;
  }

I get one of two things, it either endless loops the reconnect dialog, or it never shows at all, please shed me some light on what I'm doing wrong.

Try to use this, its working in my case ::

        WifiManager notif_manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        List<ScanResult> scan_list = notif_manager.getScanResults();
        for (ScanResult scan_ap : scan_list) 
        {
            if (scan_ap.SSID.equals("\"" + networkSSID + "\"")) 
            {

            /* Create a WifiConfig */
                WifiConfiguration eliteAp = new WifiConfiguration();

                /* AP Name */
                eliteAp.SSID = "\"" + ssidString + "\"";

                /* Priority */
                eliteAp.priority = 40;

                /* Enable Hidden SSID */
                eliteAp.hiddenSSID = false;
                .
                .
                .
                .
                .
                .       
                eliteAp.status = WifiConfiguration.Status.ENABLED;
                int res = wifi_manager.addNetwork(eliteAp);
                boolean d = wifi_manager.enableNetwork(res, true);
                break;

            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM