how to get current wifi connection info in android

前端 未结 3 685
天涯浪人
天涯浪人 2020-12-15 03:04

I\'m trying to find if scanResult is the current connected wifi network.

here is my code

public boolean IsCurrentConnectedWifi(ScanResul         


        
3条回答
  •  北荒
    北荒 (楼主)
    2020-12-15 03:41

    Update Android Oreo. You will also need to add ACCESS_FINE_LOCATION Permission in Manifest otherwise it will return unknown SSID

    
    
    
    
     fun getCurrentSsid(context: Context): String {
                var ssid = "NA"
                val connManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
                val networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
                if (networkInfo.isConnected) {
                    val wifiManager = context.getSystemService(Context.WIFI_SERVICE) as WifiManager
                    val connectionInfo = wifiManager.connectionInfo
                    if (connectionInfo != null && !connectionInfo.ssid.isEmpty()) {
                        ssid = connectionInfo.ssid
                    }
                }
                return ssid
            }
    

提交回复
热议问题