mjaakko / NeoStumbler

New stumbler application for contributing data to geolocation services
MIT License
125 stars 11 forks source link

Wifi near scanning doesn't work when screen is off #195

Open lnd-dl opened 2 months ago

lnd-dl commented 2 months ago

Description

When I use this app, the wifi scanning works only when my screen is on or for a little bit afterwards. Cell tower scanning works fine. When I used Debug Stumbler for MLS this didn't happen. I could scan for wifi even when in background. When I compare the permissions of the two apps, Debug stumbler has the right to read internal storage nothing else. I disabled "ignore wifi-scan throttling", this works fine as long as the app is on the screen.

App variant

full

Version

latest

Device

Huawei P40

Android version

Emui 12 (Android 11)

Logs

No response

New bug

mjaakko commented 2 months ago

Do you have battery optimizations disabled? NeoStumbler prompts to do this when starting scanning for the first time

lnd-dl commented 2 months ago

Yes, I have. Cell Towers are scanned fine in background while screen is off.

mjaakko commented 2 months ago

Alright. By Debug Stumbler, do you mean this: https://github.com/mozilla/DemoStumbler?

lnd-dl commented 2 months ago

I think I got it from here: https://github.com/GerryFerdinandus/MozStumbler/releases/tag/V1.8.8_android_10

AtmosphericIgnition commented 2 months ago

Could be some weireness Huawei devices are doing to save battery. Check out the info here for some troubleshooting steps.

lnd-d commented 2 months ago

Could be, I guess it is related to Huawei if it works for others. I use this as a workaround: https://github.com/JonasBernard/FakeStandby But it is a not a good solution.

rmh2022 commented 1 month ago

Hi

I can reproduce this consistently with non-Huawei phone (mine is a Fairphone 3 running DivestOS 20-20240907-dos-FP3).

During my test I was in the street surrounded by tall buildings, and I was barely moving so these readings with 0 APs are not feasible.

Also note the problem doesn't seem to affect GSM or Bluetooth.

P.S. NeoStumbler version is 1.3.1 (fdroid)

neostumbler_screen_onoff_test

AtmosphericIgnition commented 1 month ago

I'm on the latest RC, and I have observed 10s of "empty" reports in a row when sitting outside with a clear view of the sky. Can provide screenshots if needed.

rmh2022 commented 1 month ago

Hi The problem is related to Wifi scan throttling. In my initial test I had Wifi scan throttling disabled since this is the recommended setting. Now I re-enabled it in Developer Settings, then unchecked the "Ignore Wi-Fi scan throttling" setting in the app, and WiFi detection is working even with screen turned off.

I notice that every few reports there's one with 0 Wifi reads, I assume this is normal as it is an expected effect of Wifi scan throttling which is now active.

Screenshot_20240921-200201_NeoStumbler

rmh2022 commented 1 month ago

I'm on the latest RC, and I have observed 10s of "empty" reports in a row when sitting outside with a clear view of the sky. Can provide screenshots if needed.

Do you have Wifi scan throttling enabled? I would guess this is the normal result in that case. Unless you can reproduce the OP problem, I wouldn't attach these screenshots since it would just add noise due to unrelated info.

mjaakko commented 1 month ago

Interesting observation @rmh2022. Not sure what is happening there, the only thing that's done on NeoStumbler side regarding Wi-Fi scan throttling is whether to do Wi-Fi scans every 10 seconds or every 30 seconds. I'm guessing this might be some weird battery optimization bug in the OS

I notice that every few reports there's one with 0 Wifi reads, I assume this is normal as it is an expected effect of Wifi scan throttling which is now active.

Yes, that's expected

rmh2022 commented 1 month ago

Strangely enough, I disabled again Wifi scan throttling in Developer settings, and now the problem is not reproducible. Then I re-checked "Ignore Wi-Fi scan throttling" setting in the app, still not reproducible. I can't make sense of this, it might still be related to throttling in combination with another factor (race condition?) but I'm unsure now.

For anyone else experiencing this issue, I would suggest to check if playing with the throttling options makes it disappear, as I'm not really sure if it is related or not.

