findAndConnect should only return true upon connection success
corymsmith opened this issue · 10 comments
The comment and console logs you have here says it will return true if its in range, since the function is findAndConnect shouldn't this only return true once it actually connects? Otherwise whats the best way to know that its been connected?
//found returns true if ssid is in the range
wifi.findAndConnect(ssid, password, (found) => {
if (found) {
console.log("wifi is in range");
}else{
console.log("wifi is not in range");
}
});
Hi corymsmith,
Android doesn’t provide a callback when you try to connect to a wifi network. You have to listen for connectivity changes to know if connection succeeded. You can use NetInfo from React Native Components to do it. If findAndConnect function returns false, you don't need to listen for this connectivity changes so you are not in range.
I'm familiar with NetInfo, I guess it would be nice of this lib handled some of that but I completely understand why it currently doesn't :)
If I have time maybe I'll submit a PR, otherwise we can probably close this.
Yes, sure! It would be nice to have some help to improve it. The lib could handle all this logic and provide the desired callback. I leave this issue open, thanks!
+1 for this
My application needs to know the wifi password for another purpose. Let's say the user has the password already configured for this specific SSID.
I do the following:
-Request password
-Disconnect the user from the network
-Call findAndConnect()
If the user has input the correct password then it will connect and netinfo will generate an event.
However, if the user has input an incorrect password then it will not connect, but the Android wifi daemon might take over and connect the phone to the SSID with the correct stored password, and thus netinfo will generate the same event.
This means there is no way to know if the password is correct or not.
Is there a way to stop the wifi daemon from connecting automatically? Is there a way to delete the network configuration for that SSID?
Edit: I've seen now that the code is supposed to delete that network
https://github.com/devstepbcn/react-native-android-wifi/blob/master/android/src/main/java/com/devstepbcn/wifi/AndroidWifiModule.java#L142
This doesn't work, because, according to the documentation
Applications are not allowed to remove networks created by other applications.
Source: https://developer.android.com/reference/android/net/wifi/WifiManager.html#removeNetwork(int)
This happens from Android 6.0 onwards
based on this - it looks like one could emit an event or fulfill a promise (or both) when this broadcast receiver is invoked.
There's a few methods that could be used for this, if you take a look at the fork of WifiWizard I have, you will see one PR I merged from original repo that checks in a for loop before resolving promise:
@asCii88 That is the exact conclusion I've arrived to and this is completely stupid. Have you managed to find a way around it by any means? I've tried every combination of findAndConnect/netinfo/remove networks.. but with no luck.
so I am trying to loop until the SSID is visible (think of it as a hardware device that has its own Hotspot). What would be the proper process to continue to try to connect to it?