Wifi列表扫描

Kernel: v4.4.179

Device: rk3328

Platform Version: Android 9.0

  1. 打开wifi开关
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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();
}
}
}

2. **开始扫描**
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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

Click Me