devstepbcn / react-native-android-wifi

A react-native module for viewing and connecting to Wifi networks on Android devices.
ISC License
212 stars 121 forks source link

findAndConnect restarting the phone and crashing the app #54

Open shihabus opened 6 years ago

shihabus commented 6 years ago

As a workaround for connection verification I have used NetInfo API. But when I use the findAndConnect method to connect, it crashes the app and restarts the device. What can be the reason for this odd behavior?

btav commented 6 years ago

Which version of Android are you using?

shihabus commented 6 years ago

I was using Oreo, which version 8.0+. But in lower version, the findAndConnect was not crashing.

devstepbcn commented 6 years ago

Hi @shihabus , have you request user permissions? request permissions Check issues #49, #47

Can you reproduce the bug with the app Example ?

@shihabus we use NetInfo as well #2

shihabus commented 6 years ago

The problem with using NetInfo is that, connectionChange togles muiltiple times on connecting. It is hard to derive a pattern so as to understand whether the connection was successful or not. Can anyone help me on this? @devstepbcn I am requesting permission but not access-coarse-location. I will try this and let you know.

shihabus commented 6 years ago

@btav I was using Nougat and Oreo. In Nougout it was fine, in Oreo the app gets crashed and phone reboots.

gigby commented 6 years ago

@shihabus I don't use NetInfo as well. I am just periodically calling getSsid() and it works good.

shihabus commented 6 years ago

@gigby That workaround makes sense. So u call getSSID() how frequent? You have any input on, why wifiList returns empty string? This is a recent issue, previously when I was using the lib, this behavior was not there.

gigby commented 6 years ago

@shihabus exaple of using getSSID(): async waitingForIsConnectionRight(multiplier) { let res = false; let counter = 5; const time = 1000;

const delay = multiplier ? multiplier * time : time;

do {
  counter -= 1;

  /* eslint-disable no-await-in-loop */
  res = await new Promise((resolve) => {
    setTimeout(() => {
      resolve(this.isWifiConnectionRight());
    }, delay);
  });
} while ((counter > 0) && !res);
/* eslint-enable no-await-in-loop */

return res;

}

Getting wi-fi list: setWifiNetworks = async () => { const data = await new Promise((resolve, reject) => { wifi.reScanAndLoadWifiList((wifiStringList) => { resolve(filterDuplicatedNetworks(JSON.parse(wifiStringList))); }, (error) => { reject(error); }, ); });

this.setState({
  data,
  loading: false,
  isFormShowed: false,
});

};

These functions work well on almost all devices on Android 7 and 8. I checked few days age this library after last updates (they fixed android 8 connection). It works well

shihabus commented 6 years ago

@gigby Thanks!!!

_handleConnectivityChange = async (isConnected) => { // ==== let connectionInfo = await NetInfo.getConnectionInfo() this.props.connectionStateChanged(isConnected) this.props.connectionTypeChanged(connectionInfo.type) // console.log('Type',this.props.connectionType) // console.log('State',this.props.connectionState) };

onConnect=async ()=>{ WifiManager.connect(this.props.selectedWifi.SSID,this.props.passphrase); this.setLoaderVisible(true) this._handleConnectivityChange() setTimeout(()=>{ if(this.props.connectionType==='wifi'){ wifi.getSSID((ssid) => { if(ssid===this.props.selectedWifi.SSID){ Alert.alert( 'Info', 'Authentication success!', [ {text: 'OK', onPress: () => { this.setLoaderVisible(false) this.props.connectionSuccess(this.props.selectedWifi.SSID,this.props.passphrase) Actions.dashboard() }

              },
            ],
            { cancelable: false }
          )
        }
      })
}
if(this.state.androidPlatform<=25){
      if(this.props.connectionType==='none'){
        Alert.alert(
            'Alert',
            'Authentication failed!',
            [
              {text: 'OK', onPress: () => {
                this.setLoaderVisible(false)
                this.props.connectionFailed()
                wifi.isRemoveWifiNetwork(this.props.selectedWifi.SSID, (isRemoved) => {

                });
              }
              },
            ],
            { cancelable: false }
          )
    }
}else{
      Alert.alert(
          'Alert',
          'Authentication failed!',
          [
            {text: 'OK', onPress: () => {
              this.setLoaderVisible(false)
              this.props.connectionFailed()
              wifi.isRemoveWifiNetwork(this.props.selectedWifi.SSID, (isRemoved) => {

              });
            }
            },
          ],
          { cancelable: false }
        )
  }

}, 5000); }

shihabus commented 6 years ago

@gigby the method I follow is, after trying to connect with WifiManager.connect() I try to watch NetInfo. If the state changes, I try to get SSID and try to see if it matches the SSID I tried to connect.

shihabus commented 6 years ago

@devstepbcn can u fix the Connect() method.