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

Connectivity return incorrect results ? #230

Closed
chihung93 opened this issue Sep 28, 2017 · 17 comments
Closed

Connectivity return incorrect results ? #230

chihung93 opened this issue Sep 28, 2017 · 17 comments

Comments

@chihung93
Copy link

chihung93 commented Sep 28, 2017

OS: Android 7.0
Emulator: Nexus S - Nougat API 24

When I turn off wifi on my phone, I return the result:
connectivity.isAvailable() == true

It happens when turn on/off Wifi on phone several times.

ReactiveNetwork.observeNetworkConnectivity(this)
                .subscribeOn(Schedulers.io())
                .observeOn(Schedulers.newThread())
                .subscribe(new Consumer<Connectivity>() {
                    @Override public void accept(final Connectivity connectivity) {
                        EventBus.getDefault().post(new NetworkEvent(connectivity.isAvailable()));
                    }
                });
@pwittchen
Copy link
Owner

pwittchen commented Sep 28, 2017

Thanks for reporting this issue.

This issue may be related to proxy or emulator configuration. I've tested it on real devices with a regular network connection and it worked fine. We can investigate proxy and emulator issue, but it may be system related thing.

@chihung93
Copy link
Author

it happens on this real device :

OS: Android 5.0
Real Device: Jacs - ANDROID PoE TOUCH DEVICES

@pwittchen
Copy link
Owner

pwittchen commented Sep 28, 2017

I've analyzed your code once again. Please note, you're using NetworkInfo#isAvailable() method. According to the documentation it "Indicates whether network connectivity is possible". Connectivity is possible, so the value is true. It's correct behavior. If you want to check if you're connected to the network or not, please use NetworkInfo#getState() method. In ReactiveNetwork, you can access it via Connectivity#getState() method. You can view available states in NetworkInfo.State documentation. E.g. it can be CONNECTED, DISCONNECTED, IDLE and so on.

@chihung93
Copy link
Author

And this lib so cool.
Thank you so much @pwittchen.

@pwittchen
Copy link
Owner

Ok. When everything is clear, then we can close this issue :).

Repository owner deleted a comment from maweilong Nov 20, 2017
@aldocano
Copy link

aldocano commented May 21, 2018

Im sorry for reopening this issue but i have a question that is similar to it. Please check the code below and i struggle with it because of the incorrect value it returns. Because i do have internet connection and the library return false multiple times.

    Disposable rec = ReactiveNetwork.observeInternetConnectivity(new SocketInternetObservingStrategy(), "https://google.com")
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(isConnectedToHost -> {
                if (isConnectedToHost) {
                    getApp().setConnected(true);
                    if(dialog!=null){
                        dialog.dismiss();
                    }
                } else {
                    getApp().setConnected(false);
                    showInternetDialog();
                }
            });

@pwittchen pwittchen reopened this May 21, 2018
@pwittchen
Copy link
Owner

Hi @aldocano,

In order to help you, I need more details. In standard scenario and setup, this code will work correctly. There must be something specific with your network, device or app.

@aldocano
Copy link

aldocano commented May 21, 2018

I happens in all devices, while i watch a online video in spans the dialog of no internet connection, i dont know why, because the device has continuous internet connection.

@chihung93
Copy link
Author

@aldocano Al
I think it pings to an IP address continuously so it will be blocked for a short time.

@aldocano
Copy link

Which one is the better way to continuously check the internet connection?

@pwittchen
Copy link
Owner

If it's not necessary, it's better to check connectivity only once.
Read more here: https://github.com/pwittchen/ReactiveNetwork#checking-internet-connectivity-once
You can observe connectivity continuously if you really need this.

@aldocano
Copy link

Sorry for late response, i need to continuously check it, maybe pinging server returns some timeouts and therefore it displays the not connected dialog.?

@pwittchen
Copy link
Owner

pwittchen commented May 22, 2018

Then you should use method with observe prefix. It's possible that server returns timeouts. It's also possible that you are using a proxy or network, which controls traffic and blocks many similar requests to avoid request flood, DoS, etc. You can try to set larger ping interval in milliseconds, which may fix this issue. It's possible with the library API. Default ping interval is 2000 ms (= 2 s). By the way, I have plans to make API for internet observing customisations more convenient.

@aldocano
Copy link

The server is google firebase server url. Strange that this occurs in strange conditions.
Can you show an example with the observe prefix and also the larger ping interval.

@pwittchen
Copy link
Owner

pwittchen commented May 22, 2018

Sure.

You can use the following observable:

Observable<Boolean> observeInternetConnectivity(int interval, String host, int port, int timeout)

For example:

Disposable rec = ReactiveNetwork.observeInternetConnectivity(5000, "https://google.com", 80, 1000)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(isConnectedToHost -> {
     			// ...
            });

You can find more methods here: https://github.com/pwittchen/ReactiveNetwork#observing-internet-connectivity

I know, this API became unconveninent when number of parameters and different variantions grew, but I have plan to fix this.

@aldocano
Copy link

Thank you for the help i will try and let you know if there is any difference. Thanks.

@pwittchen
Copy link
Owner

There's no further discussion in more than one month, so I'm closing 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

3 participants