home-assistant / android

:iphone: Home Assistant Companion for Android
https://companion.home-assistant.io/
Apache License 2.0
2.24k stars 627 forks source link

Wear OS wont update Heart Rate Sensor after arround 30 minutes #4584

Closed GammaPotato closed 3 weeks ago

GammaPotato commented 3 weeks ago

Android version(s):

Samsung Galaxy Watch 6 Classic (SM-R960)
One-UI: 5.0 Watch 
System Version: 13
Software Version: R960XXU1AXC2

Samsung Galaxy S24 Ultra (SM-S928B/DS)

One-UI: 6.1
Android Version: 14
Basebandversion: S928BXXS3AXGF

Home Assistant version:

2024.8.1      Core
2024.08.0     Supervirsor
13.0          Operating System
20240809.0    Fronted

Description of problem, include YAML if issue is related to notifications:

For Some reason The Wear OS App dont send heart rate Sensor after arround 30 minutes when the screen is off. All Other Sensors gets updated but not the heart rate sensor. i Turned off the "Adaptive Battery" and "Put apps to sleep". Also "remove permissions when background" is off too.

to Fix that i have to Activate the screen and open the HA App on the watch. and minimize it again. The sensor gets update every 1 to 3 minutes for next 30 minutes. After that. Same problem. Also Generally no Sensor updates when i close the app on Task Manager.

is it possible to add it in to the Background services? HA on watch is not showen to run in background. Watch OS has a own feature to check that. and HA on watch stays in normal Task manger and not in Background services.

Companion App Logs:

homeassistant_companion_log_7-16-2024_21-34-14.txt

Text is in German. Im from Austria Screenshots watch

dshokouhi commented 3 weeks ago

as you have a samsung watch you need to open the samsung companion app on your phone and grant the HA app background access

dshokouhi commented 3 weeks ago

also make sure the app has access to body sensors all teh time wehn you go to the permission screen.

Also as an FYI the heart rate sensor will be going away soon due to google policy changes: https://github.com/home-assistant/android/pull/4552

GammaPotato commented 3 weeks ago

Both apps have background access. And all persmissions are granted and no optimation are on.

Why remove it? Just build in the ability for the app to run in the background, like in my screenshot. I have seen many apps that can run in the background as long as they want when they have this function.

dshokouhi commented 3 weeks ago

I have seen many apps that can run in the background as long as they want when they have this function.

per the upcoming policy change teh only way for an app to be approved to run in the background for those sensors is on a case by case basis with each manufacturer

https://developer.android.com/about/versions/13/behavior-changes-13#body-sensors-background-permission

Note: This is a "hard-restricted" permission that cannot be held by an app until the device's installer allowlists the permission for your app.

dshokouhi commented 3 weeks ago

unfotunately your logs are from the phone and not the watch so we wont be able to see if there is an error from the sensor, did you check the permission for the app to ensure its granted all the time? This is for the sensors permission

GammaPotato commented 3 weeks ago

There is no button to find the logs on watch

And yes. Premissons for background are granted. Body sensors background to. ALL permissions granted for everytime.

dshokouhi commented 3 weeks ago

There is no button to find the logs on watch

yes there is no easy way to get logs off the watch, the only way is to connect to android studio

My heart rate sensor updates all the time without issue

image

also our sensor worker does indeed run in the background as evidenced by all other sensors updating so its possible there is an error but I am not sure at this point. The symptoms sound like a permission or background access issue. Logs may tell us whats happening.

GammaPotato commented 3 weeks ago

im connected to android studio. can u Tell me how to get the HA logs ?

dshokouhi commented 3 weeks ago

ok you want to open the Logcat window and make sure your device is selected in the drop down. In the search panel you can type in HRSensor and we should see some log lines like:

https://github.com/home-assistant/android/blob/master/wear/src/main/java/io/homeassistant/companion/android/sensors/HeartRateSensorManager.kt#L109

or

https://github.com/home-assistant/android/blob/master/wear/src/main/java/io/homeassistant/companion/android/sensors/HeartRateSensorManager.kt#L111

that will tell us if were getting data or not

youll need to reproduce the issue as well which may not be as easy when hooked up to the debugger.

if you see log lines with values changing then we need to look at the time of occurrence in teh logs and grab all the logs at the time the value was supposed to change. Again this will only be helpful if hte issue is reproduced and we can see that in teh logs

GammaPotato commented 3 weeks ago

i have the live logs and i try to get that issue. the first thing what i see is this here

2024-08-16 22:50:26.858 16285-16467 HRSensor io.homeassistant.companion.android D Re-registering listener as it appears to be stuck

dshokouhi commented 3 weeks ago

that log line means we didnt get any HR events for 1 minute and we are re-registering to get more updates, if that is all you see then that means the device is not telling the app it has updates and there is not much we can do there in those cases.

Are you seeing it stop completely or just infrequent updates? Does it stop for days at a time?

GammaPotato commented 3 weeks ago

Got this log. Some errors are shown, and the updates stop randomly. After a couple of hours, there may be updates for up to 10 minutes, and then nothing for hours. In this log, I haven’t received any updates from the heart rate (HR) sensor since the beginning.

The Samsung Health app shows the HR without missing any data; I can see the data for the entire day.

HA Watch Logs.txt chrome_n44d8dFvZ9

GammaPotato commented 3 weeks ago

Oh and since start of the log no HR Sensor data got updated.

2024-08-16 23:06:13.824 16285-16285 WM-Processor            io.homeassistant.companion.android   E  Unable to stop foreground service
                                                                                                    android.app.BackgroundServiceStartNotAllowedException: Not allowed to start service Intent { act=ACTION_STOP_FOREGROUND cmp=io.homeassistant.companion.android/androidx.work.impl.foreground.SystemForegroundService }: app is in background uid UidRecord{bd3e65a u0a178 TRNB bg:+18m30s137ms idle change:procadj procs:0 seq(26999662,26987762)}
                                                                                                        at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1913)
                                                                                                        at android.app.ContextImpl.startService(ContextImpl.java:1869)
                                                                                                        at android.content.ContextWrapper.startService(ContextWrapper.java:822)
                                                                                                        at androidx.work.impl.Processor.stopForegroundService(Processor.java:403)
                                                                                                        at androidx.work.impl.Processor.cleanUpWorkerUnsafe(Processor.java:425)
                                                                                                        at androidx.work.impl.Processor.onExecuted(Processor.java:345)
                                                                                                        at androidx.work.impl.Processor.lambda$startWork$1$androidx-work-impl-Processor(Processor.java:179)
                                                                                                        at androidx.work.impl.Processor$$ExternalSyntheticLambda2.run(D8$$SyntheticClass:0)
                                                                                                        at android.os.Handler.handleCallback(Handler.java:942)
                                                                                                        at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                                        at android.os.Looper.loopOnce(Looper.java:201)
                                                                                                        at android.os.Looper.loop(Looper.java:288)
                                                                                                        at android.app.ActivityThread.main(ActivityThread.java:7962)
                                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:550)
                                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:952)
dshokouhi commented 3 weeks ago

i dont see any heart rate data coming into the app at all, and you double checked the Sensors permission for the app. Outside of that and the setting from teh samsung companion watch app on the phone. I am afraid your device is simply not reporting to the app that it has data during these intervals. Which still feels like its being blocked by background activity in some way because our code is not reachable at times.

GammaPotato commented 3 weeks ago

I told ya it has all permissions...

      com.google.android.c2dm.permission.RECEIVE: granted=true
      android.permission.FOREGROUND_SERVICE: granted=true
      android.permission.RECEIVE_BOOT_COMPLETED: granted=true
      android.permission.INTERNET: granted=true
      android.permission.ACCESS_NETWORK_STATE: granted=true
      io.homeassistant.companion.android.DYNAMIC_RECEIVER_NOT_EXPORTED_PERMISSION: granted=true
      android.permission.VIBRATE: granted=true
      android.permission.ACCESS_WIFI_STATE: granted=true
      android.permission.WAKE_LOCK: granted=true
        android.permission.POST_NOTIFICATIONS: granted=true, flags=[ USER_SET|USER_SENSITIVE_WHEN_GRANTED|USER_SENSITIVE_WHEN_DENIED]
        android.permission.ACCESS_FINE_LOCATION: granted=true, flags=[ USER_SET|USER_SENSITIVE_WHEN_GRANTED|USER_SENSITIVE_WHEN_DENIED]
        android.permission.BODY_SENSORS: granted=true, flags=[ USER_SET|USER_SENSITIVE_WHEN_GRANTED|USER_SENSITIVE_WHEN_DENIED]
        android.permission.BLUETOOTH_CONNECT: granted=true, flags=[ USER_SET|USER_SENSITIVE_WHEN_GRANTED|USER_SENSITIVE_WHEN_DENIED]
        android.permission.ACCESS_COARSE_LOCATION: granted=true, flags=[ USER_SET|USER_SENSITIVE_WHEN_GRANTED|USER_SENSITIVE_WHEN_DENIED]
        android.permission.READ_PHONE_STATE: granted=true, flags=[ USER_SET|USER_SENSITIVE_WHEN_GRANTED|USER_SENSITIVE_WHEN_DENIED]
        android.permission.BODY_SENSORS_BACKGROUND: granted=true, flags=[ USER_SET|REVOKE_WHEN_REQUESTED|USER_SENSITIVE_WHEN_GRANTED|USER_SENSITIVE_WHEN_DENIED|RESTRICTION_INSTALLER_EXEMPT]
        android.permission.BLUETOOTH_ADVERTISE: granted=true, flags=[ USER_SET|USER_SENSITIVE_WHEN_GRANTED|USER_SENSITIVE_WHEN_DENIED]
        com.google.android.clockwork.settings.WATCH_TOUCH: granted=true, flags=[ USER_SET]
        android.permission.ACTIVITY_RECOGNITION: granted=true, flags=[ USER_SET|USER_SENSITIVE_WHEN_GRANTED|USER_SENSITIVE_WHEN_DENIED]
        android.permission.RECORD_AUDIO: granted=true, flags=[ USER_SET|USER_SENSITIVE_WHEN_GRANTED|USER_SENSITIVE_WHEN_DENIED]
        android.permission.ACCESS_BACKGROUND_LOCATION: granted=true, flags=[ USER_SET|USER_SENSITIVE_WHEN_GRANTED|USER_SENSITIVE_WHEN_DENIED|RESTRICTION_INSTALLER_EXEMPT]
        android.permission.BLUETOOTH_SCAN: granted=true, flags=[ USER_SET|USER_SENSITIVE_WHEN_GRANTED|USER_SENSITIVE_WHEN_DENIED]
2024-08-16 23:08:43.763 16285-17353 SensorReceiver          io.homeassistant.companion.android   E  Error while getting core config to sync sensor status
2024-08-16 23:08:43.766 16285-16467 SensorReceiver          io.homeassistant.companion.android   E  Error while getting core config to sync sensor status
2024-08-16 23:08:44.096 16285-17353 SensorReceiver          io.homeassistant.companion.android   W  Exception while updating sensors: IntegrationException: java.net.UnknownHostException
2024-08-16 23:08:44.097 16285-17438 SensorReceiver          io.homeassistant.companion.android   I  Sensor updates and sync completed
dshokouhi commented 3 weeks ago

I told ya it has all permissions...

unfortunately it does not tell us if its granted all the time

android.permission.BODY_SENSORS: granted=true, flags=[ USER_SET|USER_SENSITIVE_WHEN_GRANTED|USER_SENSITIVE_WHEN_DENIED]

image

the other logs are meaningless if you cant confirm the HRSensor line getting actual data to send

GammaPotato commented 3 weeks ago

Permission is set

Screenshot_20240816_231850_permissioncontroller.png

GammaPotato commented 3 weeks ago

Got this in log right now but sensor got no updates. still no update since 33 Minutes

2024-08-16 23:20:05.196 16285-18669 HRSensor                io.homeassistant.companion.android   D  Re-registering listener as it appears to be stuck
2024-08-16 23:20:05.198 16285-18669 SensorManager           io.homeassistant.companion.android   D  unregisterListener :: io.homeassistant.companion.android.sensors.HeartRateSensorManager@bb71f47
2024-08-16 23:20:05.200 16285-18669 SensorManager           io.homeassistant.companion.android   D  registerListener :: 83, Samsung HR None Wakeup Sensor, 200000, 0, io.homeassistant.companion.android.sensors.HeartRateSensorManager@bb71f47
2024-08-16 23:20:05.209 16285-18669 HRSensor                io.homeassistant.companion.android   D  Heart Rate sensor listener registered
GammaPotato commented 3 weeks ago

this moment all sensors got randomly updated. i did nothing. this is the log while this happend. i didnt understand everything but i think there got something killed?

HA Watch Logs 2.txt

dshokouhi commented 3 weeks ago

yes this last log did get an event to send back

2024-08-16 23:22:24.600 18741-18741 HRSensor io.homeassistant.companion.android D HR event received with accuracy: high and value: 69.0 with event count: 1

we can even see at times it gets an update after sensors were already updated which means the next update that gets sent to the server will contain the update

2024-08-16 23:23:00.710 18741-18785 SensorReceiver          io.homeassistant.companion.android   I  Sensor updates and sync completed
2024-08-16 23:23:00.914 18741-18741 HRSensor                io.homeassistant.companion.android   D  HR event received with accuracy: high and value: 68.0 with event count: 1

so in this case it looks like its working as expected. I suspect the issue is just the device not sending the app updates at certain time. Why I am not sure.

One thing i noticed in the OP Also Generally no Sensor updates when i close the app on Task Manager. Thats to be expected, closing the app means the work manager schedule will cease to exist. The app needs to be able to run in teh background at all times. We do not recommend closing the app.

GammaPotato commented 3 weeks ago

we can even see at times it gets an update after sensors were already updated which means the next update that gets sent to the server will contain the update

i did some changes to the android system with adb. i will tell ya tomorrow if that fixed it.

and i dont close the app. Whats the normal update rate for the HR sensor?

dshokouhi commented 3 weeks ago

Roughly every 15 minutes but it can vary, the sensor will also update anytime any other sensor that is push based updates like the interactive sensor.

https://companion.home-assistant.io/docs/wear-os/sensors

GammaPotato commented 3 weeks ago

the sensor will also update anytime any other sensor that is push

not here. all sensors get updates but only the HR sensor not while the others are updating

i test some changes now and try to send refresh commands from HA

chrome_tacmujcQ1p

dshokouhi commented 3 weeks ago

the sensor will also update anytime any other sensor that is push

not here. all sensors get updates but only the HR sensor not while the others are updating

Im explaining how frequently sensor updates are requested, in your case your sensor is simply not reporting any data which is unexpected. However as seen by the logs we are at least requesting it alongside all other sensors which is how it works.

GammaPotato commented 3 weeks ago

After couple hours of testing i have this result: Sensor Trigger and Updates

i set up some counter for:

SensorWorker
Screen off Trigger
Screen on Trigger
Sensor Update command (send per Notification all 5 minutes)
Hr Updates

while all other sensors gets updated, the HR sensor gets no updates. i think there might be a problem by accessing the HR while the app is in background. The hole day im missing a lot off Data but the Samsung health app has Data for the hole time. photo_2024-08-17_19-20-52

The Importance Sensor tells most time:

perceptible
cached
Top_sleeping

i think the problem is not on the Watch it self.

Sensor Triggers by counter since Today 00:25 AM. its 07:30 PM now chrome_DwjsAB7Urt

GammaPotato commented 3 weeks ago

3 days later. No Answer. Fixed by uninstalling HA from my Galaxy Watch 6. The app is not corecctly working for Samsung.

Test on Watch 4 and 5 too. Same problems.