Reputation: 11
fun checkNetworkInfo(context: Context) {
val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val activeNetwork: NetworkInfo? = cm.activeNetworkInfo
val isConnected: Boolean = activeNetwork?.isConnectedOrConnecting == true
val networkCapabilities = cm.getNetworkCapabilities(activeNetwork?.**network**)
val isWiFi: Boolean = networkCapabilities?.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) == true
val isMobile: Boolean = networkCapabilities?.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) == true
val mobileType: String = when (activeNetwork?.subtype) {
TelephonyManager.NETWORK_TYPE_GPRS -> "GPRS"
TelephonyManager.NETWORK_TYPE_EDGE -> "EDGE"
TelephonyManager.NETWORK_TYPE_UMTS -> "UMTS"
TelephonyManager.NETWORK_TYPE_HSDPA -> "HSDPA"
TelephonyManager.NETWORK_TYPE_HSUPA -> "HSUPA"
TelephonyManager.NETWORK_TYPE_HSPA -> "HSPA"
TelephonyManager.NETWORK_TYPE_CDMA -> "CDMA"
TelephonyManager.NETWORK_TYPE_EVDO_0 -> "EVDO revision 0"
TelephonyManager.NETWORK_TYPE_EVDO_A -> "EVDO revision A"
TelephonyManager.NETWORK_TYPE_1xRTT -> "1xRTT"
TelephonyManager.NETWORK_TYPE_LTE -> "LTE"
else -> "Unknown"
}
val telephonyManager = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
val carrierFrequencyInfo = telephonyManager.**carrierFrequencyInfo**
val carrierFrequency: String = if (**carrierFrequencyInfo** != null) {
"${carrierFrequencyInfo.dlBandwidth} Hz"
} else {
"Not available"
}
println("Active Network: $activeNetwork")
println("Connected: $isConnected")
println("WiFi: $isWiFi")
println("Mobile: $isMobile")
println("Mobile Type: $mobileType")
println("Carrier Frequency: $carrierFrequency")**your text**
In the code block above I was trying to query network information from the phone using Android Connectivity manager and then later on print the queried information to the home screen however the kotlin compiler is returning unresolved reference errors on the network and carrierFrequencyInfo properties which I have highlighted. Any help in solving the errors or in generating alternative code for querying network information like the Frequency, Network Service Provider, Network Type, Connectivity Status in Kotlin would be appreciated.
Upvotes: 0
Views: 194