Kernel: v4.4.179
Device: rk3328
Platform Version: Android 9.0
只针对权限允许的情况
- 打开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();
}
}
}
- 开始扫描
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