上篇文章 Android 获取IP和UA中提及了获取WIFI的IP地址,本篇文章介绍下怎么扫描WIFI。

官方文档

权限

根据官方文档描绘,扫描WIFI需要请求相关权限,如下:

Android 13以上

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <!--在Android13以上,当usesPermissionFlags设置为neverForLocation时,无需再请求ACCESS_FINE_LOCATION权限-->
    <uses-permission
        android:name="android.permission.NEARBY_WIFI_DEVICES"
        android:usesPermissionFlags="neverForLocation" />
</manifest>

Android 13以下

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
</manifest>

需要留意,在实践测验中,Android 13以上的设备依然需要请求android.permission.ACCESS_FINE_LOCATION才干扫描到WIFI,测验设备为小米13。

注册播送监听扫描状态

经过注册播送监听WIFI扫描是否完结,代码如下:

class WIFIExampleActivity : AppCompatActivity() {
    private val scanResultReceiver = object : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent?) {
            if (intent?.getBooleanExtra(WifiManager.EXTRA_RESULTS_UPDATED, false) == true) {
                // 扫描完结
            }
        }
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // 注册播送
        registerReceiver(scanResultReceiver, IntentFilter().apply {
            addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)
        })
    }
    override fun onDestroy() {
        super.onDestroy()
        // 移除播送
        unregisterReceiver(scanResultReceiver)
    }
}

扫描WIFI

经过WifiManager扫描WIFI,并获取扫描结果,代码如下:

// 列表适配器
class WIFIAdapter : RecyclerView.Adapter<WIFIAdapter.WIFIViewHolder>() {
    private val wifiData = ArrayList<WIFIEntity>()
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): WIFIViewHolder {
        return WIFIViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.layout_wifi_item, parent, false))
    }
    override fun onBindViewHolder(holder: WIFIViewHolder, position: Int) {
        wifiData[position].run {
            holder.tvWifiName.text = wifiSSID
            holder.tvWifiSSID.text = wifiBSSID
            holder.ivWifiStrength.setImageResource(getStrengthIcon(wifiStrength))
            holder.ivNeedPassword.setImageResource(if (needPassword) R.drawable.icon_lock else R.drawable.icon_unlock)
        }
    }
    override fun getItemCount(): Int {
        return wifiData.size
    }
    fun setNewData(wifiData: ArrayList<WIFIEntity>?) {
        val lastItemCount = itemCount
        if (lastItemCount != 0) {
            this.wifiData.clear()
            notifyItemRangeRemoved(0, lastItemCount)
        }
        wifiData?.let { this.wifiData.addAll(it) }
        notifyItemChanged(0, itemCount)
    }
    private fun getStrengthIcon(wifiStrength: Int): Int {
        return when (wifiStrength) {
            0 -> R.drawable.wifi_strength_0
            1 -> R.drawable.wifi_strength_1
            2 -> R.drawable.wifi_strength_2
            else -> R.drawable.wifi_strength_3
        }
    }
    interface ItemClickListener {
        fun onItemClick(wifiInfo: WIFIEntity)
    }
    class WIFIViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val tvWifiName: AppCompatTextView = itemView.findViewById(R.id.tv_wifi_name)
        val tvWifiSSID: AppCompatTextView = itemView.findViewById(R.id.tv_wifi_ssid)
        val ivNeedPassword: AppCompatImageView = itemView.findViewById(R.id.iv_need_password)
        val ivWifiStrength: AppCompatImageView = itemView.findViewById(R.id.iv_wifi_strength)
    }
}
class WIFIExampleActivity : AppCompatActivity() {
    private lateinit var binding: LayoutWifiExampleActivityBinding
    private val wifiAdapter = WIFIAdapter()
    private var wifiManager: WifiManager? = null
    private var requestPermissionName: String = Manifest.permission.ACCESS_FINE_LOCATION
    private val requestSinglePermissionLauncher = registerForActivityResult(ActivityResultContracts.RequestPermission()) { granted: Boolean ->
        if (granted) {
            // 请求定位权限经过,扫描WIFI
            if (wifiManager?.isWifiEnabled == true) {
                wifiManager?.startScan()
            }
        } else {
            //未赞同授权
            if (!shouldShowRequestPermissionRationale(requestPermissionName)) {
                //用户回绝权限而且系统不再弹出请求权限的弹窗
                //这时需要咱们自己处理,比如自定义弹窗告知用户为何必须要请求这个权限
            }
        }
    }
    private val scanResultReceiver = object : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent?) {
            if (intent?.getBooleanExtra(WifiManager.EXTRA_RESULTS_UPDATED, false) == true) {
                val wifiData = ArrayList<WIFIEntity>()
                wifiManager?.scanResults?.forEach {
                    val ssid = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                        it.wifiSsid.toString()
                    } else {
                        it.SSID
                    }
                    val bssid = it.BSSID
                    // 获取WIFI加密类型
                    val capabilities = it.capabilities
                    // 获取WIFI信号强度
                    val level = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                        wifiManager?.calculateSignalLevel(it.level) ?: 0
                    } else {
                        WifiManager.calculateSignalLevel(it.level, 4)
                    }
                    wifiData.add(WIFIEntity(ssid, bssid, capabilities.contains("wpa", true) || capabilities.contains("web", true), capabilities, level))
                }
                // 根据信号强度降序排列
                wifiData.sortByDescending { it.wifiStrength }
                wifiAdapter.setNewData(wifiData)
            }
        }
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.layout_wifi_example_activity)
        wifiManager = applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
        binding.includeTitle.tvTitle.text = "WIFI Example"
        binding.btnStartScan.setOnClickListener {
            // 检测定位权限
            if (ActivityCompat.checkSelfPermission(this, requestPermissionName) == PackageManager.PERMISSION_GRANTED) {
                if (wifiManager?.isWifiEnabled == true) {
                    wifiManager?.startScan()
                }
            } else {
                requestSinglePermissionLauncher.launch(requestPermissionName)
            }
        }
        binding.rvWifiInfo.adapter = wifiAdapter
        registerReceiver(scanResultReceiver, IntentFilter().apply {
            addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)
        })
    }
    override fun onDestroy() {
        super.onDestroy()
        unregisterReceiver(scanResultReceiver)
    }
}

效果如图:

Android WIFI (一) 扫描WIFI

示例

在示例Demo中添加了相关的演示代码。

ExampleDemo github

ExampleDemo gitee