devstepbcn/react-native-android-wifi

findAndConnect restarting the phone and crashing the app

Opened this issue · 11 comments

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

Which version of Android are you using?

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

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

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.

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

gigby commented

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

@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

@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

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

@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.

@devstepbcn can u fix the Connect() method.