Android Wifi Manager网络切换无法正常工作

时间:2014-10-15 21:33:01

标签: android wifimanager network-connection

在我的应用程序中,我必须显示访问点列表,然后连接到选定的网络。当应用程序启动并选择网络时,连接将通过,所选网络也会反映在手机设置 - > wifi中。但是当我尝试在发布后切换网络时会发生以下情况

  1. 所选网络已连接
  2. 然后断开连接
  3. wifi扫描,然后连接到先前连接的网络
  4. 保存网络和新网络的行为相同。如何让所选网络的错误断开连接。我正在检查Open和WPA2网络类型。

    我的代码

        public WifiConfiguration exists(String SSID)
    {
        List<WifiConfiguration> existingConfigs = wifiManager.getConfiguredNetworks();
        for (WifiConfiguration existingConfig : existingConfigs) 
        {
    
            if (existingConfig.SSID.equals("\""+SSID+"\""))
            {
                return existingConfig;
            }
        }
        return null; 
    }
    
    private WifiConfiguration CreateWifiInfo(String SSID, String Password, WifiCipherType Type)
    {
        WifiConfiguration config = new WifiConfiguration();  
    
        config.SSID = "\"" + SSID + "\"";
    
        if(Type == WifiCipherType.WIFICIPHER_NOPASS)
        {
    
            config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
    
        }
        else if(Type == WifiCipherType.WIFICIPHER_WPA2)
        {
            config.preSharedKey = "\""+Password+"\"";
            config.hiddenSSID = true;                    
        }
        else
        {
            return null;
        }
        return config;
    }
    public boolean Connect(String SSID, String Password, WifiCipherType Type)
    
        WifiConfiguration tempConfig = this.exists(SSID);
    
        int netID = -1;
        if(tempConfig == null)
        {
            Log.i(MainActivity.TAG, "Network is new, so adding new one.");
            WifiConfiguration wifiConfig = this.CreateWifiInfo(SSID, Password, Type);
            netID = wifiManager.addNetwork(wifiConfig);
            Log.i(MainActivity.TAG, "New net ID : " + Integer.toString(netID));
        } else {
            netID = tempConfig.networkId;
            Log.i(MainActivity.TAG, "Found net ID: " + Integer.toString(netID));
        }
    
        boolean bRet = wifiManager.enableNetwork(netID, true);  
    

    bRet此处为所选网络返回true。

    任何建议

    由于 P

0 个答案:

没有答案