【问题标题】:Turn on Wifi and connect to a protected SSID on ios with react native打开 Wifi 并使用 react native 连接到 ios 上受保护的 SSID
【发布时间】:2020-12-13 15:03:41
【问题描述】:

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

附:我完全按照库中的文档进行了操作,包括两个平台的位置访问。

【问题讨论】:

    标签: ios network-programming react-native-ios


    【解决方案1】:

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

    iOS:

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

    您应该将这些功能添加到您的应用中:

    1. 访问 WiFi 信息

    2. 热点配置(README 中遗漏了这一点!

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

    安卓:

    别忘了开启定位功能!

    从当前的 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!"
          });
      }
    

    【讨论】:

    • 是否可以在 iOS 和 Android 上以编程方式打开具有特定 SSID 和密码的热点?
    【解决方案2】:

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

    方法setEnabled仅适用于android。

    【讨论】:

      猜你喜欢
      • 2023-02-15
      • 2013-02-06
      • 2021-02-05
      • 2014-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-18
      • 1970-01-01
      相关资源
      最近更新 更多