Android 5.0 Lollipop and 4.4 KitKat ignores my WiFi network, enableNetwork() is useless

前端 未结 3 1340
情歌与酒
情歌与酒 2020-12-24 12:49

My app connect directly to a hardware device that act as an access point (with no access to internet).

I can\'t connect because Android 5.0 automat

3条回答
  •  伪装坚强ぢ
    2020-12-24 13:40

    Just need to correctly format the SSID. Here is a sample code:

        WifiManager mWifiManager = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE);
        WifiConfiguration tmpConfig = new WifiConfiguration();
    
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            tmpConfig.SSID = String.format("\"%s\"", sSSID);
            tmpConfig.BSSID = sBSSID;
        } else {
            tmpConfig.SSID = "\"" + sSSID + "\"";
            tmpConfig.BSSID = "\"" + sBSSID + "\"";
        }
    
        tmpConfig.priority = 1;
        if (iSecurityType.equals("WEP")) {
            tmpConfig.wepKeys[0] = sPassword;
            tmpConfig.wepTxKeyIndex = 0;
        } else if (iSecurityType.equals("WPA2") ||   iSecurityType.equals("WPA")) {
                tmpConfig.preSharedKey = "\"" + sPassword+ "\"";
            } else {
                tmpConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
            }
    
        tmpConfig.status = WifiConfiguration.Status.ENABLED;
        int netId = mWifiManager.addNetwork(tmpConfig);
    
        mWifiManager.updateNetwork(tmpConfig);
        boolean result = mWifiManager.enableNetwork(netId, true);
        mWifiManager.updateNetwork(tmpConfig);
        mWifiManager.saveConfiguration();
    

提交回复
热议问题