fix(network_info_plus): report ethernet IP, mask and gateway below Android 12 - #3999
Open
redge-munch wants to merge 1 commit into
Open
redge-munch wants to merge 1 commit into
redge-munch wants to merge 1 commit into
Conversation
…droid 12 The ConnectivityManager path in getWifiIPAddress() was gated at API 31, falling back to WifiManager below that. WifiInfo.ipAddress is 0 on a wired link, so getWifiIP() returned null and getWifiSubmask() returned an empty string on every Android below 12. That gate was never deliberate. It arrived in f750ae9, whose actual goal was reading the SSID on Android 12, where NetworkCapabilities.getTransportInfo() genuinely requires API 31. The same commit rewrote getWifiIPAddress() and reused the constant. 40d7e13 removed the gate from the SSID path nine days later, but it survived on the address and gateway paths. The gate now sits at API 24, which is the minSdkVersion the current Flutter tooling enforces, so no supported consumer can run below it. The APIs the path uses are older still: getActiveNetwork() is API 23 and getLinkProperties() is API 21. The same gate in NetworkInfoPlusPlugin.setupChannels() has to move too, otherwise connectivityManager stays null and nothing changes. getGatewayIPAddress() could not take the same change: it called LinkProperties.getDhcpServerAddress(), which is API 30 and would throw NoSuchMethodError below that. It now reads the default route's gateway via RouteInfo.isDefaultRoute() and getGateway(), both API 21. On API 31+ that also reports the gateway rather than the DHCP server address.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
If an Android device below version 12 is on ethernet, the plugin can't find the network:
getWifiIP()gives backnullgetWifiSubmask()gives back an empty stringgetWifiBroadcast()andgetWifiIPv6()don't work eithergetWifiGatewayIP()doesn't give the ethernet gatewayOn Android 12 and up, the plugin uses
ConnectivityManager. That works for any kind of connection. But it only turns on atBuild.VERSION_CODES.S. Below that, the plugin usesWifiInfoanddhcpInfo. Those only know about Wi-Fi, so on ethernet they give back0.The
Scheck came in with #1151. It was needed there for the SSID, becauseNetworkCapabilities.getTransportInfo()is API 31. But the calls for the IP and gateway are much older.This PR turns on the
ConnectivityManagercode from Android 7 (Build.VERSION_CODES.N, API 24). It makes 3 small changes:NetworkInfoPlusPlugin.setupChannels(): GetConnectivityManagerfrom N. Before, it wasnullon older devices. Without this, the other changes do nothing.NetworkInfo.getWifiIPAddress(): Only the version number changes.getActiveNetwork()is API 23, andgetLinkProperties()is API 21.NetworkInfo.getGatewayIPAddress(): Get the gateway from the default route. The old code usedgetDhcpServerAddress(), but that needs API 30. It would crash on API 24 to 29. The default route calls (isDefaultRoute()andgetGateway()) work from API 21.Subnet mask, broadcast and IPv6 don't need any changes. They all use the IP from
getWifiIPAddress(), so they start working once that does.The plugin's
minSdkis 19. Below N, the old Wi-Fi code still runs.One change on Android 12 and up:
getWifiGatewayIP()now gives the real gateway, not the DHCP server. Most of the time these are the same, but not always.Testing
flutter analyzefinds no problems innetwork_info_plus, andflutter testpasses.flutter build apk --debug.Related Issues
Checklist
CHANGELOG.mdnor the plugin version inpubspec.yamlfiles.flutter analyze) does not report any problems on my PR.Breaking Change
Does your PR require plugin users to manually update their apps to accommodate your change?
!in the title as explained in Conventional Commits).