打开 Wifi 并连接到 ios 上受保护的 SSID,并做出本机反应
Turn on Wifi and connect to a protected SSID on ios with react native
我正在使用 React Native 开发 IOT 项目,其中 Phone 必须连接到受保护的 WiFi(无互联网连接)。我应该以编程方式打开 WiFi 并连接到受保护的 ssid。因此,我使用了以下库:https://github.com/JuanSeBestia/react-native-wifi-reborn。
在 android,我可以完全控制设备,而且一切都很顺利。另一方面,在 ios,我无法打开 wifi,即使在该用户打开 wifi 之后,库中定义的函数(如 connectToProtectedSSID)似乎也不起作用。
如有任何建议,我们将不胜感激。
P.s。我完全遵循了库中的文档,包括两个平台中的位置访问。
没有publicAPI来控制Wifi on iOS。
方法setEnabled仅适用于android。
最后,两个平台的解决方案:
iOS:
使用以下方法应用程序将打开 WIFI 并连接到特定的 SSID(至少 iOS 13):
您应该将这些功能添加到您的应用程序中:
访问 WiFi 信息
热点配置(自述文件中遗漏了!)
(iOS 13) "隐私 - 使用时的位置
说明”或“隐私 - 位置始终和使用时
“设置 -> 信息中的描述”
import WifiManager from "react-native-wifi-reborn";
ConnectToNetwork = async () => {
WifiManager.connectToProtectedSSID("YourSSIDName", "YourPassword", false)
.then(
() => {
//console.log("connectToProtectedSSID successfully!");
},
(reason) => {
//console.log("connectToProtectedSSID failed!");
//console.log(reason);
}
);
}
Android:
别忘了打开位置!
正在断开与当前 SSID 的连接并连接到所需的 SSID:
import RNAndroidLocationEnabler from 'react-native-android-location-enabler';
import WifiManager from "react-native-wifi-reborn";
ConnectToNetwork() {
WifiManager.setEnabled(true);
WifiManager.disconnect();
//WifiManager.forceWifiUsage(true);
PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{
title: '',
message:'',
buttonNegative: '',
buttonPositive: '',
},
).then((granted) => {
//console.log(granted);
if (granted === PermissionsAndroid.RESULTS.GRANTED)
{
//console.log("granted");
RNAndroidLocationEnabler.promptForEnableLocationIfNeeded({interval: 10000, fastInterval: 5000})
.then(data => {
WifiManager.connectToProtectedSSID("YourSSIDName", "YourPassword", false)
.then(
() => {
//console.log("connectToProtectedSSID successfully!");
},
(reason) => {
//console.log("connectToProtectedSSID failed!");
//console.log(reason);
}
);
//console.log("location enabled");
//WifiManager.connectToProtectedSSID("", "", false)
WifiManager.getCurrentWifiSSID().then(
ssid => {
if(ssid =="YourSSIDName"){
}
else {
}
//console.log("Your current connected wifi SSID is " + ssid);
},
() => {
//console.log("Cannot get current SSID!");
}
);
}).catch(err => {
//console.log("not permitted to enable location");
});
}
else
{
//console.log("not granted");
// Permission denied
}
// expected output: "Success!"
});
}
我正在使用 React Native 开发 IOT 项目,其中 Phone 必须连接到受保护的 WiFi(无互联网连接)。我应该以编程方式打开 WiFi 并连接到受保护的 ssid。因此,我使用了以下库:https://github.com/JuanSeBestia/react-native-wifi-reborn。 在 android,我可以完全控制设备,而且一切都很顺利。另一方面,在 ios,我无法打开 wifi,即使在该用户打开 wifi 之后,库中定义的函数(如 connectToProtectedSSID)似乎也不起作用。 如有任何建议,我们将不胜感激。
P.s。我完全遵循了库中的文档,包括两个平台中的位置访问。
没有publicAPI来控制Wifi on iOS。
方法setEnabled仅适用于android。
最后,两个平台的解决方案:
iOS:
使用以下方法应用程序将打开 WIFI 并连接到特定的 SSID(至少 iOS 13):
您应该将这些功能添加到您的应用程序中:
访问 WiFi 信息
热点配置(自述文件中遗漏了!)
(iOS 13) "隐私 - 使用时的位置 说明”或“隐私 - 位置始终和使用时 “设置 -> 信息中的描述”
import WifiManager from "react-native-wifi-reborn"; ConnectToNetwork = async () => { WifiManager.connectToProtectedSSID("YourSSIDName", "YourPassword", false) .then( () => { //console.log("connectToProtectedSSID successfully!"); }, (reason) => { //console.log("connectToProtectedSSID failed!"); //console.log(reason); } ); }
Android:
别忘了打开位置!
正在断开与当前 SSID 的连接并连接到所需的 SSID:
import RNAndroidLocationEnabler from 'react-native-android-location-enabler';
import WifiManager from "react-native-wifi-reborn";
ConnectToNetwork() {
WifiManager.setEnabled(true);
WifiManager.disconnect();
//WifiManager.forceWifiUsage(true);
PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{
title: '',
message:'',
buttonNegative: '',
buttonPositive: '',
},
).then((granted) => {
//console.log(granted);
if (granted === PermissionsAndroid.RESULTS.GRANTED)
{
//console.log("granted");
RNAndroidLocationEnabler.promptForEnableLocationIfNeeded({interval: 10000, fastInterval: 5000})
.then(data => {
WifiManager.connectToProtectedSSID("YourSSIDName", "YourPassword", false)
.then(
() => {
//console.log("connectToProtectedSSID successfully!");
},
(reason) => {
//console.log("connectToProtectedSSID failed!");
//console.log(reason);
}
);
//console.log("location enabled");
//WifiManager.connectToProtectedSSID("", "", false)
WifiManager.getCurrentWifiSSID().then(
ssid => {
if(ssid =="YourSSIDName"){
}
else {
}
//console.log("Your current connected wifi SSID is " + ssid);
},
() => {
//console.log("Cannot get current SSID!");
}
);
}).catch(err => {
//console.log("not permitted to enable location");
});
}
else
{
//console.log("not granted");
// Permission denied
}
// expected output: "Success!"
});
}