Android: Check network state and WiFi SSID
Add Persmisions
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Check the instant Network status
We get an instance of class ConnectivityManager from system service with Context.CONNECTIVITY_SERVICE. It provide the instant network state. We call ConnectivityManager.getActiveNetwork() to have the network of the system currently use. Then we check the network type from the object NetworkCapabilites.fun updateActiveNetworkFlags(){ val connectivityManager = getSystemService((Context.CONNECTIVITY_SERVICE)) as ConnectivityManager val currentNetwork: Network? = connectivityManager.activeNetwork val networkCapability: NetworkCapabilities? = connectivityManager.getNetworkCapabilities(currentNetwork) wifiConnected = networkCapability?.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) ?:false mobileConnected = networkCapability?.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) ?:false Log.d(TAG,"Wifi network is active: $wifiConnected") Log.d(TAG, "Mobile network is active: $mobileConnected") }
Class Network represents one of the networks that the device connected to. When the network is disconnected, the Network object is invalid immediately. New Network object is created if the device later reconnects to the same appliance.
Listen to network events
ConnectivityManager.getActiveNetwork() only provide the network status at the instant time. To keep monitoring the connection, we need to register a callback with ConnectivityManager.
ConnectivityManager.registerDefaultNetworkCallback(NetworkCallback) and ConnectivityManager.registerNetworkCallback(NetworkRequest, NetworkCallback) serve different purposes.
The default Network is determined by system, which would prefers unmetered and faster network. So the system may switch the default network when it connects to a better one (from mobile to Wi-Fi).
However, some apps may interest about other Network. Build NetworkRequest object matching the required properties and call ConnectivityManager.registerNetworkCallback(NetworkRequest, NetworkCallback).
connectivityManager.registerDefaultNetworkCallback(object : ConnectivityManager.NetworkCallback() { override fun onAvailable(network : Network) { Log.e(TAG, "The default network is now: " + network) } override fun onLost(network : Network) { Log.e(TAG, "The application no longer has a default network. The last default network was " + network) } override fun onCapabilitiesChanged(network : Network, networkCapabilities : NetworkCapabilities) { Log.e(TAG, "The default network changed capabilities: " + networkCapabilities) } override fun onLinkPropertiesChanged(network : Network, linkProperties : LinkProperties) { Log.e(TAG, "The default network changed link properties: " + linkProperties) } })
Unregister the Network Callback when is not needed .
NET_CAPABILITY_INTERNET: The network is set up to access the internet but not actually able to reach public servers
NET_CAPABILITY_NOT_METERED: the network data transfer is not metered
NET_CAPABILITY_NOT_VPN: the network isn’t a virtual private network
NET_CAPABILITY_VALIDATED: The network has actual access to the public internet. But still it maybe blocked if the IP-based filtering is used on the connected appliance.
NET_CAPABILITY_CAPTIVE_PORTAL: The network has a captive portal when it is probed
Check Wifi SSID (name)
For Android 8
// Network capabilities have changed for the network override fun onCapabilitiesChanged(network: Network, networkCapabilities: NetworkCapabilities) { val wiFiManager = getSystemService(Context.WIFI_SERVICE) as WifiManager val wifiInfo : WifiInfo? = wiFiManager.connectionInfo as WifiInfo? if(wifiInfo != null) Log.d(TAG, "onCapabilitiesChanged() Connected Wifi SSID: " + wifiInfo.ssid) }
For android 9 to 11
For android 12 or above
val networkCallback = if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.S){ object : ConnectivityManager.NetworkCallback(ConnectivityManager.NetworkCallback.FLAG_INCLUDE_LOCATION_INFO) { // Network capabilities have changed for the network override fun onCapabilitiesChanged(network: Network, networkCapabilities: NetworkCapabilities) { //super.onCapabilitiesChanged(network, networkCapabilities) val wifiInfo: WifiInfo? = networkCapabilities.transportInfo as WifiInfo? if(wifiInfo != null) { Log.d(TAG, "onCapabilitiesChanged() Connected Wifi SSID: " + wifiInfo.ssid) } } …… } }else{ object : ConnectivityManager.NetworkCallback() { // Network capabilities have changed for the network override fun onCapabilitiesChanged(network: Network, networkCapabilities: NetworkCapabilities) { val wiFiManager = getSystemService(Context.WIFI_SERVICE) as WifiManager val wifiInfo : WifiInfo? = wiFiManager.connectionInfo as WifiInfo? if(wifiInfo != null) Log.d(TAG, "onCapabilitiesChanged() Connected Wifi SSID: " + wifiInfo.ssid) } …… } }
Reference
[1] Read network state, Connectivity, Android developers,
https://developer.android.com/develop/connectivity/network-ops/reading-network-state
[2] WifiManager, Android developers
https://developer.android.com/reference/android/net/wifi/WifiManager#getconnectioninfo
[3] WifiInfo, Android developers
https://developer.android.com/reference/android/net/wifi/WifiInfo
留言
發佈留言