P.S. When I first reproduced the problem in 2024-09-19, I was in the street, not connected to any Wifi, whereas all the tests I did today I was at home, connected to my Wifi router. It might be related to this. I'll check again next time I go out.

rmh2022 commented 1 month ago

P.S. When I first reproduced the problem in 2024-09-19, I was in the street, not connected to any Wifi, whereas all the tests I did today I was at home, connected to my Wifi router. It might be related to this. I'll check again next time I go out.

Yup, that was my mistake indeed. Please accept my apologies for sending contradicting info. During the tests I did yesterday I neglected another factor which differed from my 2024-09-19 test: I was at home and my phone was connected to local Wifi router. I've since done several tests, both inside and outdoors, and with different throttling settings, just to be sure.

The problem consistently appears when (and only when) these two factors are present:

I've verified the following factors to be irrelevant:

Also note in this post I'm specifically referring to the problem where ALL reports list 0 Wifi reads. I have occasionally also experimented a different issue where occasionally there are some reports with 0 Wifi reads (even when throttling is turned off) but for the purpose of this experiment I'm completely ignoring those. Whenever I say in this post that problem manifests, I'm referring to 100% of the reports being affected.

FTR, this is a Fairphone 3 running DivestOS 20-20240907-dos-FP3 with NeoStumbler version is 1.3.1 (fdroid). Instead of GSF I'm running microG Services 0.3.2.240913 (as unprivileged app).

mjaakko commented 1 month ago

Great analysis @rmh2022. I think I found the problem: newer Android versions don't allow apps to keep the Wi-Fi on (nor to programmatically turn it on). It seems that some (most?) devices turn off their Wi-Fi when the screen is off and thus Wi-Fi scanning won't work, unless "Wi-Fi scan always available" has been enabled in the device settings. Devices with Google Play Services might have that enabled by default, because Google Play Services uses that for the fused location provider

Does your device have that option available in the settings? If not, the only workaround I can think of is to add support for keeping the screen on in NeoStumbler while the scanning service is active

AtmosphericIgnition commented 1 month ago

I tried searching the settings on GrapheneOS, and I could not find any setting like that even inside developer options.

rmh2022 commented 1 month ago

@mjaakko Glad to assist. I'm afraid I can't find this "Wi-Fi scan always available" setting. Perhaps DivestOS and other alternate OS developers would be amenable to enable/expose it, specially those that rely on microG and by extension the non-GSF location ecosystem that NeoStumbler is contributing to.

OTOH the workaround you propose seems useful. In fact I've already achieved the same effect using Caffeinate, but having this functionality integrated in the same app would make things easier to manage (one less app to interact with).

Thanks for your time and dedication.

mjaakko commented 1 month ago

Yeah, it would be better for custom ROMs to add that option to the settings, because there are valid use cases for it. I wonder what would happen if an activity was started with the intent to make Wi-Fi scan always available: https://developer.android.com/reference/android/net/wifi/WifiManager#ACTION_REQUEST_SCAN_ALWAYS_AVAILABLE

I'll do #156 first and add an option to try to enable Wi-Fi scanning always available there. If that won't work, I'll add ability to keep the screen on while scanning

lnd-d commented 1 month ago

@mjaakko Glad to assist. I'm afraid I can't find this "Wi-Fi scan always available" setting. Perhaps DivestOS and other alternate OS developers would be amenable to enable/expose it, specially those that rely on microG and by extension the non-GSF location ecosystem that NeoStumbler is contributing to.

OTOH the workaround you propose seems useful. In fact I've already achieved the same effect using Caffeinate, but having this functionality integrated in the same app would make things easier to manage (one less app to interact with).

Thanks for your time and dedication.

Wow, thanks for the recommendation. Caffeinate together with always on display makes my Huawei device scan Wi-Fi without any problems, even when the screen is locked, for as long as I want it to or the battery will allow it (it drains quite fast). This is much better than 'fake standby' or leaving the screen on all the time, which isn't very practical when you're carrying it around in your pocket.