[英]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_STATE和CHANGE_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.