简体   繁体   English

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

[英]Connecting to a wifi network with Android 9 (API level 28)

I've been working on a react-native application recently, and I'd like to connect the devices to a wifi network through the app. 我最近一直在研究一个react-native应用程序,我想通过应用程序将设备连接到一个wifi网络。 I used the react-native-wifi npm lib to achieve this but it seems i have some problems connection with my android 9 phone. 我使用react-native-wifi npm lib来实现这一点,但似乎我与android 9手机有一些问题。 Although the lib is working with an other phone which is running android 7.1 (Nougat). 虽然lib正在使用另一部运行android 7.1(Nougat)的手机

The ACCESS_FINE_LOCATION permission is granted in runtime by the user, also ACCESS_WIFI_STATE and CHANGE_WIFI_STATE are granted in the manifest. 用户在运行时授予ACCESS_FINE_LOCATION权限,并在清单中授予ACCESS_WIFI_STATECHANGE_WIFI_STATE

So the native module that is used is: 所以使用的本机模块是:

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

(returning false means the connection didnt succeed) (返回false表示连接未成功)

I was debugging it and it seems that wifi.updateNetwork(conf) and wifi.addNetwork(conf) always return -1 no matter what (so far). 我正在调试它,似乎wifi.updateNetwork(conf)wifi.addNetwork(conf)总是返回-1,无论如何(到目前为止)。 By the android docs these methods should still work with the API level 28. 通过android docs,这些方法仍然适用于API级别28。

I'm trying to connect to a WPA2 network. 我正在尝试连接到WPA2网络。 Also i tried to remove the network from the wifi.getConfiguredNetworks() list but didn't change anything. 此外,我试图从wifi.getConfiguredNetworks()列表中删除网络,但没有改变任何东西。

I don't know what i'm missing, but i'd be really happy if somebody could help me out with this one. 我不知道我错过了什么,但是如果有人可以帮我解决这个问题,我会非常高兴。

Thanks! 谢谢!

After all i managed to find the solution which is really simple, just had to quote the ssid and it works. 毕竟我设法找到了非常简单的解决方案,只需引用ssid即可。 Tested on API level 27 and 28. 在API级别27和28上测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Android (API 30) 中以编程方式连接到特定的 wifi 网络 - Connecting to a specific wifi network programmatically in Android (API 30) 在Android 5和6中自动连接到WiFi网络 - Connecting to WiFi network automatically in Android 5 and 6 在 Android API 级别 28 及更高级别连接时,如何获取 Wifi AP 详细信息,如 SSID、Band 等? - How to get Wifi AP details like SSID, Band, etc when connected in Android API level 28 and above? 是否有一个 Flutter 库可以扫描 WIFI 网络并连接/断开这些网络,适用于 Android API 级别 28-30 - Is there a Flutter library that can scan for WIFI networks and connect/disconnect to/from these networks, that works for Android API level 28-30 如何降级 Api 级别 (32 -&gt; 28) Android? - How to downgrade Api Level (32 -> 28) Android? 在 Android 9 上配置 APN(API 级别 28) - Configure APNs on Android 9 (API level 28) 带有 API 级别 28 的 Android-ProviderTestCase2 不起作用 - Android-ProviderTestCase2 with API Level 28 not working 离子 Android 挂起,至少 API 级别 28 - Ionic Android hangs with least API level 28 Android textFontWeight api 等级小于28 - Android textFontWeight api level less than 28 Android 自定义 ListView 未显示在 API 级别 28 - Android customized ListView not showing in API Level 28
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM