ostrya / PresencePublisher

An Android MQTT client that regularly publishes messages to notify about the device's presence
MIT License
79 stars 12 forks source link

Publish Presence on Wifi dis-/connect besides the time interval #60

Closed Flunkyball closed 2 weeks ago

Flunkyball commented 1 year ago

Hello,

I recently setup presence publisher on my phone together with a mqtt broker reachable from the internet. Despite everything working very well I was a little surprised that the app relies only on the given time interval to check if is connected with a certain wifi etc. My expectation would be that the mqtt message is sent immediately of e.g. a given wifi is connected or disconnected.

Is there room for improvement ?

ostrya commented 1 year ago

The issue with any such extra logic is that it all depends on how much battery optimization is going on. To save battery, Android may just decide to disable WiFi and only check once every 30 minutes or so. So while the technical possibility exists to react immediately on network changes, it does not work very reliably. I had originally built the app like that, but with increasing Android versions, the options to get this to work in the background without the app being eventually stopped just got less and less reliable. At some point, I just switched to the WorkManager library which is supposed to work well with standard battery saving efforts of Android. This of course comes with the limitation that immediate action becomes impossible.

If you're running on Android 8 or below, you could try to run an older version of the app (e.g. version 2.3.2) to see if that works any better.

Another alternative which I currently will not take because it would mean including non-FOSS software would be to use Google's geo-fencing API which are part of GMS (see https://developer.android.com/training/location/geofencing).

So all in all, I currently see no good way to improve the situation.

Flunkyball commented 1 year ago

Thank you for the explanation.

Iam not sure how e.g. tasker does manage it but there network detection seems to be pretty reliable on all Android versions. Maybe it would be an option to extend PresencePublisher with an API compatible to Tasker ? Then Tasker would be able to trigger to event.

Good to hear that you try to avoid GMS especially that this would not solve my use case.

Usually my phone (LOS 19.1 on Galaxy S20) gets connected to my home wifi a few meters in front of my house. If the presence would be triggered then I could switch on the lights etc. while entering the flat.