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(network_info_plus): Get SSID on Android 12 and newer #1231

Merged
merged 2 commits into from
Oct 13, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,19 @@ 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() =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
val currentNetwork = connectivityManager?.activeNetwork
connectivityManager?.getNetworkCapabilities(currentNetwork)?.transportInfo as WifiInfo?
} else {
@Suppress("DEPRECATION")
wifiManager?.connectionInfo
}
// Using deprecated `connectionInfo` call here to be able to get info on demand
@Suppress("DEPRECATION")
private val wifiInfo: WifiInfo
get() = wifiManager.connectionInfo

// Android returns "SSID"
fun getWifiName(): String? = wifiInfo?.ssid
fun getWifiName(): String? = wifiInfo.ssid

fun getWifiBSSID(): String? = wifiInfo?.bssid
fun getWifiBSSID(): String? = wifiInfo.bssid

fun getWifiIPAddress(): String? {
var ipAddress: String? = null
Expand Down Expand Up @@ -89,7 +84,7 @@ internal class NetworkInfo(private val wifiManager: WifiManager?,
}
}
}
} catch (socketException: SocketException) {
} catch (ignored: SocketException) {

}
return null
Expand All @@ -103,7 +98,7 @@ internal class NetworkInfo(private val wifiManager: WifiManager?,
dhcpServer
} else {
@Suppress("DEPRECATION")
val dhcpInfo = wifiManager?.dhcpInfo
val dhcpInfo = wifiManager.dhcpInfo
val gatewayIPInt = dhcpInfo?.gateway

gatewayIPInt?.let { formatIPAddress(it) }
Expand Down