Repository navigation
[network_info_plus] Use new Android APIs #1151
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
Merged
vbuberen
merged 7 commits into
fluttercommunity:main
from
vbuberen:update/network_info_android_apis
Oct 4, 2022
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bd1dfab
Add new APIs to get network info
vbuberen 49d5898
Add FINE_LOCATION permission to example manifest
vbuberen 364ec6e
Improve example app layout
vbuberen 4305905
Update release notes
vbuberen 1752a11
Update docs for website and README
vbuberen 9a8d509
Bump version, update repository link
vbuberen f3df017
Remove ACCESS_FINE_LOCATION from the plugin and add to example instead
vbuberen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,38 +1,49 @@ | ||
| package dev.fluttercommunity.plus.network_info | ||
|
|
||
| import android.net.ConnectivityManager | ||
| import android.net.wifi.WifiInfo | ||
| import android.net.wifi.WifiManager | ||
| import android.os.Build | ||
| import java.net.* | ||
|
|
||
| /** Reports network info such as wifi name and address. */ | ||
| internal class NetworkInfo(private val wifiManager: WifiManager?) { | ||
| internal class NetworkInfo(private val wifiManager: WifiManager?, | ||
| private val connectivityManager: ConnectivityManager? = null | ||
| ) { | ||
|
|
||
| private val wifiInfo: WifiInfo? | ||
| get() = wifiManager?.connectionInfo | ||
| get() = | ||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { | ||
| val currentNetwork = connectivityManager?.activeNetwork | ||
| connectivityManager?.getNetworkCapabilities(currentNetwork)?.transportInfo as WifiInfo? | ||
| } else { | ||
| @Suppress("DEPRECATION") | ||
| wifiManager?.connectionInfo | ||
| } | ||
|
|
||
| // Android returns "SSID" | ||
| fun getWifiName(): String? { | ||
| var ssid: String? = null | ||
| if (wifiInfo != null) ssid = wifiInfo!!.ssid | ||
| return ssid | ||
| } | ||
| fun getWifiName(): String? = wifiInfo?.ssid | ||
|
|
||
| fun getWifiBSSID(): String? { | ||
| return if (wifiInfo != null) { | ||
| wifiInfo!!.bssid | ||
| } else { | ||
| null | ||
| } | ||
| } | ||
| fun getWifiBSSID(): String? = wifiInfo?.bssid | ||
|
|
||
| fun getWifiIPAddress(): String? { | ||
| var wifiInfo: WifiInfo? = null | ||
| if (wifiManager != null) wifiInfo = wifiManager.connectionInfo | ||
| var ip: String? = null | ||
| var interfaceIp = 0 | ||
| if (wifiInfo != null) interfaceIp = wifiInfo.ipAddress | ||
| if (interfaceIp != 0) ip = formatIPAddress(interfaceIp) | ||
| return ip | ||
| var ipAddress: String? = null | ||
|
|
||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { | ||
| val linkAddresses = connectivityManager?.getLinkProperties(connectivityManager.activeNetwork)?.linkAddresses | ||
|
|
||
| val ipV4Address = linkAddresses?.firstOrNull { linkAddress -> | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| linkAddress.address.hostAddress?.contains('.') | ||
| ?: false | ||
| }?.address?.hostAddress | ||
|
|
||
| ipAddress = ipV4Address | ||
| } else { | ||
| @Suppress("DEPRECATION") | ||
| val interfaceIp = wifiInfo!!.ipAddress | ||
| if (interfaceIp != 0) ipAddress = formatIPAddress(interfaceIp) | ||
| } | ||
| return ipAddress | ||
| } | ||
|
|
||
| fun getWifiSubnetMask(): String { | ||
|
|
@@ -84,10 +95,19 @@ internal class NetworkInfo(private val wifiManager: WifiManager?) { | |
| return null | ||
| } | ||
|
|
||
| fun getGatewayIPAddress(): String { | ||
| val dhcpInfo = wifiManager!!.dhcpInfo | ||
| val gatewayIPInt = dhcpInfo.gateway | ||
| return formatIPAddress(gatewayIPInt) | ||
| fun getGatewayIPAddress(): String? { | ||
| return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { | ||
| val linkAddresses = connectivityManager?.getLinkProperties(connectivityManager.activeNetwork) | ||
| val dhcpServer = linkAddresses?.dhcpServerAddress?.hostAddress | ||
|
|
||
| dhcpServer | ||
| } else { | ||
| @Suppress("DEPRECATION") | ||
| val dhcpInfo = wifiManager?.dhcpInfo | ||
| val gatewayIPInt = dhcpInfo?.gateway | ||
|
|
||
| gatewayIPInt?.let { formatIPAddress(it) } | ||
| } | ||
| } | ||
|
|
||
| private fun formatIPAddress(intIP: Int): String = | ||
|
|
||
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,8 @@ | |
| package="dev.fluttercommunity.plus.network_info_plus_example"> | ||
|
|
||
| <uses-permission android:name="android.permission.INTERNET" /> | ||
| <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> | ||
| <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added it, so it is easy to open Settings of |
||
|
|
||
| <application | ||
| android:name="${applicationName}" | ||
|
|
||
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
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
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.

Uh oh!
There was an error while loading. Please reload this page.