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

Android functions providing empty responses #9

Closed
jkaravakis opened this issue Nov 8, 2019 · 16 comments
Closed

Android functions providing empty responses #9

jkaravakis opened this issue Nov 8, 2019 · 16 comments
Labels
help wanted Extra attention is needed

Comments

@jkaravakis
Copy link
Contributor

I have successfully implemented react-native-wifi-reborn into my app, but none of the functions are responding properly on Android.

For example getCurrentWifiSSID is giving the following logged response "Your current connected wifi SSID is "

connectToProtectedSSID is catching a failure, and loadWifiList is succeeding but returning an empty array.

I am using React Native 0.61.2, and did not link. Do I need to manually request WiFi permissions from the user?

Thank you!

@JuanSeBestia
Copy link
Owner

What could you see on the console ?, did I get an error?

@JuanSeBestia JuanSeBestia added the help wanted Extra attention is needed label Nov 9, 2019
@jkaravakis
Copy link
Contributor Author

WifiManager.getCurrentWifiSSID()
      .then(ssid => {
        console.log('Your current connected wifi SSID is ' + ssid);
      })
      .catch(err => {
        console.log('Cannot get current SSID!' + err);
      });

getCurrentWifiSSID logs Your current connected wifi SSID is <unknown ssid>

WifiManager.connectToProtectedSSID('wifiNetwork', 'password', true)
      .then(success => {
        console.log('success' + success);
      })
      .catch(err => {
        console.log(err);
      });

connectToProtectedSSID logs

Error: Can't connect to wifi!
    at Object.fn [as connectToProtectedSSID] (NativeModules.js:99)
    at Welcome.js:20
    at commitHookEffectList (ReactNativeRenderer-dev.js:16921)
    at commitPassiveHookEffects (ReactNativeRenderer-dev.js:16970)
    at Object.invokeGuardedCallbackImpl (ReactNativeRenderer-dev.js:307)
    at invokeGuardedCallback (ReactNativeRenderer-dev.js:531)
    at flushPassiveEffectsImpl (ReactNativeRenderer-dev.js:20061)
    at unstable_runWithPriority (scheduler.development.js:643)
    at runWithPriority (ReactNativeRenderer-dev.js:5591)
    at flushPassiveEffects (ReactNativeRenderer-dev.js:20031)
WifiManager.loadWifiList(
      function(success) {
        console.log('success!!!');
        console.log(success);
      },
      function(failure) {
        console.log('failure!!!');
      },
    );

loadWifiList logs

success!!!
[]

@eliaslecomte
Copy link
Collaborator

eliaslecomte commented Nov 13, 2019

The current code requires location permission. It can't scan for wifi networks or connect to one starting Android 6 without it.
I've openen a PR: #13 that makes it possible to connect without the permission but this should indeed be highlighted through the documentation better.

@eliaslecomte
Copy link
Collaborator

eliaslecomte commented Nov 13, 2019

I am willing to look into making this more clear:

  • update readme
  • return an error telling location permission is required to scan when trying without

@JuanSeBestia can you assign me to this?

@jkaravakis
Copy link
Contributor Author

Thank you @eliaslecomte! I've implemented a permissions request with PermissionsAndroid for ACCESS_FINE_LOCATION and the loadWifiList and getCurrentWifiSSID functions are now working. However, I still can't get the connectToProtected SSID function to work. I'm getting the following error now:

Error: Can't connect to wifi!
    at Object.fn [as connectToProtectedSSID] (NativeModules.js:99)
    at SetupChecklist.js:45
    at MessageQueue.__invokeCallback (MessageQueue.js:483)
    at MessageQueue.js:135
    at MessageQueue.__guard (MessageQueue.js:384)
    at MessageQueue.invokeCallbackAndReturnFlushedQueue (MessageQueue.js:134)
    at debuggerWorker.js:80

I'm going to dig into this a bit more, but any thoughts are much appreciated.

@eliaslecomte
Copy link
Collaborator

Hey @jkaravakis. Even ACCESS_COARSE_LOCATION should do the trick. There is corrently a bug in the connect logic on line 213 and + of RNWifiModule.java

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { conf.SSID = ssid; } else { conf.SSID = "\"" + ssid + "\""; }

This is not correct. This is fixed in our fork https://github.com/inthepocket/react-native-wifi-reborn and there is a PR open. Waiting for @JuanSeBestia to publish it :-).

@jkaravakis
Copy link
Contributor Author

@eliaslecomte you are awesome! I can confirm it works using your fork. Much appreciated!

FYI regarding coarse access, that did not seem to work for me.

@mferhatkeles
Copy link

I'm having same problems with @jkaravakis and even after defining ACCESS_FINE_LOCATION nothing worked for me.

my logs are same as well,
"Your current connected wifi SSID is "
and
"Error: Can't connect to wifi!"

also I don't know how to get @eliaslecomte 's fork 😞

@jkaravakis
Copy link
Contributor Author

Hi @mferhatkeles,

I had to use PermissionsAndroid.request to get location permission from the user. Documentation can be founder here: https://facebook.github.io/react-native/docs/permissionsandroid

Essentially you want to run the below function:

PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
      );

Hope this helps!

@mferhatkeles
Copy link

mferhatkeles commented Nov 17, 2019

hello @jkaravakis

async function requestAccess() {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
//PermissionsAndroid.PERMISSIONS.ACCESS_NETWORK_STATE,
{
'title': 'Wifi networks',
'message': 'We need your permission in order to find wifi networks'
}
)
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log("Thank you for your permission! :)");
} else {
console.log("You will not able to retrieve wifi available networks list");
}
} catch (err) {
console.warn(err)
}
}

this is my function to get it and I can get granted every time. but wifi functions do not work. did you totally fix your problem? can you use connectToProtectedSSID ?

@jkaravakis
Copy link
Contributor Author

@mferhatkeles I don't see anything wrong with your code, have your tried nesting connecToProtectedSSID within if (granted === PermissionsAndroid.RESULTS.GRANTED)? This structure is working for me.

@mferhatkeles
Copy link

@jkaravakis ı just tried this and nothing happened 😞

@eliaslecomte
Copy link
Collaborator

@mferhatkeles there is a problem currently with the connecToProtectedSSID on some Android versions. Can you get the list wifi networks to work?
Did you add the location permission to your android manifest?

@mferhatkeles
Copy link

mferhatkeles commented Nov 18, 2019

@eliaslecomte yes I added the permission to android manifest still does not work. Still I might have problem with permission tho. Where should ı call permission function?

@JuanSeBestia
Copy link
Owner

JuanSeBestia commented Nov 19, 2019

v2.2.2 is Released!
But i'm ver taired today to check this issue, sorry :(

@jkaravakis
Copy link
Contributor Author

v2.2.2 seems to have addressed the original issue. Many thanks to @eliaslecomte for the excellent work!

eliaslecomte pushed a commit that referenced this issue Aug 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants