Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

findAndConnect should only return true upon connection success #4

Open
corymsmith opened this issue Oct 21, 2016 · 10 comments
Open

findAndConnect should only return true upon connection success #4

corymsmith opened this issue Oct 21, 2016 · 10 comments

Comments

@corymsmith
Copy link

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");
  }
});
@devstepbcn
Copy link
Owner

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.

@corymsmith
Copy link
Author

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 :)

@corymsmith
Copy link
Author

If I have time maybe I'll submit a PR, otherwise we can probably close this.

@devstepbcn
Copy link
Owner

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!

@scriptfans
Copy link

+1 for this

@asCii88
Copy link

asCii88 commented Jun 30, 2017

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

@skibz
Copy link

skibz commented Oct 10, 2017

based on this - it looks like one could emit an event or fulfill a promise (or both) when this broadcast receiver is invoked.

@tripflex
Copy link

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:

https://github.com/tripflex/WifiWizard2/blob/master/src/android/wifiwizard2/WifiWizard2.java#L532-L561

@iuliuvisovan
Copy link

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

@jnystrom
Copy link

jnystrom commented Feb 1, 2019

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants