Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a foreground service for reliable background transmitting #4347

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

dshokouhi
Copy link
Member

Summary

Hopefully fixes: #3159 by implementing a similar foreground service like in #3369 based on the docs to create a "fake" scanner that looks for nothing to help keep the transmitter alive.

Needs end user testing. 🤞

Screenshots

image

image

Link to pull request in Documentation repository

Documentation: home-assistant/companion.home-assistant#pending

Any other notes

Code may not look pretty for now but wanted to get end user feedback

@jpelgrom
Copy link
Member

Maybe it's good to split the Bluetooth scan permission into its own PR which we can quickly approve+merge as that fixes an obvious oversight/bug, whereas the foreground service is an effort to improve things but also introduces new behavior?

@dshokouhi
Copy link
Member Author

Maybe it's good to split the Bluetooth scan permission into its own PR which we can quickly approve+merge as that fixes an obvious oversight/bug, whereas the foreground service is an effort to improve things but also introduces new behavior?

actually the scan permission is required in this PR because we are now using the scanner in this sensor where we were not before

@jpelgrom
Copy link
Member

I missed that's new, sorry. In that case never mind and let's wait for positive results.

@dshokouhi
Copy link
Member Author

Agreed this requires end users to confirm its working for them as I cannot reproduce

@dshokouhi dshokouhi marked this pull request as draft April 19, 2024 02:55
@dshokouhi dshokouhi force-pushed the foreground_service_ble_transmitter branch from 50ee744 to bfe0c9c Compare June 14, 2024 16:26
@dshokouhi dshokouhi marked this pull request as ready for review June 14, 2024 16:34
@dshokouhi
Copy link
Member Author

May have taken some time but we got end user confirmation :) probably want to keep this in beta for a lil bit to let it soak

this is now ready for review!

Copy link
Member

@jpelgrom jpelgrom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really able to test this myself but user comment sounds promising, and I don't see it causing any harm at the moment.

Edit: actually, have you tested this when scanning and transmitting is enabled? Do these changes interfere with regular scanning?

Comment on lines +72 to +79
beaconManager?.enableForegroundServiceScanning(builder.build(), 445)
beaconManager?.setEnableScheduledScanJobs(false)
beaconManager?.beaconParsers?.clear()
beaconManager?.backgroundBetweenScanPeriod = Long.MAX_VALUE
beaconManager?.backgroundScanPeriod = 0
beaconManager?.foregroundBetweenScanPeriod = Long.MAX_VALUE
beaconManager?.foregroundScanPeriod = 0
beaconManager?.startMonitoring(region)
Copy link
Member

@jpelgrom jpelgrom Jun 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be good to add a comment here explaining this enables a foreground service to improve reliability, so that one doesn't need to blame the code to find out why we are monitoring in a transmitting service.

Minor cleanup so you don't need beaconManager?. everywhere:

beaconManager?.apply {
    enableForegroundServiceScanning(builder.build(), 445)
    setEnableScheduledScanJobs(false)
    // etc
}

@dshokouhi
Copy link
Member Author

dshokouhi commented Jun 16, 2024

Edit: actually, have you tested this when scanning and transmitting is enabled? Do these changes interfere with regular scanning?

I thought I did that test before but now it seems that only one foreground service can be up at a time 🤔 not sure what to do here. I dont think we can share them either.

2024-06-15 17:39:46.481 18736-18736 SensorDetailViewModel   io....stant.companion.android.debug  E  Exception while requesting update for sensor ble_emitter (Ask Gemini)
                                                                                                    java.lang.IllegalStateException: May not be called after consumers are already bound.
                                                                                                    	at org.altbeacon.beacon.BeaconManager.enableForegroundServiceScanning(BeaconManager.java:1792)
                                                                                                    	at io.homeassistant.companion.android.common.bluetooth.ble.TransmitterManager.startTransmitting(TransmitterManager.kt:76)
                                                                                                    	at io.homeassistant.companion.android.common.sensors.BluetoothSensorManager.updateBLESensor(BluetoothSensorManager.kt:360)
                                                                                                    	at io.homeassistant.companion.android.common.sensors.BluetoothSensorManager.requestSensorUpdate(BluetoothSensorManager.kt:198)
                                                                                                    	at io.homeassistant.companion.android.settings.sensor.SensorDetailViewModel$setEnabled$2.invokeSuspend(SensorDetailViewModel.kt:217)
                                                                                                    	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
                                                                                                    	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:104)
                                                                                                    	at android.os.Handler.handleCallback(Handler.java:959)
                                                                                                    	at android.os.Handler.dispatchMessage(Handler.java:100)
                                                                                                    	at android.os.Looper.loopOnce(Looper.java:232)
                                                                                                    	at android.os.Looper.loop(Looper.java:317)
                                                                                                    	at android.app.ActivityThread.main(ActivityThread.java:8592)
                                                                                                    	at java.lang.reflect.Method.invoke(Native Method)
                                                                                                    	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:580)
                                                                                                    	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:878)

@dshokouhi
Copy link
Member Author

putting this back in draft while considering new changes based on the latest test comment

@dshokouhi dshokouhi marked this pull request as draft June 18, 2024 19:31
@dshokouhi
Copy link
Member Author

Based on the most recent comment I am thinking of maybe updating our docs and sensor string description to mention the beacon monitor being enabled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Android iBeacon transmission stops shortly after started
2 participants