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

isWifiEnabled doesnt properly reflect the state of the wifi #6

Open
Freundschaft opened this issue Oct 27, 2014 · 12 comments
Open

isWifiEnabled doesnt properly reflect the state of the wifi #6

Freundschaft opened this issue Oct 27, 2014 · 12 comments

Comments

@Freundschaft
Copy link

isWifiEnabled is only set after the status of the wifi is retrieved, if I use addnetwork without first checking the active wifi connection it will incorrectly give me the information that wifi is not enabled

EDIT: never mind I found the fix. apparently I was using an older version of the plugin

@parsonsmatt
Copy link
Collaborator

Thanks for reporting the issue! Could you let me know which version you were using? If it's not a known issue already I'll add it.

@Freundschaft
Copy link
Author

Thanks for the reply!
I'll make some further evaluations apparently there is still some issue when wifi is already enabled and I try to add a new network, I get a response from the plugin telling me that wifi is not enabled. I'll get a detailled report and post it here.

@Freundschaft
Copy link
Author

I think the problem is somehow related to the way setWifiEnabled works.
When I start setWifiEnabled on Android it might take a while till the wifi is actually enabled, the callback that from setWifiEnabled can apparently be invoked before isWifiEnabled reflects the correct status
possibly it would make more sense to check on getWifiState () instead of isWiFiEnabled? Does Android allow adding of networks if the WiFi is in the state of WIFI_STATE_ENABLING?

@parsonsmatt
Copy link
Collaborator

Interesting. I don't know offhand but I'll look into it.

@brugnara, could you comment on this behavior? You're more familiar with the code on this feature than I am.

@Freundschaft
Copy link
Author

I can confirm the behavior. If I run WifiWizard.addNetwork within a WifiWizard.setWifiEnabled success handler, I get timing issues due to the unclear state during wifi enabling phase.
In my opinion this can be fixed by checking getWiFiState and comparing with the different enums of

WIFI_STATE_DISABLED, WIFI_STATE_DISABLING, WIFI_STATE_ENABLED, WIFI_STATE_ENABLING, WIFI_STATE_UNKNOWN

right now I have added a timeout between setting wifi to enabled and adding a wifi network, but that's merely a hacky fix of course.

@Freundschaft
Copy link
Author

WifiWizard.setWifiEnabled("true", function (result) {
                            console.log("enabling Wifi");
                            console.log(result);
                            var nfcwificonfig = WifiWizard.formatWPAConfig(WifiWizard.formatWifiString(SSID), Password);

                            WifiWizard.isWifiEnabled(function(wifienabled) {
                                console.log(wifienabled);
                            });

                            //$timeout(function() {
                                        WifiWizard.addNetwork(nfcwificonfig, function(result) {
                                            console.log("addNetwork success. message from Plugin: " + result);

                                            WifiWizard.connectNetwork(SSID, function() {
                                                console.log("connectNetwork success. message from Plugin: " + result);
                                            });
                                        }, function (error) {
                                            console.log("error adding network: " + error);
                                        });
                            //}, 2000);
                        });

yields

D/CordovaLog(28830): file:///android_asset/www/js/app.js: Line 33 : enabling Wifi
D/CordovaLog(28830): file:///android_asset/www/js/app.js: Line 34 : OK
D/CordovaLog(28830): file:///android_asset/www/js/app.js: Line 52 : true
D/CordovaLog(28830): file:///android_asset/www/js/app.js: Line 64 : error adding network: Wifi is not enabled.

What's really confusing me is the fact that WiFiWizard.isWiFiEnabled still yields true even though WiFiWizard.addNetwork is telling me that the WiFi is not enabled. From what I can see in your java plugin code both should depend on the value of WiFiManager.isWifiEnabled ()

apparently there is a related post on stackoverflow.
http://stackoverflow.com/questions/12560348/android-wifimanager-iswifienabled-returns-true-but-wifi-is-disabled

@parsonsmatt
Copy link
Collaborator

Good call. It seems that setWifiEnabled returns true as soon as the process starts. Since a very common use case is to setWifiEnabled(function(msg) { doSomethingWithWifi(); }), I think that changing the Java code to somehow wait for getWifiState() == WIFI_STATE_ENABLED before triggering the callback might be a good solution. A simple timeout might do it, if the provided functions don't end up being reliable for control flow.

@brugnara
Copy link
Contributor

Android API is involved here and the behaviour of isWifiEnabled is the one we adopted. We surely can implement a getWifiState() that returns an ENUM but if Android developers had write two different functions I think that this should be reflected into this package as it is right now. We can't predict all android failures and timings. This library is only a "man in the middle" and not a framework that will magically solve al problems :)

That part of code is very simple:

private boolean isWifiEnabled(CallbackContext callbackContext) {
    boolean isEnabled = wifiManager.isWifiEnabled();
    callbackContext.success(isEnabled ? "1" : "0");
    return isEnabled;
}

And Android API says about isWifiEnabled:

Return whether Wi-Fi is enabled or disabled.

Returns
true if Wi-Fi is enabled
See Also
getWifiState()

@brugnara
Copy link
Contributor

@parsonsmatt Waiting for status change sounds very bad for me. As I wrote, this is not a framework :)

@Freundschaft
Copy link
Author

I'd also agree with @brugnara on the framework question
I think the issue might be a different one here.

What you do in Java is:

if(action.equals(IS_WIFI_ENABLED)) {
                return this.isWifiEnabled(callbackContext);
        } else if(action.equals(SET_WIFI_ENABLED)) {
                return this.setWifiEnabled(callbackContext, data);
        } else if (!wifiManager.isWifiEnabled()) {
                callbackContext.error("Wifi is not enabled.");
                return false;
        } else if(action.equals(ADD_NETWORK)) {
                return this.addNetwork(callbackContext, data);
        } else if(action.equals(REMOVE_NETWORK)) {
[...]

A thing that I unfortunately don't know is if Android allows the addition of networks if the state is still on "WIFI_STATE_ENABLING".
In that case I guess that the check that causes "Wifi is not enabled" should be changed from !wifiManager.isWifiEnabled() to (getWifiState()==WIFI_STATE_ENABLING||WIFI_STATE_ENABLED) or something similar.

I also agree that the phonegap plugin should be modeled after native android behavior, but in case that android does allow adding and removal of networks during ENABLING phase, then the current implementation does not reflect this properly.

@DynamicRemo
Copy link

Yesterday i got the latest version of WifiWizard Plugin for my Cordova/Phonegap Project via "cordova plugin add com.pylonproducts.wifiwizard". Everything works fine except the WifiWizard.isWifiEnabled, it always return True even when the Wifi is not enabled.
Here`s my code, please look into this, Thanking You!

WifiWizard.isWifiEnabled( wifiIsEnabled, wifiIsNotEnabled );
function wifiIsEnabled(e){
     alert("wifi is Enabled");
}
function wifiIsNotEnabled(e){
    alert("wifi Is Not Enabled");
}

@hoerresb
Copy link
Owner

hoerresb commented Oct 9, 2015

Thanks for bringing this up. I will take a look at the issue and hopefully have a fix in the next couple of days.

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

5 participants