Solution 1 :
Could you provide please more code or the some GitHub gist with more code, because there could be the issue with your broadcast receiver or with printing function.
By the way, you should also note, that there are lot of wifi scan limitations and your scan could be throttled and you will not get any new scan results.
Problem :
I’ve written an indoor localization algorithm, but for it to work, I need an application to log wifi and acceleration data continuously, while the user is walking around a building. In essence, every few hundred milliseconds, I need to log a “fingerprint”, which tracks user movement i.e. starting in room A, to room B, ending in room C. This helps in indentifying which rooms have doorways (basically, which rooms are connected).
Acceleration logging works fine, but I’m having some trouble with wifiManager.startScan()
. This is running on Android 9.
Specifically, I have the following code:
public WiFiScanner(Context context, boolean repeated, OnWiFiDataCallback wiFiDataCallback) {
super();
this.context = context;
this.repeated = repeated;
this.wiFiDataCallback = wiFiDataCallback;
wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
Log.d("WIFI-SCAN", "Starting a WiFiScanner");
if (!wifiManager.isWifiEnabled()) {
Toast.makeText(context, "WiFi not enabled", Toast.LENGTH_SHORT).show();
return;
}
context.registerReceiver(this, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
new CountDownTimer(500, 500) {
public void onTick(long millisUntilFinished) { /* do nothing */ }
public void onFinish() {
wifiManager.startScan();
Log.d("WIFI-SCAN", "Started a wifiManager.startScan()");
}
}.start();
}
@Override
public void onReceive(Context c, Intent intent) {
List<ScanResult> results = wifiManager.getScanResults();
Log.d("WIFI-SCAN", "Received wifi scan results");
WiFiScan scan = new WiFiScan(results);
// push data to receiver
wiFiDataCallback.onWiFiSample(scan);
if (repeated) {
new CountDownTimer(500, 500) {
public void onTick(long millisUntilFinished) { /* do nothing */ }
public void onFinish() {
wifiManager.startScan();
Log.d("WIFI-SCAN", "Started a wifiManager.startScan()");
}
}.start();
} else
stopScanning();
}
The first result is OK, but all other results I receive are just copies of the first one, regardless of my location in the building.
D/WIFI-SCAN: Started a wifiManager.startScan()
D/WIFI-SCAN: Received wifi scan results
D/STRING: data is <wr t="1591525068106">
<r b="84:16:f9:25:de:24" s="-35" c="36" />
<r b="84:16:f9:25:de:25" s="-44" c="3" />
<r b="86:16:f9:24:de:25" s="-44" c="3" />
<r b="74:da:38:a2:33:67" s="-69" c="2" />
<r b="80:8c:97:74:06:09" s="-73" c="9" />
<r b="50:c7:bf:51:e8:27" s="-76" c="11" />
<r b="98:da:c4:b8:01:8f" s="-80" c="5" />
<r b="80:8c:97:74:06:0a" s="-81" c="40" />
<r b="80:8c:97:2f:81:2b" s="-84" c="11" />
<r b="90:5c:44:25:1d:a4" s="-84" c="1" />
<r b="d8:32:14:e7:ea:51" s="-84" c="2" />
<r b="cc:2d:21:33:a9:d0" s="-84" c="11" />
<r b="54:67:51:07:11:73" s="-86" c="1" />
<r b="92:5c:14:25:1d:a4" s="-87" c="1" />
</wr>
D/WIFI-SCAN: Started a wifiManager.startScan()
D/WIFI-SCAN: Received wifi scan results
D/STRING: data is <wr t="1591525068634">
<r b="84:16:f9:25:de:24" s="-35" c="36" />
<r b="84:16:f9:25:de:25" s="-44" c="3" />
<r b="86:16:f9:24:de:25" s="-44" c="3" />
<r b="74:da:38:a2:33:67" s="-69" c="2" />
<r b="80:8c:97:74:06:09" s="-73" c="9" />
<r b="50:c7:bf:51:e8:27" s="-76" c="11" />
<r b="98:da:c4:b8:01:8f" s="-80" c="5" />
<r b="80:8c:97:74:06:0a" s="-81" c="40" />
<r b="80:8c:97:2f:81:2b" s="-84" c="11" />
<r b="90:5c:44:25:1d:a4" s="-84" c="1" />
<r b="d8:32:14:e7:ea:51" s="-84" c="2" />
<r b="cc:2d:21:33:a9:d0" s="-84" c="11" />
<r b="54:67:51:07:11:73" s="-86" c="1" />
<r b="92:5c:14:25:1d:a4" s="-87" c="1" />
</wr>
b
is bssid
, s
is signal strength
and c
is channel
.
I can’t figure out why this happens. I’ve added the delay you can see in the code, thinking it may be buffering scan results, or returning the same result since it never gets to finish another scan. I can sort of work around this by checking if the new result differs from the last one before logging it, but that gives me results which are sometimes 1 or 2 minutes apart, which defeats the purpose of continuous logging and tracking user movement from room to room.
I can see two reasons this occurs, but I’m not sure. Either I’m calling wifiManager.startScan()
too fast/too much, or Android wifi scanning is decoupled from wifiManager.startScan()
(i.e. OS does passive scanning, instead of active, as calling a scanning method might imply) and the actual scanning happens very slowly, and when called, it just returns whatever it has buffered.
Has anyone else had this issue? Is there a way to actually “force” it to perform a new scan when called?