st0nedB / rooms

With "Rooms" mobile devices can perform indoor self-localization using an app and low-cost BLE beacons.
MIT License
64 stars 7 forks source link

Background task is not working probably #14

Open GaoMax opened 3 years ago

GaoMax commented 3 years ago

Describe the bug Sometimes the background task doesn't behave like it should. If the unexpected behavior occurs, the task is dead. Launching the app is needed for it to work probably again. It happens when "Making Fake Move" is performed multiple times instead of only once. If the expected behavior occurs, it makes a fake move and restarts the background task.

Unexpected behavior

Background task registered Background task ended. Making Fake Move. Making Fake Move. Making Fake Move. Room Buero, Likelihood 90.4 Making Fake Move.

OR

Background task registered Background task ended. Making Fake Move. Making Fake Move. Making Fake Move. Room Buero, Likelihood 99.6 Making Fake Move. Making Fake Move. Room Buero, Likelihood 99.6

Expected behavior

Background task registered Background task ended. Making Fake Move. Room Buero, Likelihood 97.2 Background task registered

Smartphone (please complete the following information):

GaoMax commented 3 years ago

Hey, since the latest commit background task has completed stopped working for me. I've used the original code, no modifications. Behavior is the same, making fake move is called multiple times and no new background task is registered. I've tried modifying the mode, but haven't been successful. I haven't quite understood your background energy saving measures.

GaoMax commented 3 years ago

On further investigation it appears to be a timing problem. The multiple calls of Making Fake Move is due to the shortened threshold of remainingTime. Changing the threshold to 5-10 fixes the multiple making fake moves calls, but the task dies afterwards. Putting it at 15 makes it work most of the times, but not always, same for 20. It appears that if the phone does not move at all before background task starts, the restart of the task fails.

st0nedB commented 3 years ago

Just to add some additional background info: To keep the app alive, Apple requires to keep the Localization services running at all times. However, this draws a lot of power, even when the device is not moving and hence not changing location. The current workaround is to move the app to background processing, which gives around 30s of processing before iOS terminates it completely. To avoid termination, the location services are switched back on and beacon ranging is performed, which keeps the app alive (aka triggering a "fake move"). Afterwards the app switches to the background again for another 30s. Location services are also switched back on when the IMU detects movement.

Increasing the threshold to trigger the fakeMove earlier will hence decrease power efficiency.

GaoMax commented 3 years ago

Thank you for elaborating! This mechanism is quite ingenious. But it seems like it is currently not working probably. Maybe Apple broke this workaround in an iOS update? If the 30s window is used fully i.e. 1 second is left for the background task, it behaves weirdly. I couldn't find any code related bugs for this behavior. If 15s are left for the background task, it behaves correctly.

st0nedB commented 3 years ago

Yes, that's definitely the first thing to investigate. Maybe I'll find time to look into it at the weekend, but I can't promise ;) BTW: This mechanism is also why I didn't consider submitting to the AppStore. Its probably not the way Apple intents for things to work. My hope is they will give us some way to perform beacon ranging in the background in the future without using the full localization services framework. This method is great for indoor localization and actually really privacy conserving.

GaoMax commented 3 years ago

Sounds awesome!