Skip to content

findAndConnect should only return true upon connection success #4

Open

Description

@corymsmith

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

Activity

  1. devstepbcn commented on Oct 24, 2016

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

  2. corymsmith commented on Oct 24, 2016

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

  3. corymsmith commented on Oct 24, 2016

    @corymsmith
    Author

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

  4. devstepbcn commented on Oct 25, 2016

    @devstepbcn
    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!

  5. scriptfans commented on Jan 3, 2017

    @scriptfans

    +1 for this

  6. asCii88 commented on Jun 30, 2017

    @asCii88

    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

  7. skibz commented on Oct 10, 2017

    @skibz

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

  8. tripflex commented on Feb 21, 2018

    @tripflex

    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

  9. iuliuvisovan commented on Jul 9, 2018

    @iuliuvisovan

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

  10. jnystrom commented on Feb 1, 2019

    @jnystrom

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions