0

We are working in an application where we connect to several wifi AP.

The only device where we located the problem is a Huawei device, concretely Huawei P20 PRO (Android 9.1).

We are able to connect to wifi AP not protected with password, but if it is protected, we are not able to establish the connection with the Wifi AP.

Here is a extract of the code we are using:

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;
    }

Anyone suffered a similar problems?

  • Have you tried with other device having the same version? – Ankita Nov 14 '19 at 10:30
  • Has your Wifi AP internet access ? If not, you have to bind the connectivity manager to the network when available – matdev Nov 14 '19 at 10:38
  • First of all thanks for the answers. I've tried the code with several devices, only huawei device failed. The AP is a camera without an internet connection. The camera provides images through a udp connection – Daniel García Nov 14 '19 at 14:07

0 Answers0