Wifi列表扫描

Kernel: v4.4.179
Device: rk3328
Platform Version: Android 9.0

只针对权限允许的情况

  1. 打开wifi开关
if (!wifiManager.isWifiEnabled()) {
            if (wifiManager.setWifiEnabled(true)) {
                Log.i(TAG, "Wi-fi enabled");
            } else {
                Log.w(TAG, "Wi-fi could not be enabled!");
                return null;
            }

            int count = 0;
            while (!wifiManager.isWifiEnabled() && count < 10) {
                try {
                    Thread.sleep(1000);
                    ++count;
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
    }
  1. 开始扫描
	if (wifiManager.isWifiEnabled()) {

            boolean scanned = wifiManager.startScan();
            if (DBG)
                Log.d(TAG, "startScan = " + scanned);
            final List<WifiConfiguration> configs = wifiManager.getConfiguredNetworks();
            if (DBG)
                Log.d(TAG, "configs = " + configs);
            if (configs != null) {
                for (WifiConfiguration config : configs) {
                    AccessPoint accessPoint = new AccessPoint(config);
                    accessPoints.add(accessPoint);
                    apMap.put(accessPoint.ssid, accessPoint);
                }
            }

            int scanCount = 0;
            while (wifiManager.getScanResults().size() == 0 && scanCount < 10) {
                try {
                    Thread.sleep(1000);
                    ++scanCount;
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            List<ScanResult> results = wifiManager.getScanResults();
            if (results != null) {
                for (ScanResult result : results) {
                    // Ignore hidden and ad-hoc networks.
                    if (result.SSID == null || result.SSID.length() == 0 ||
                            result.capabilities.contains("[IBSS]")) {
                        continue;
                    }
                    boolean found = false;
                    for (AccessPoint accessPoint : apMap.getAll(result.SSID)) {
                        if (accessPoint.update(result))
                            found = true;
                    }
                    if (!found) {
                        AccessPoint accessPoint = new AccessPoint(result);
                        accessPoints.add(accessPoint);
                        apMap.put(accessPoint.ssid, accessPoint);
                    }
                }
            }
            Collections.sort(accessPoints);
        }

转载请注明出处:http://www.wolfnx.com/2017/12/27/WifiScan

作者 : wolfnx
邮箱 : wolfnx@outlook.com
邮箱2 : lostnx@gmail.com

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值