adolfintel / OpenPods

The Free and Open Source app for monitoring your AirPods on Android
https://fdossena.com/?p=openPods/index.frag
GNU General Public License v3.0
938 stars 161 forks source link

scanning too much #27

Closed AeliusSaionji closed 4 years ago

AeliusSaionji commented 4 years ago

Hello!

I've recently become aware that this app is preventing my phone from entering deep sleep. It's issuing a wakelock anywhere from 15 to 40 times a minute, all the time. I was probably the one who requested the feature that forced you to do that :smile:

Is there any way you can stop the scanning unless it's a) connected or b) manually requested?

Thanks!

adolfintel commented 4 years ago

I don't think it's possible: the app needs to receive bluetooth events and BLE beacons. I haven't noticed any difference in battery life though.

I might be able to make the BLE scanner smarter, but manual activation of the scanner is out of the question, it would make the application pretty useless.

AeliusSaionji commented 4 years ago

I discovered this while investigating battery drain issues :/ My "screen off" idle drain is 2-4%/hour with OpenPods, and 0.5%/hour with OpenPods disabled.

The culprit as revealed by BetterBatteryStats was Bluetooth repeatedly doing a "gatt" scan, which also kept the CPU in higher clock speeds. Thus my phone is never truly sleeping.

adolfintel commented 4 years ago

Try this APK: app-debug.zip

This only starts the scanner when it detects the connection event. I don't think it's reliable though, especially because I noticed that sometimes the ACL_CONNECTED and ACL_DISCONNECTED events are not sent in the correct order.

AeliusSaionji commented 4 years ago

Thanks, I'll let you know the results in a day or two!

AeliusSaionji commented 4 years ago

Yep, before connecting to the airpods, there was no bluetooth activity. After connecting then disconnecting a few times, OpenPods was right back to scanning when the airpods were disconnected. Furthermore, I observed that while airpods are connected and the screen is off, OpenPods is still hitting bluetooth.

Here's a thought: can you pause any and all app functions while the screen is off?

adolfintel commented 4 years ago

It's exactly what I expected, shit and unreliable, like most android APIs. Yea I could try pausing the scanner when the screen is off, I'll see if that works

adolfintel commented 4 years ago

I tried repeating what you did but it seems that on my phone the bluetooth scanner does NOT run when the screen is off.

What phone and OS are you running?

AeliusSaionji commented 4 years ago

Android Pie, basically aosp

AeliusSaionji commented 4 years ago

Do you have BetterBatteryStats?

Looking at the Alarms view is the fastest way for me to see if OpenPods is running. There are 0 bt alarms otherwise.

adolfintel commented 4 years ago

I'm using LineageOS 16 which should be pretty close to AOSP. The BLE scanner stops working after a few minutes with the screen turned off.

adolfintel commented 4 years ago

By the way the Android documentation says that the BLE scanner is supposed to stop scanning with the screen off, as I observed. I'm not sure why it's not doing it on your phone. I will try to investigate it though.

AeliusSaionji commented 4 years ago

I have the razer phone 2, could be razer messed that up ¯_(ツ)_/¯

The rom is pretty close to aosp, though.

adolfintel commented 4 years ago

Here's another test APK for you: app-debug.zip

This one tries to turn off the scanner when the screen is turned off. The screen on/off events work, but the stopScan method of the BLE scanner doesn't seem to do jack shit on my phone (android still decides to stop scanning after a few minutes of screen off).

Let me know if it's better for you.

By the way, do you have 1st gen or 2nd gen AirPods?

AeliusSaionji commented 4 years ago

1st gen.

Thanks, installed, will get back to you later!

AeliusSaionji commented 4 years ago

Preliminary impression: what black magic did you use to create this? It's more responsive than it ever was about showing the notification, and it's not using the bluetooth radio at all as far as BBS can see. What's the catch? 😆

adolfintel commented 4 years ago

I didn't do anything, I just added that screen on/off listener. Funny enough, this version does NOT stop scanning on my phone when I turn off the screen, only when android decides to.

Just as a sanity test, I reinstalled the Alpha 9 version currently on F-Droid and kept bluetooth turned on for the last 11 hours. I asked my friend to do the same (he has a samsung stock ROM), and we both got around 2% battery usage from bluetooth, confirming what the android documentation says, no scanning is performed when the screen is off. I can only conclude that your problem must be a bug with razer's ROM.

I could add a switch in the settings to toggle this mode, I have to think about it.

AeliusSaionji commented 4 years ago

Well, I can confirm OpenPods is working as expected and my phone is down to an idle drain of -0.7%/hr. Whatever the case, this is the version that is working for me!

adolfintel commented 4 years ago

I decided to push these changes to master and see if anyone complains; if it happens, I'll go back to Alpha 9.

You can get the updated APK from here: https://downloads.fdossena.com/geth.php?r=openpods-apk

adolfintel commented 4 years ago

I pulled Alpha 10, sorry :laughing:

I want to add a settings menu where you can toggle this, because Alpha 9 worked better on my phone, and I suspect that it works better for most people too. I'll re-release Alpha 10 tomorrow.

AeliusSaionji commented 4 years ago

How does it work better for you? Notification pops up faster?

adolfintel commented 4 years ago

With Alpha 10 sometimes it takes like 30 seconds before it notices that airpods have been connected/disconnected

adolfintel commented 4 years ago

Alpha 10 is up again at the previous link. You can enable the battery saving in the settings. Let me know if it works for you.

AeliusSaionji commented 4 years ago

Nope, the power saving setting is not power saving for me!

adolfintel commented 4 years ago

That's weird, it's the same code as yesterday. Are you sure it isn't working?

AeliusSaionji commented 4 years ago

I did not actually install the first alpha10 at that url, I only used the app-debug from 2 days ago. With that one, there were zero bluetooth "alarms" in betterbatterystats.

The alpha10, even with that setting checked, is back at it.

adolfintel commented 4 years ago

Maybe it needs to be restarted to release those wakelocks? I'm really running out of ideas here

AeliusSaionji commented 4 years ago

Yeah a restart of the app seems to have fixed it, thanks.

adolfintel commented 4 years ago

Did you restart the app by swiping it from task manager or by force stopping it?

AeliusSaionji commented 4 years ago

I force stopped it from android

Tanddant commented 3 years ago

This issue is really bad on Android 11 - both on my Pixel 5 and Pixel 2 xl.

On a 10 seconds logcat dumb (19:40:37.370 -19:40:47.369 to be exact) I have 1571 "BtGatt.ScanManager: awakened up at time XXXXXX" - That's over 100 times/second.

And that causes my phone to never go to deep sleep, thus causing it drain at ~10% battery/hour.

Oddly enough the problem is pressent even after uninstalling the app, and only goes away after a reboot.

adolfintel commented 3 years ago

Damn, that's really bad. I don't have android 11 yet to test it though.

I definitely don't scan that much, I just start one scanner and restart it if it dies. Can you put a log print here? https://github.com/adolfintel/OpenPods/blob/ae5a1ab119ac1e88ed2710a50b7cc51657bd322f/OpenPods/app/src/main/java/com/dosse/airpods/PodsService.java#L119

It should only be called once or twice.

Tanddant commented 3 years ago

While I've never done android development I got it up and running and did a log there is only printed once, but after a adding a few more logs, it seems to be the onBatchScanResults method that's called VERY frequently:

image

adolfintel commented 3 years ago

Does it change anything if you enable battery saver in the app settings?

Tanddant commented 3 years ago

That stops it stops the onBatchScanResults and BtGatt spam.

but also causes there to be no notification when my airpods connect.

adolfintel commented 3 years ago

Actually it's supposed to start/stop the scanner when the screen is turned on/off. Can you see that event in the logcat?

Tanddant commented 3 years ago

I see those log events, also seems to log when I connect my Airpods, just no notification.

image

Tanddant commented 3 years ago

It still detects connections even with screen off.

image

adolfintel commented 3 years ago

It doesn't receive anything, otherwise you'd see the data coming from the airpods. So basically both modes of BLE scanning are broken: regular scanning rapes the battery, slow scanning doesn't work at all. Brilliant

adolfintel commented 3 years ago

I'll create an issue about Android 11 compatibility, maybe others can provide insights

Tanddant commented 3 years ago

Using regular scanning I do get some data on connection:

image

Tanddant commented 3 years ago

Cool - Let me know if I can provide anything else.

adolfintel commented 3 years ago

Your device has a qualcomm soc, right?

Tanddant commented 3 years ago

Correct - for a bit more info I'm seeing it on both a Pixel 5 (765G) and Pixel 2 xl (835)

f1vefour commented 2 years ago

It still does this on a Pixel 2 XL and Pixel 4 XL.

I thought something was wrong with Bluetooth on the 2 XL for months and then I got the 4 XL and experienced the same exact issue, I had to leave Bluetooth off all the time to prevent the insane battery drain. Finally I figured out it was openpods causing the issue and uninstalled it.

I've been using materialpods and it doesn't drain the battery at all, not sure how it works differently but it certainly does.