使用华为以编程方式连接到wifi AP

时间:2019-11-14 10:15:35

标签: android connection wifi huawei

我们正在开发一个可以连接到多个wifi AP的应用程序。

我们找到问题所在的唯一设备是华为设备,具体地说是华为P20 PRO(Android 9.1)。

我们能够连接不受密码保护的wifi AP,但是如果受保护,我们将无法与Wifi AP建立连接。

以下是我们正在使用的代码的一部分:

private void connect(WifiConfiguration wifiAP) {

        WifiManager wifi = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        SharedPreferences prefs =
                context.getSharedPreferences(prefs_pass_dic, Context.MODE_PRIVATE);
        String pass = prefs.getString(wifiAP.SSID.replace("\"",""), "");
        WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        wifiManager.disconnect();

        wifiAP = createAPConfiguration(wifiAP.SSID.replace("\"", ""), pass.trim(), getSecurityType(wifiAP));

        int res = wifi.addNetwork(wifiAP);
        if (res == -1) {
            for (WifiConfiguration cc : wifiManager.getConfiguredNetworks()) {
                if (cc.SSID.equals(wifiAP.SSID))
                    res = cc.networkId;
            }
        }

        boolean b = wifi.enableNetwork(res, true);
        Log.d(TAG, "# enableNetwork returned " + b);
        wifi.reconnect();

        wifi.saveConfiguration();
}

private WifiConfiguration createAPConfiguration(String networkSSID, String networkPasskey, String securityMode) {
        WifiConfiguration wifiConfiguration = new WifiConfiguration();

        wifiConfiguration.SSID = "\"" + networkSSID + "\"";
        wifiConfiguration.priority = getMaxPriority(context) + 1;
        if (securityMode.equalsIgnoreCase("OPEN")) {

            wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);

        } else if (securityMode.equalsIgnoreCase("WEP")) {

            wifiConfiguration.wepKeys[0] = "\"" + networkPasskey + "\"";
            wifiConfiguration.wepTxKeyIndex = 0;
            wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
            wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);

        } else if (securityMode.equalsIgnoreCase("PSK")) {

            wifiConfiguration.preSharedKey = "\"" + networkPasskey + "\"";
            wifiConfiguration.hiddenSSID = true;
            wifiConfiguration.status = WifiConfiguration.Status.ENABLED;
            wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
            wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
            wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
            wifiConfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
            wifiConfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
            wifiConfiguration.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
            wifiConfiguration.allowedProtocols.set(WifiConfiguration.Protocol.WPA);

        } else {
            Log.i("wifi", "# Unsupported security mode: " + securityMode);

            return null;
        }

        return wifiConfiguration;
    }

有人遇到过类似的问题吗?

0 个答案:

没有答案