How do I see if Wi-Fi is connected on Android?

前端 未结 22 2623
挽巷
挽巷 2020-11-22 05:56

I don\'t want my user to even try downloading something unless they have Wi-Fi connected. However, I can only seem to be able to tell if Wi-Fi is enabled, but they could sti

22条回答
  •  鱼传尺愫
    2020-11-22 06:24

    The following code (in Kotlin) works from API 21 until at least current API version (API 29). The function getWifiState() returns one of 3 possible values for the WiFi network state: Disable, EnabledNotConnected and Connected that were defined in an enum class. This allows to take more granular decisions like informing the user to enable WiFi or, if already enabled, to connect to one of the available networks. But if all that is needed is a boolean indicating if the WiFi interface is connected to a network, then the other function isWifiConnected() will give you that. It uses the previous one and compares the result to Connected.

    It's inspired in some of the previous answers but trying to solve the problems introduced by the evolution of Android API's or the slowly increasing availability of IP V6. The trick was to use:

    wifiManager.connectionInfo.bssid != null 
    

    instead of:

    1. getIpAddress() == 0 that is only valid for IP V4 or
    2. getNetworkId() == -1 that now requires another special permission (Location)

    According to the documentation: https://developer.android.com/reference/kotlin/android/net/wifi/WifiInfo.html#getbssid it will return null if not connected to a network. And even if we do not have permission to get the real value, it will still return something other than null if we are connected.

    Also have the following in mind:

    On releases before android.os.Build.VERSION_CODES#N, this object should only be obtained from an Context#getApplicationContext(), and not from any other derived context to avoid memory leaks within the calling process.

    In the Manifest, do not forget to add:

    
    

    Proposed code is:

    class MyViewModel(application: Application) : AndroidViewModel(application) {
    
       // Get application context
        private val myAppContext: Context = getApplication().applicationContext
    
       // Define the different possible states for the WiFi Connection
        internal enum class WifiState {
            Disabled,               // WiFi is not enabled
            EnabledNotConnected,    // WiFi is enabled but we are not connected to any WiFi network
            Connected,              // Connected to a WiFi network
        }
    
        // Get the current state of the WiFi network
        private fun getWifiState() : WifiState {
    
            val wifiManager : WifiManager = myAppContext.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
    
            return if (wifiManager.isWifiEnabled) {
                        if (wifiManager.connectionInfo.bssid != null)
                            WifiState.Connected
                        else
                            WifiState.EnabledNotConnected
                   } else {
                        WifiState.Disabled
                   }
        }
    
        // Returns true if we are connected to a WiFi network
        private fun isWiFiConnected() : Boolean {
            return (getWifiState() == WifiState.Connected)
        }
    }
    

提交回复
热议问题