打开Wifi并使用本机响应连接到ios上受保护的SSID

问题描述

我正在使用React Native进行IOT项目,其中Phone必须连接到受保护的WiFi(无互联网连接)。我应该以编程方式打开WiFi并连接到受保护的ssid。因此,我使用了以下库:https://github.com/JuanSeBestia/react-native-wifi-reborn。 在android上,我可以完全控制设备,并且一切正常。另一方面,在ios上,我无法打开wifi,即使该用户打开wifi之后,似乎库中定义的函数(如connectToProtectedSSID)也无法正常工作。 任何建议将不胜感激。

P.s。我完全按照库中的文档进行操作,包括在两个平台上的位置访问。

解决方法

最后,两个平台的解决方案:

iOS:

使用以下方法,应用将打开WIFI并连接到特定的SSID(至少适用于iOS 13):

您应该将以下功能添加到您的应用中:

  1. 访问WiFi信息

  2. 热点配置(自述文件中缺少此内容!

  3. (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",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!"
      });
  }

enter image description here

,

没有公共API可以控制Wifi on iOS

方法setEnabled仅适用于android。