Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 与华为通过编程方式连接到wifi AP_Android_Connection_Wifi_Huawei Mobile Services - Fatal编程技术网

Android 与华为通过编程方式连接到wifi AP

Android 与华为通过编程方式连接到wifi AP,android,connection,wifi,huawei-mobile-services,Android,Connection,Wifi,Huawei Mobile Services,我们正在一个应用程序中工作,在该应用程序中,我们连接到多个wifi AP 我们发现问题的唯一设备是华为设备,具体来说是华为P20 PRO(Android 9.1) 我们可以连接到不受密码保护的wifi AP,但如果它受到保护,我们将无法与wifi AP建立连接 以下是我们正在使用的代码摘录: private void connect(WifiConfiguration wifiAP) { WifiManager wifi = (WifiManager) context.getA

我们正在一个应用程序中工作,在该应用程序中,我们连接到多个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;
    }

有人遇到过类似问题吗?

您是否尝试过使用其他具有相同版本的设备?您的Wifi AP是否可以访问互联网?如果没有,您必须在可用时将connectivity manager绑定到网络首先感谢您的回答。我已经在多台设备上试用了该代码,但只有华为设备失败。AP是一个没有互联网连接的摄像头。摄像机通过udp连接提供图像