JohanDegraeve / xdripswift

xdrip for iOS, written in Swift
GNU General Public License v3.0
317 stars 313 forks source link

CarPlay - Application takes over audio source of the car in follower mode #498

Closed viktorturda closed 3 months ago

viktorturda commented 5 months ago

Dear developers,

First of all thank you so much for your efforts making this great application!

I've recently switched from Libre2 to Libre3 and using the app in Follower mode with LibreLinkUp data source. I really like the feature that Apple CarPlay shows my current BG through the calendar. Unfortunately in follower mode it always "steals" the car's audio source so I can't use the app and listen to the radio of the car at the same time.

I've found in the code that to keep the application in the background it plays an audio in 5 secs intervals: https://github.com/JohanDegraeve/xdripswift/blob/3a6f4afe94e87a3eb928cbda67dcdf5dd5887cf7/xdrip/Managers/LibreLinkUp/LibreLinkUpFollowManager.swift#L771

Is there any other way to keep the app in background rather than playing audio?

Thanks in advance, Viktor

JohanDegraeve commented 5 months ago

There is another way. Can you build xDrip4iOS yourself or are you using another developer's testflight account? Because I don't have any Libre 3 right now. I could try to do the changes in a seperate branch.

viktorturda commented 5 months ago

Currently I’m using Shuggah from App Store but I can easily build and test it from a branch.

If you would like to test it too the same happens in nightscout follower mode: https://github.com/JohanDegraeve/xdripswift/blob/3a6f4afe94e87a3eb928cbda67dcdf5dd5887cf7/xdrip/Managers/NightScout/NightScoutFollowManager.swift#L368

Thanks so much for your quick response!

JohanDegraeve commented 5 months ago

For follower mode there's no other solution. Except if the follower would carry an additional bluetooth device (BLE) that regularly triggers the iPhone to wake it up. My wife's phone is following me using an M5Stack as bluetooth device that wakes up xDrip4iOS very 5 minutes. xDrip4iOS then fetches reading from NS which is then sent back to the M5Stack. But that works only at home, she doesn't carry the M5Stack all the time with her. And that would require coding.

The solution I want to propose is to add a heartbeat mechanism. Libre 3 would serve as heartbeat and would simple wake up xDrip4iOS (in case of Libre it would be every minute because Libre sends readings every minute). It would be a generic mechanism, other device can be configured as hearbeat :

Actually I already implemented it also for Libre 3. But for some reason it did not always work. My plan would be to redo those changes in a seperate branch and let you test

viktorturda commented 5 months ago

I see, so if I'm getting it right Libre3 would be connected to xdrip via bluetooth so it will keep the app alive by sending readings every minute. The readings from bluetooth will be discarded as I assume you can't decode it. Readings still will be downloaded from LibreLinkUp account and bluetooth connection only works as a heartbeat.

My question is that the official Libre3 app and xdrip bluetooth connection won't conflict in that case? As I remember when I used Libre2 I had to switch off the official app bluetooth connection.

I'm happy to help and test your solution. I'm a software engineer as well just don't have too much experience in mobile development but yeah I can build the app and test it from a source branch.

Thank you

JohanDegraeve commented 5 months ago

That's about it yes. Except that bluetooth does not keep the app 'alive'. The app (xDrip4iOS) goes to suspended mode, that goes as soon as the app finished processing the previous reading. I think you can compare with a laptop that you put in sleep mode. Windows will store the current status of all the applications on disk. If you relaunch Windows (by clicking the keyboard) it will read from disk and reopen the applications as if they never stopped. The difference here is that only xDrip4iOS goes in sleep mode, not the comple iPhone. Apple has also been clever enough to not put their own apps in suspended mode (i think), for instance the mail app. Or for instance iCloud. I'm always surprised to see how fast pictures are uploaded to the cloud. While third party apps have a big difficulty to ensure pictures are uploaded.

xDrip4iOS will not reply back to the Libre, so it won't conflict. In case of Libre 2 that was different, xDrip4iOS was doing the authentication, which required sending back messages.

Ok I'll prepare a test branch

JohanDegraeve commented 5 months ago

this could or should do it : https://github.com/JohanDegraeve/xdripswift/tree/issue498

