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

fix(android): isConnected is wrong incorrect for wifi networks only the app has access to #443

Conversation

eliaslecomte
Copy link
Contributor

@eliaslecomte eliaslecomte commented Feb 1, 2021

Overview

Use registerNetworkCallback instead of registerDefaultNetworkCallback as the default method isn't notified of wifi network changes that are available to the app but not the system / other apps.

Issue

When I use react-native-wifi-reborn to connect with an IoT wifi network (for commissioning), and I use fetch, netinfo returns the correct network information. However the isConnected is false and the eventListener isn't notified of the change.

After debugging the code I noticed that the ConnectivityNetworkCallback was never invoked for the new wifi network.

To me it looks like the registerDefaultNetworkCallback only notifies for wifi networks that are available system-wide, while the registerNetworkCallback also for wifi networks that are only available to the app.

Looking at the Android sdk this is what is executed for registerDefaultNetworkCallback:

@RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
    public void registerDefaultNetworkCallback(@NonNull NetworkCallback networkCallback,
            @NonNull Handler handler) {
        // This works because if the NetworkCapabilities are null,
        // ConnectivityService takes them from the default request.
        //
        // Since the capabilities are exactly the same as the default request's
        // capabilities, this request is guaranteed, at all times, to be
        // satisfied by the same network, if any, that satisfies the default
        // request, i.e., the system default network.
        CallbackHandler cbHandler = new CallbackHandler(handler);
        sendRequestForNetwork(null /* NetworkCapabilities need */, networkCallback, 0,
                REQUEST, TYPE_NONE, cbHandler);
    }

While registerNetworkCallback:

@RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
    public void registerNetworkCallback(@NonNull NetworkRequest request,
            @NonNull NetworkCallback networkCallback, @NonNull Handler handler) {
        CallbackHandler cbHandler = new CallbackHandler(handler);
        NetworkCapabilities nc = request.networkCapabilities;
        sendRequestForNetwork(nc, networkCallback, 0, LISTEN, TYPE_NONE, cbHandler);
    }

The default networkcapabilities are:

/**
     * Capabilities that are set by default when the object is constructed.
     */
    private static final long DEFAULT_CAPABILITIES =
            (1 << NET_CAPABILITY_NOT_RESTRICTED) |
            (1 << NET_CAPABILITY_TRUSTED) |
            (1 << NET_CAPABILITY_NOT_VPN);

Looking at the code the issue is that when fetching wifi data, the WifiManager is used to fetch fields like ssid, bssid, frequency, ... . The isConnected indicates false because the variable mConnectionType is actually NONE. Also, important to notice is, that if you have a mobile connection, it's actually true if that connection is active.

Related issues

Test Plan

Running a simple test; connect via react-native-wifi-reborn to a wifi network, call Netinfo.fetch() and see if wifi details + isConnected are correct.
Also checking if NetInfo.addEventListener is updated of the change.

Currently tested on:

  • Samsung Galaxy S10 (Android 11) ☑️
  • Huawei P10 (Android 10) ☑️
  • Pixel 2 (Android 10) ☑️

…networks that are available to the app but not the system. Using registerNetworkCallback fixes this.
@eliaslecomte
Copy link
Contributor Author

I'm no expert on the matter. I've read this post https://proandroiddev.com/connectivity-network-internet-state-change-on-android-10-and-above-311fb761925 before experimenting with the registerDefaultNetworkCallback / registerNetworkCallback to find out that fixes this issue.

@matt-oakes matt-oakes merged commit 7084771 into react-native-netinfo:master Mar 3, 2021
github-actions bot pushed a commit that referenced this pull request Aug 24, 2021
## [6.0.1](v6.0.0...v6.0.1) (2021-08-24)

### Bug Fixes

* Ensure warnings are not shown on React Native 0.65 ([#487](#487) by [@lubomyr](https://github.com/lubomyr)) ([ac0ed65](ac0ed65))
* **android:** isConnected is incorrect for wifi networks only the app has access to ([#443](#443) by [@eliaslecomte](https://github.com/eliaslecomte)) ([7084771](7084771))
@matt-oakes
Copy link
Collaborator

🎉 This PR is included in version 6.0.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

@mikehardy
Copy link
Contributor

This may have a regression as noted in #496 but that issue report has no details (yet), just cross-linking them via this comment.

tobyt42 added a commit to tobyt42/react-native-netinfo that referenced this pull request Nov 9, 2021
tailorem pushed a commit to fit52-digital/react-native-netinfo that referenced this pull request Jan 6, 2022
tailorem pushed a commit to fit52-digital/react-native-netinfo that referenced this pull request Jan 6, 2022
@mikehardy
Copy link
Contributor

It is with deep sadness that I must say we merged #571 which means the underlying issue here will be reopened.

This was painful but the regressions from this could not be ignored, and reverting this seemed like it would cause less trouble than leaving it in, though I/we recognize this is still a problem.

Please see #571 for discussion

@bhd119
Copy link

bhd119 commented Apr 8, 2022

6.0.1版本,修改NetworkCallbackConnectivityReceiver文件:
@OverRide
@SuppressLint("MissingPermission")
void register() {
try {
// NetworkRequest.Builder builder = new NetworkRequest.Builder();
getConnectivityManager().registerDefaultNetworkCallback(mNetworkCallback);
// getConnectivityManager().registerNetworkCallback(builder.build(), mNetworkCallback);
} catch (SecurityException e) {
// TODO: Display a yellow box about this
}
}

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

Successfully merging this pull request may close these issues.

Android 10 - type is none for no internet wifi connections.
4 participants