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): avoid crash on devices without wifi #662

Merged
merged 2 commits into from Apr 6, 2023

Conversation

g4rb4g3
Copy link
Contributor

@g4rb4g3 g4rb4g3 commented Apr 4, 2023

Overview

I recently faced a crash on a device that does not have WiFi and not provide the WiFi system service.

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.net.wifi.WifiManager.isWifiEnabled()' on a null object reference
        at com.reactnativecommunity.netinfo.ConnectivityReceiver.createConnectivityEventMap(ConnectivityReceiver.java:138)
        at com.reactnativecommunity.netinfo.ConnectivityReceiver.getCurrentState(ConnectivityReceiver.java:83)
        at com.reactnativecommunity.netinfo.NetInfoModule.getCurrentState(NetInfoModule.java:58)
.....

Only thing we use in our app is
const { isConnected } = useNetInfo();
So for us it doesn't really matter what kind of connection, if the device does not support WiFi but has some other connectivity it is fine.
This PR will add a null check for mWifiManager so it would not crash anymore.

Test Plan

No test plan from my side, just followed the stacktrace that showed the crash and fixed the issue, also checked other usages of mWifiManager so it would not crash somewhere else.

@g4rb4g3 g4rb4g3 requested a review from matt-oakes as a code owner April 4, 2023 06:54
Copy link
Contributor

@mikehardy mikehardy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry this crashed for you! Interesting edge case, but it makes sense.
I think the way you originally coded the first change will be breaking for people, but there is an easy modification I propose that should be non-breaking and still solve the crash?

What do you think?

…ivityReceiver.java

Co-authored-by: Mike Hardy <github@mikehardy.net>
@g4rb4g3 g4rb4g3 requested a review from mikehardy April 6, 2023 07:58
Copy link
Contributor

@mikehardy mikehardy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤞 looks good like this

@mikehardy mikehardy added the pending merge A PR that will be merged shortly, waiting for CI or final comment label Apr 6, 2023
@mikehardy mikehardy changed the title fix: crash on devices without wifi fix(android): crash on devices without wifi Apr 6, 2023
@mikehardy mikehardy changed the title fix(android): crash on devices without wifi fix(android): avoid crash on devices without wifi Apr 6, 2023
@mikehardy mikehardy merged commit a519e59 into react-native-netinfo:master Apr 6, 2023
2 checks passed
@mikehardy mikehardy removed the pending merge A PR that will be merged shortly, waiting for CI or final comment label Apr 6, 2023
github-actions bot pushed a commit that referenced this pull request Apr 6, 2023
## [9.3.9](v9.3.8...v9.3.9) (2023-04-06)

### Bug Fixes

* **android:** avoid crash on devices without wifi ([#662](#662)) ([a519e59](a519e59))
@matt-oakes
Copy link
Collaborator

🎉 This PR is included in version 9.3.9 🎉

The release is available on:

Your semantic-release bot 📦🚀

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.

None yet

3 participants