This works only in follower mode, and allows to trigger NS download with "background keep-alive" disabled

Let's see if that works (I tested with Dex, not Libre, it works with Dexcom).

viktorturda commented 5 months ago

Thank you very much! I will test it as soon as possible.

In the meantime I've found a workaround to keep the application in the foreground by lock screen when it's connected to CarPlay so it doesn't have to play a sound to keep the app alive.

viktorturda commented 5 months ago

hey @JohanDegraeve

In your step to step description I see a "scan" step. Does it mean I need to scan my sensor via NFC? If so I assume I need to pay for an Apple Developer account as a free AppleID signed app can't use NFC, am I right?

JohanDegraeve commented 5 months ago

no just bluetooth scan. If you click the scan button in the app itself, there will be no pop up asking you to do NFC scan. Did you take the latest commit in the branch? because I did some fixes 2 days ago

viktorturda commented 5 months ago

I didn't have time to build yet but thanks for the notice I will pull the latest changes when I get there

viktorturda commented 5 months ago

Hey @JohanDegraeve

I tested the branch for an hour but unfortunately it doesn't work as expected. I followed your steps and disable Background Keep-Alive in follower mode to do not play the audio.

There were some BG readings but a lot of missed and also got a few missed readings notification. Same with calendar event creation and Nightscout upload.

Screenshots of the test: IMG_2127 IMG_2128 IMG_2129 IMG_2130

viktorturda commented 5 months ago

Sorry about the cropped images

JohanDegraeve commented 5 months ago

so it did work sometimes? That's also what I experienced while using Libre 3. could you send the problem report? It would need to be one shortly after your tests. Or if you have the heartbeat still configured and connected right now, you can send it immediately. Send to xdrip@proximus.be

viktorturda commented 5 months ago

Yeah there were some readings.

I removed this build for now but I can do a test again later. How can I extract the problem report and how long should I run the test?

JohanDegraeve commented 5 months ago

one hour should be enough. There's a seperate option in the settings to send a problem report. No need to incude debug level. And it should be ok to keep the app running with this build later on, but then with the keep alive enabled

viktorturda commented 5 months ago

I sent the logs but it worked properly this time. No idea what was the difference...

viktorturda commented 5 months ago

Anyway, I don't want to waste you time anymore if there is no an easy, exact and clear solution. The workaround I mentioned above to keep the app in the foreground by lock screen is fine by me.

JohanDegraeve commented 5 months ago

I sent the logs but it worked properly this time. No idea what was the difference...

maybe it's because you had the app in the foreground that it was working? In the logs I don't see a lot of bluetooth-activity.

viktorturda commented 5 months ago

I did exactly the same as last time, maybe I had fewer apps open in the first 10-20 minutes not sure if that makes any difference. After that I opened a lot of apps that I usually use.

What I've found in the logs that is playing audio now even if keep alive is disabled:

2024-02-04 11:37:48.1080 5.0.2 4193 LibreLinkUpFollowManager        playing audio every 5 seconds. LibreLinkUp keep-alive: Disabled
2024-02-04 11:37:53.1080 5.0.2 4193 LibreLinkUpFollowManager        playing audio every 5 seconds. LibreLinkUp keep-alive: Disabled
2024-02-04 11:37:58.1090 5.0.2 4193 LibreLinkUpFollowManager        playing audio every 5 seconds. LibreLinkUp keep-alive: Disabled
2024-02-04 11:38:03.1080 5.0.2 4193 LibreLinkUpFollowManager        playing audio every 5 seconds. LibreLinkUp keep-alive: Disabled
2024-02-04 11:38:08.1080 5.0.2 4193 LibreLinkUpFollowManager        playing audio every 5 seconds. LibreLinkUp keep-alive: Disabled
JohanDegraeve commented 5 months ago

could you pull my latest changes and send the logs? (just 10 minutes is ok)

doesn't matter that it keeps playing the audio for now, I'll see with Paul why it's doing that

viktorturda commented 5 months ago

I pulled and build the latest changes and sent the report. Now I don't see playing sound records in the log, I reopened the app after I set keep-alive to disabled.

But I still can't see too much info about bluetooth activity again, hope it will help.

viktorturda commented 5 months ago

Ohhh I see bluetooth activity only my grep expression was bad...

