以编程方式连接到wifi不适用于新API

时间:2018-09-04 09:10:20

标签: android wifimanager wificonfiguration

我想以编程方式连接Wifi。我在旧设备(4年前购买)上做了一些测试,它运行良好。但是,当我在模拟器(Android 8.1.0,API 27)上进行了一些测试时,该测试无法正常工作。你有解释吗? (实际上,我可以看到我的wifi已保存在设置的已保存网络中,但设备未连接到wifi) 这是我的代码(基本上是我从堆栈溢出问题How do I connect to a specific Wi-Fi network in Android programmatically?的答案中复制来的):

findViewById(R.id.connect_to_wifi).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Thread connectionThread = new Thread(new Runnable() {
                @Override
                public void run() {
                    wifiManager.setWifiEnabled(true);
                    String ssid = ((TextView)findViewById(R.id.wifi_name)).getText().toString();
                    String key = ((TextView)findViewById(R.id.wifi_password)).getText().toString();
                    WifiConfiguration conf = new WifiConfiguration();
                    conf.SSID = String.format("\"%s\"", ssid);
                    conf.status = WifiConfiguration.Status.ENABLED;
                    conf.priority = 40;
                    //wifiConfig.preSharedKey = String.format("\"%s\"", key);
                    if (((EditText)findViewById(R.id.encryption)).getText().toString().equals("WEP")) {
                        Log.v("rht", "Configuring WEP");
                        conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
                        conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
                        conf.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
                        conf.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
                        conf.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
                        conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
                        conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
                        conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
                        conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);

                        if (key.matches("^[0-9a-fA-F]+$")) {
                            conf.wepKeys[0] = key;
                        } else {
                            conf.wepKeys[0] = "\"".concat(key).concat("\"");
                        }

                        conf.wepTxKeyIndex = 0;

                    } else if (((EditText)findViewById(R.id.encryption)).getText().toString().contains("WPA")) {
                        Log.v("rht", "Configuring WPA");

                        conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
                        conf.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
                        conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
                        conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
                        conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
                        conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
                        conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
                        conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
                        conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);

                        conf.preSharedKey = "\"" + key + "\"";

                    } else {
                        Log.v("rht", "Configuring OPEN network");
                        conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
                        conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
                        conf.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
                        conf.allowedAuthAlgorithms.clear();
                        conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
                        conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
                        conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
                        conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
                        conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
                        conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
                    }

                    int netId = wifiManager.addNetwork(conf);
                    wifiManager.disconnect();
                    wifiManager.enableNetwork(netId, true);
                    wifiManager.reconnect();
                }
            });
            connectionThread.start();
        }
    });

这是日志:

09-04 09:16:34.348 7410-8091/amiin.bazouk.application.com.localisationdemo V/rht: Configuring WPA
09-04 09:16:34.352 1725-2739/system_process I/WifiService: addOrUpdateNetwork uid=10079
09-04 09:16:34.352 1725-2739/system_process I/addOrUpdateNetwork:  uid = 10079 SSID "partner" nid=-1
09-04 09:16:34.357 1725-2497/system_process I/WifiService: getConfiguredNetworks uid=10014
09-04 09:16:34.377 1725-2041/system_process D/WifiConfigStore: Writing to stores completed in 21 ms.
09-04 09:16:34.381 1725-2497/system_process I/WifiService: disconnect uid=10079
09-04 09:16:34.383 7831-7831/? E/wpa_supplicant: nl80211: Failed to open /proc/sys/net/ipv4/conf/wlan0/drop_unicast_in_l2_multicast: No such file or directory
    nl80211: Failed to set IPv4 unicast in multicast filter
09-04 09:16:34.384 1725-2497/system_process I/WifiService: enableNetwork uid=10079 disableOthers=true
09-04 09:16:34.391 1397-3075/? W/audio_hw_generic: Hardware backing HAL too slow, could only write 0 of 720 frames
09-04 09:16:34.393 1725-2041/system_process D/WifiStateMachine: connectToUserSelectNetwork netId 8, uid 10079, forceReconnect = false
09-04 09:16:34.396 1725-2739/system_process I/WifiService: getConfiguredNetworks uid=10014
09-04 09:16:34.405 1397-3075/? W/audio_hw_generic: Hardware backing HAL too slow, could only write 0 of 720 frames
09-04 09:16:34.421 1397-3075/? W/audio_hw_generic: Hardware backing HAL too slow, could only write 0 of 720 frames
09-04 09:16:34.439 1725-2041/system_process D/WifiConfigStore: Writing to stores completed in 27 ms.
09-04 09:16:34.439 1725-2041/system_process E/WifiConfigManager: UID 10079 does not have permission to update configuration "partner"WPA_PSK
09-04 09:16:34.439 1725-2041/system_process I/WifiStateMachine: connectToUserSelectNetwork Allowing uid 10079 with insufficient permissions to connect=8
09-04 09:16:34.440 1725-2041/system_process D/WifiStateMachine: CMD_START_CONNECT sup state DisconnectedState my state DisconnectedState nid=8 roam=false
09-04 09:16:34.443 1725-2497/system_process I/WifiService: reconnect uid=10079
09-04 09:16:34.446 1562-1562/? E/wificond: Received error messsage: Operation not supported on transport endpoint
    NL80211_CMD_ABORT_SCAN failed
09-04 09:16:34.446 1562-1562/? W/wificond: Abort scan failed
09-04 09:16:34.447 1725-2041/system_process D/SupplicantStaIfaceHal: connectToNetwork "partner"WPA_PSK
    Network is already saved, will not trigger remove and add operation.
09-04 09:16:34.699 1725-2497/system_process D/WificondControl: Scan result ready event
09-04 09:16:34.715 1725-2041/system_process W/WifiConfigManager: Looking up network with invalid networkId -1

2 个答案:

答案 0 :(得分:0)

  • 设置conf.BSSID
  • conf.SSIDconf.preSharedKey之前和之后加上双引号(“),如果缺少。

答案 1 :(得分:0)

Android 8.1+不会直接连接到wifi,首先在运行时为COURSE_LOCATION添加权限,然后它将连接