Restore Speak BG for Bluetooth and Improve Announcement Logic by bjorkert · Pull Request #421 · loopandlearn/LoopFollow
Description
This PR addresses three key issues with the "Speak BG" feature:
- It restores functionality for users on the Bluetooth background refresh mode.
- It fundamentally improves the announcement logic to prevent incorrect repetitions.
- When the app refreshed it's data, for example after sending remote commands, the bg value was unintentionally spoken, this is now removed.
Restoring Bluetooth Background Mode
The "Speak BG" feature was failing for users on the Bluetooth background refresh setting because iOS was denying the app's request to activate an audio session from a background state.
This has been fixed by implementing a more robust AVAudioSession management strategy:
- The audio session is now activated only when a new BG reading is ready to be announced.
- The session is intelligently deactivated immediately after speech is finished, but only when necessary. It correctly keeps the session alive when in "Silent Tune" background mode but closes it for Bluetooth mode to save power.
Improved Announcement Logic & Bug Fix
The previous logic used a simple 30-second timer to prevent repeated announcements. This was inefficient and led to bugs where old BG values could be repeated after certain actions, like remote commands.
The announcement logic has been completely rewritten:
- Instead of a timer, the app now tracks the timestamp of the last BG value that was announced.
- It will only speak a new value if its timestamp is more recent than the last one spoken.
This new data-driven approach is more reliable and naturally prevents the repetition of old data.
Resolves: #343