viktorturda commented 5 months ago

And for the record I just tested only for about 20 minutes but it seemed it works without playing sound

JohanDegraeve commented 5 months ago

there is a lot, you can see every minute between 17:48 and 18:11 "in peripheralDidUpdateValueFor, characteristic = 0898177A-EF89-11E9-81B4-2A2AE2DBCCE4"

(now I see you also grepped)

I think it works yes but there seems to be a lot, you can see there's many times "NightScoutFollowManager in download"

I hope LibreView will not see this as DOS attack :)

viktorturda commented 5 months ago

So it tries to download from LibreLinkUp more than once in a minute? That sounds dangerous :)

JohanDegraeve commented 5 months ago

I'll test the same branch using Dexcom as heartbeat and will see why it continues to download

JohanDegraeve commented 5 months ago

I made a new commit. It works with Dexcom and I then made the changes for Libre. can you do a test?

viktorturda commented 5 months ago

yep, logs are sent

JohanDegraeve commented 5 months ago

it seems to be working now , can you confirm? there's a download every minute. And there should be no more sound when using carplay You can use this full time by the way with out the sound

viktorturda commented 5 months ago

Yeah confirmed

I didn’t have a chance to try it in the car but I can see it doesn’t play audio and BG readings are still there so it should work

Are you going to merge this to master?

thank you so much for your efforts

JohanDegraeve commented 5 months ago

It's not yet ready for merging. I did some changes related to disabling the keep-alive, which need to be reviewed by Paul.

It's good that it's working now and thanks for helping in testing!

viktorturda commented 5 months ago

I’m glad I could help and really really appreciate what you guys doing here with developing this application

paulplant commented 5 months ago

Nice work!!

Once a minute should be fine, scheduleNewDownload() already sets a 60 second download time.

I'm away on business until the weekend but will review soon. I'd also like to reflect that the heartbeat is being used in the UI in the data source view which would be nice.

JohanDegraeve commented 5 months ago

Once a minute should be fine.

what do you mean with once a minute?

... scheduleNewDownload() already sets a 60 second download time.

schedulenewdownload has been disabled if followerBackgroundKeepAliveType = .disabled see https://github.com/JohanDegraeve/xdripswift/blob/issue498/xdrip/Managers/NightScout/NightScoutFollowManager.swift#L166 and https://github.com/JohanDegraeve/xdripswift/blob/issue498/xdrip/Managers/NightScout/NightScoutFollowManager.swift#L208 If there's no sound played every 5 seconds (.disabled) then it makes no sense because it will not run when the app is in the background. And the aim is that the heartbeat triggers the download, not the scheduled download

paulplant commented 5 months ago

I remember seeing a comment hoping that a LibreLinkUp connection every 60 seconds wouldn't be considered a DOS attack by Abbott servers but I meant that we're already scheduling 60 second downloads whenever followerBackgroundKeepAliveType != .disabled. So I mean it will be fine.

And yes, disabled the ".disable/.normal/.aggressive" is what we need to do when a valid heartbeat is connected as you say... I just wanted to see how we can do this in a nice way for the user to prevent confusion. I'll be back over the next days and will check it out.

JohanDegraeve commented 5 months ago

I remember seeing a comment hoping that a LibreLinkUp connection every 60 seconds wouldn't be considered a DOS attack by Abbott servers but I meant that we're already scheduling 60 second downloads whenever followerBackgroundKeepAliveType != .disabled. So I mean it will be fine.

There was an error in a previous version, there was a continuous download being triggered. Once a minute should indeed not be a problem.

And yes, disabled the ".disable/.normal/.aggressive" is what we need to do when a valid heartbeat is connected as you say... I just wanted to see how we can do this in a nice way for the user to prevent confusion. I'll be back over the next days and will check it out. It looks to me not easy to make sure this is clear for the user. I'll squash the commits now to make it cleaner, then you can have a look

paulplant commented 3 months ago

@viktorturda , please confirm that this issue can be closed after the previous release

viktorturda commented 3 months ago

@viktorturda , please confirm that this issue can be closed after the previous release

Yeah, confirmed! Thank you so much for your hard work

paulplant commented 3 months ago

Great, we'll close then. And this one was done by @JohanDegraeve , not me! 😀