获取手机wifi列表
1.首先是需要相关权限
至于怎么配置和申请权限可以查阅相关文档,下面置列出需要的权限
ACCESS_FINE_LOCATION
ACCESS_COARSE_LOCATION
CHANGE_WIFI_STATE
2.然后继承BroadcastReceiver编写对wifi变化的广播监听
package com.fdage.turn.dual.wifi_connect_util
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.net.NetworkInfo
import android.net.wifi.WifiManager
import android.util.Log
import com.fdage.Constant
import com.fdage.common.util.FdageLog
import com.fdage.utils.WifiUtil
//监听wifi状态
class WifiBroadcastReceiver : BroadcastReceiver() {
private var mContext: Context? = null
private var wifiReceiver: IWifiReceiver? = null
companion object {
private const val TAG = "WifiBroadcastReceiver"
var wifiReceiver: WifiBroadcastReceiver? = null
/**
* 注册wifi监听广播
*/
fun registerWifiReceiver(context: Context) {
if (wifiReceiver == null) {
mContext = context
wifiReceiver = WifiBroadcastReceiver()
//注册广播
val filter = IntentFilter()
filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION) //监听wifi是开关变化的状态
filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION) //监听wifi连接状态广播,是否连接了一个有效路由
filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION) //监听wifi列表变化(开启一个热点或者关闭一个热点)
context.registerReceiver(wifiReceiver, filter)
}
}
}
override fun onReceive(context: Context, intent: Intent) {
try {
val wifiManager = context
.getSystemService(Context.WIFI_SERVICE) as WifiManager
val success = intent.getBooleanExtra(WifiManager.EXTRA_RESULTS_UPDATED, false)
if (success) {
FdageLog.i(
TAG,
" [onReceive] 扫描成功 isConnecting: ${WifiAutomaticConnectionUtil.isConnecting}"
)
}
//wifi连接上与否
if (intent.action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
val info: NetworkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO)!!
if (info.state == NetworkInfo.State.DISCONNECTED) {
FdageLog.i(TAG, " [onReceive] wifi连接断开")
} else if (info.state == NetworkInfo.State.CONNECTED) {
val wifiInfo = wifiManager.connectionInfo
//获取当前wifi名称
FdageLog.i(TAG, " [onReceive] 连接到Wifi " + wifiInfo?.ssid)
}
}
//wifi打开与否
if (intent.action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
val wifistate = intent.getIntExtra(
WifiManager.EXTRA_WIFI_STATE,
WifiManager.WIFI_STATE_DISABLED
)
if (wifistate == WifiManager.WIFI_STATE_DISABLED) {
// wifiReceiver?.wifiStateChange(false)
FdageLog.i(TAG, " [onReceive] 系统关闭wifi ${wifiManager.isWifiEnabled}")
} else if (wifistate == WifiManager.WIFI_STATE_ENABLED) {
// wifiReceiver?.wifiStateChange(true)
FdageLog.i(TAG, " [onReceive] 系统开启wifi ${wifiManager.isWifiEnabled}")
}
}
} catch (e: Exception) {
FdageLog.i(TAG, " [onReceive] wifi广播监听报错e:${e.message} ${e.stackTrace}")
}
}
}
在需要时调用**WifiBroadcastReceiver.registerWifiReceiver()**方法来注册对wifi的变化进行监听。
3主动扫描wifi列表
注册之后可以主动调用扫描来触发wifi扫描成功的逻辑
//使用WifiManager 主动扫描wifi
var wifiManager: WifiManager = activity.getSystemService(Context.WIFI_SERVICE) as WifiManager
wifiManager.startScan()
需要注意的是**startScan()**方法是有时间和次数限制的, 官网的解释是:
每个前台应用可以在 2 分钟内扫描四次。这样便可在短时间内进行多次扫描。
所有后台应用总共可以在 30 分钟内扫描一次。
4.获取扫描结果
正常扫描后我们能在广播接收器中收到扫描成功的结果,在这里就可以调用wifiManager?.scanResults获取所有wifi的列表,但是还要对数据做些处理
/**
* 处理获取的wifi列表数据
*/
fun getScanResults(): MutableList<ScanResult> {
FdageLog.i(TAG, " [getScanResults]:获取wifi列表")
//过滤空的ssid并去重
val resultMap =
wifiManager?.scanResults?.filter { !it.SSID.isNullOrBlank() }?.groupBy { it.SSID }
val scanResults = mutableListOf<ScanResult>()
if (resultMap.isNullOrEmpty()) {
return scanResults
}
for ((_, value) in resultMap) {
// FdageLog.i(TAG, " [getScanResults] wifi的ssid:${value}")
// 取首个值
value.firstOrNull()?.let {
scanResults.add(it)
}
}
return scanResults
}
直接使用上面的方法就能获取到wifi列表了。
后续测试发现直接使用wifiManager?.scanResults也可以获取到wifi列表,只是拿到的列表可能不是最新数据。
具体使用都是根据业务逻辑进行设计,详细说明最好查阅官网的解释:
https://developer.android.com/guide/topics/connectivity/wifi-scan?hl=zh-cn