forked from mozilla/MozStumbler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWifiScanner.java
132 lines (110 loc) · 3.95 KB
/
WifiScanner.java
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package org.mozilla.mozstumbler;
import android.util.Log;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.WifiLock;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
public class WifiScanner extends BroadcastReceiver {
public static final String WIFI_SCANNER_EXTRA_SUBJECT = "WifiScanner";
public static final String WIFI_SCANNER_ARG_SCAN_RESULTS = "org.mozilla.mozstumbler.WifiScanner.scan_results";
private static final String LOGTAG = Scanner.class.getName();
private static final long WIFI_MIN_UPDATE_TIME = 1000; // milliseconds
private boolean mStarted;
private final Context mContext;
private WifiLock mWifiLock;
private Timer mWifiScanTimer;
private final Set<String> mAPs = new HashSet<String>();
private int mVisibleAPs;
WifiScanner(Context c) {
mContext = c;
}
public void start() {
WifiManager wm = getWifiManager();
if (mStarted || !wm.isWifiEnabled()) {
return;
}
mStarted = true;
mWifiLock = wm.createWifiLock(WifiManager.WIFI_MODE_SCAN_ONLY,
"MozStumbler");
mWifiLock.acquire();
IntentFilter i = new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
mContext.registerReceiver(this, i);
// Ensure that we are constantly scanning for new access points.
mWifiScanTimer = new Timer();
mWifiScanTimer.schedule(new TimerTask() {
@Override
public void run() {
Log.d(LOGTAG, "WiFi Scanning Timer fired");
getWifiManager().startScan();
}
}, 0, WIFI_MIN_UPDATE_TIME);
}
public void stop() {
if (mWifiLock != null) {
mWifiLock.release();
mWifiLock = null;
}
if (mWifiScanTimer != null) {
mWifiScanTimer.cancel();
mWifiScanTimer = null;
}
if (mStarted) {
mContext.unregisterReceiver(this);
}
mStarted = false;
mVisibleAPs = 0;
}
public void onReceive(Context c, Intent intent) {
ArrayList<ScanResult> scanResults = new ArrayList<ScanResult>();
for (ScanResult scanResult : getWifiManager().getScanResults()) {
scanResult.BSSID = BSSIDBlockList.canonicalizeBSSID(scanResult.BSSID);
if (shouldLog(scanResult)) {
scanResults.add(scanResult);
mAPs.add(scanResult.BSSID);
//Log.v(LOGTAG, "BSSID=" + scanResult.BSSID + ", SSID=\"" + scanResult.SSID + "\", Signal=" + scanResult.level);
}
}
mVisibleAPs = scanResults.size();
reportScanResults(scanResults);
}
public int getAPCount() {
return mAPs.size();
}
public int getVisibleAPCount() {
return mVisibleAPs;
}
private static boolean shouldLog(ScanResult scanResult) {
if (BSSIDBlockList.contains(scanResult)) {
Log.w(LOGTAG, "Blocked BSSID: " + scanResult);
return false;
}
if (SSIDBlockList.contains(scanResult)) {
Log.w(LOGTAG, "Blocked SSID: " + scanResult);
return false;
}
return true;
}
private WifiManager getWifiManager() {
return (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
}
private void reportScanResults(ArrayList<ScanResult> scanResults) {
if (scanResults.isEmpty()) return;
Intent i = new Intent(ScannerService.MESSAGE_TOPIC);
i.putExtra(Intent.EXTRA_SUBJECT, WIFI_SCANNER_EXTRA_SUBJECT);
i.putParcelableArrayListExtra(WIFI_SCANNER_ARG_SCAN_RESULTS, scanResults);
i.putExtra("time", System.currentTimeMillis());
mContext.sendBroadcast(i);
}
}