使用Android 9(API级别28)连接到wifi网络

时间:2019-06-24 11:15:39

标签: android react-native react-native-android

我最近一直在开发本机应用程序,我想通过该应用程序将设备连接到wifi网络。我使用react-native-wifi npm lib来实现这一点,但是似乎我的android 9手机存在一些问题。尽管该lib可以与运行 android 7.1(牛轧糖)的另一部手机一起使用。

ACCESS_FINE_LOCATION 权限由用户在运行时中授予, ACCESS_WIFI_STATE CHANGE_WIFI_STATE 则由用户授予清单。

所以使用的本机模块是:

public Boolean connectTo(ScanResult result, String password, String ssid, Promise promise) {
        //Make new configuration
        WifiConfiguration conf = new WifiConfiguration();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            conf.SSID = ssid;
        } else {
            conf.SSID = "\"" + ssid + "\"";
        }

        String capabilities = result.capabilities;

        if (capabilities.contains("WPA") ||
                capabilities.contains("WPA2") ||
                capabilities.contains("WPA/WPA2 PSK")) {

            // appropriate ciper is need to set according to security type used,
            // ifcase of not added it will not be able to connect
            conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
            conf.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
            conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
            conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
            conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
            conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
            conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
            conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
            conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
            conf.status = WifiConfiguration.Status.ENABLED;
            conf.preSharedKey = "\"" + password + "\"";

        } else if (capabilities.contains("WEP")) {
            conf.wepKeys[0] = "\"" + password + "\"";
            conf.wepTxKeyIndex = 0;
            conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
            conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);

        } else {
            conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        }

        //Remove the existing configuration for this netwrok
        List<WifiConfiguration> mWifiConfigList = wifi.getConfiguredNetworks();

        int updateNetwork = -1;

        for (WifiConfiguration wifiConfig : mWifiConfigList) {
            if (wifiConfig.SSID.equals(conf.SSID)) {
                conf.networkId = wifiConfig.networkId;
                updateNetwork = wifi.updateNetwork(conf);
            }
        }

        // If network not already in configured networks add new network
        if (updateNetwork == -1) {
            updateNetwork = wifi.addNetwork(conf);
            wifi.saveConfiguration();
        }

        if (updateNetwork == -1) {
            return false;
        }

        boolean disconnect = wifi.disconnect();
        if (!disconnect) {
            return false;
        }

        boolean enableNetwork = wifi.enableNetwork(updateNetwork, true);
        if (!enableNetwork) {
            return false;
        }

        return true;
    }

(返回false表示连接未成功)

我正在调试它,看来 wifi.updateNetwork(conf) wifi.addNetwork(conf) 始终返回 -1 (至今)。到android docs为止,这些方法仍应适用于API级别28。

我正在尝试连接到WPA2网络。我也尝试从 wifi.getConfiguredNetworks() 列表中删除网络,但没有进行任何更改。

我不知道我在想什么,但是如果有人可以帮助我解决这个问题,我将非常高兴。

谢谢!

1 个答案:

答案 0 :(得分:0)

毕竟,我设法找到了非常简单的解决方案,只需要引用ssid即可。在API级别27和28上进行了测试。