Fix ble reconnection after sleep#1540
Open
zachgersh wants to merge 5 commits into
Open
Conversation
Use General Discoverable Mode instead of Limited Discoverable Mode for reconnection advertising, and advertise indefinitely instead of timing out after 60 seconds. Limited Discoverable Mode is deprioritized by macOS background scanning, causing bonded keyboards to fail to reconnect after the host wakes from extended sleep. Signed-off-by: zach gershman <zachary@hadrian.co>
When the keyboard is disconnected but still advertising (waiting for a bonded host to reconnect), stay awake instead of entering deep sleep after 10 seconds. Deep sleep drops TX power to -40 dBm and effectively makes the keyboard invisible to the host when it wakes from sleep. This trades idle battery life for reliable reconnection — advertising draws ~50-80 µA vs ~2-3 µA in deep sleep. The impact on real-world battery life needs to be evaluated. Signed-off-by: zach gershman <zachary@hadrian.co>
When the remote host terminates the connection (e.g. Mac going to sleep), use startConnectableAdvertising instead of startDiscoverableAdvertising. The discoverable path is meant for new pairing and uses a 60-second timeout, after which the keyboard goes silent. The connectable path advertises indefinitely in General Discoverable Mode, allowing the host to reconnect when it wakes. Signed-off-by: zach gershman <zachary@hadrian.co>
Previously, prepareForSleep() unconditionally dropped TX power to -40 dBm (SLEEP_TX_POWER) whenever the device was not connected. This was well-intentioned for battery savings, but SoftDevice continues to advertise during WFE sleep, and at -40 dBm those advertisements are too weak for the bonded host to hear on wake — making the keyboard effectively invisible and forcing the user to forget & re-pair. Now we only drop TX power if we're neither connected nor advertising. When advertising for reconnection, TX power stays at its normal level so the host's BT controller can pick up the advertisements when it wakes from sleep. This is what commit 2842def ("Don't enter deep sleep while advertising for reconnection") was trying to work around — but a better fix is to let the device deep-sleep (WFE) while keeping the radio discoverable, since SoftDevice handles advertising during WFE without CPU involvement. Signed-off-by: zach gershman <zachary@hadrian.co>
This reverts commit 2842def. Signed-off-by: zach gershman <zachary@hadrian.co>
zachgersh
force-pushed
the
fix/ble-reconnection-after-sleep
branch
from
April 17, 2026 15:07
645a5d2 to
863c47f
Compare
Author
|
Hey @obra can you have a look at this? I've been running this on my keyboard now for a week and its been way better bluetooth connection wise. I think this would probably solve a lot of complaints. |
Author
|
@obra bump again. I really think this should be merged. Works way better than the base firmware for me as far as reconnection is concerned. |
|
I just want to chime in: I installed this on my Preonic, and it has totally fixed the issue for me. No problem with battery life that I can see. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
So far, so good after running this on my keyboard for the last few days here. All three of these changes work together and I've got descriptive commit messages on each describing why / what.