简体   繁体   English

Android中的Java,有关连接到wifi网络的帮助,如果未成功,请尝试重新连接

[英]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 我正在实现一种方法,如果无法成功连接到wifi网络,它将尝试使用警报对话框重新连接,这是我到目前为止的代码

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;

            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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