Skip to content

Fix ble reconnection after sleep#1540

Open
zachgersh wants to merge 5 commits into
keyboardio:masterfrom
zachgersh:fix/ble-reconnection-after-sleep
Open

Fix ble reconnection after sleep#1540
zachgersh wants to merge 5 commits into
keyboardio:masterfrom
zachgersh:fix/ble-reconnection-after-sleep

Conversation

@zachgersh

Copy link
Copy Markdown

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.

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
zachgersh force-pushed the fix/ble-reconnection-after-sleep branch from 645a5d2 to 863c47f Compare April 17, 2026 15:07
@zachgersh

Copy link
Copy Markdown
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.

@zachgersh

Copy link
Copy Markdown
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.

@karptonite

Copy link
Copy Markdown

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants