Skip to content

feats: prevent sleep while active BT profile connected and allow sleep while USB power connected#3288

Open
newx-it wants to merge 12 commits intozmkfirmware:mainfrom
newx-it:feat-ble-prevent-sleep-feat-usb-allow-sleep
Open

feats: prevent sleep while active BT profile connected and allow sleep while USB power connected#3288
newx-it wants to merge 12 commits intozmkfirmware:mainfrom
newx-it:feat-ble-prevent-sleep-feat-usb-allow-sleep

Conversation

@newx-it
Copy link
Copy Markdown

@newx-it newx-it commented Mar 19, 2026

I'm closing my previous PR in favor of this one, which has some fixes applied and should have proper commit messages/clean commit history.

In short, I've added two flags:
CONFIG_ZMK_BLE_PREVENT_SLEEP_WHILE_ACTIVE_CONNECTED and CONFIG_ZMK_USB_ALLOW_SLEEP_WHILE_POWERED
The first allows the user to disable sleeping when the active BT profile is connected, related to issue 2198. The second arose naturally as I was testing my work on the first, but I believe it has merit as a means of increasing battery life for peripherals on devices that have the "main" device plugged in, whether it be a dongle or just a split.

pre-commit doesn't seem to like my formatting but I believe it's getting confused by the broken up lines, which are added for readability.

Like with my last PR, I am a new contributor (and new to open source as a whole) so any and all advice is appreciated!

PR check-list

  • Branch has a clean commit history
  • Additional tests are included, if changing behaviors/core code that is testable.
  • Proper Copyright + License headers added to applicable files (Generally, we stick to "The ZMK Contributors" for copyrights to help avoid churn when files get edited)
  • Pre-commit used to check formatting of files, commit messages, etc.
  • Includes any necessary documentation changes.

newx-it added 4 commits March 19, 2026 08:18
Adds a flag which, when enabled, allows users to prevent the device from sleeping when the chosen BT profile is connected/active.
Adds a flag which allows users to enable sleep even if USB power is present.
newx-it and others added 3 commits March 19, 2026 08:49
fixed an error caused by an extra line of code that wasn't deleted
Not sure why pre-commit is having issues with the changes in activity.c, but attempted a fix there and fixed the final formatting issue in power.md
Removed stray backslash character
Copy link
Copy Markdown
Contributor

@caksoylar caksoylar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implementation looks OK to me, but it is worth noting that the timeout does not reset while BLE is connected. So the part can immediately go to sleep once it disconnects, if it has been idle for a while. I think that's fine, but might be worth noting somewhere in the docs.

Also, pre-commit is failing and needs to be fixed. You can set it up locally to fix things for you when you push: https://zmk.dev/docs/development/local-toolchain/pre-commit

Comment thread app/Kconfig Outdated
int "Milliseconds of inactivity before entering deep sleep"
default 900000

config ZMK_BLE_PREVENT_SLEEP_WHILE_ACTIVE_CONNECTED
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO these should be prefixed with ZMK_SLEEP, since they are all sub-options related to it.

Suggested change
config ZMK_BLE_PREVENT_SLEEP_WHILE_ACTIVE_CONNECTED
config ZMK_SLEEP_PREVENT_WHILE_BLE_CONNECTED

Comment thread app/Kconfig Outdated
depends on ZMK_BLE
default n

config ZMK_USB_ALLOW_SLEEP_WHILE_POWERED
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
config ZMK_USB_ALLOW_SLEEP_WHILE_POWERED
config ZMK_SLEEP_PREVENT_WHILE_USB_POWERED

newx-it added 2 commits March 22, 2026 21:47
Renamed both config flags, slightly changed syntax to support the swap from "allow" to "prevent" for the USB power flag.
fix formatting according to pre-commit's preferences
@newx-it
Copy link
Copy Markdown
Author

newx-it commented Mar 24, 2026

Pushed a couple changes but I'm having trouble phrasing the doc changes for the lack of BLE timeout. any suggestions?

Added documentation for the interaction between BT connection keeping the device awake and the sleep timer. When the device loses connection after the sleep timer has elapsed, it will immediately sleep.
@newx-it
Copy link
Copy Markdown
Author

newx-it commented Mar 24, 2026

Alright, docs should be fixed. I'm conflicted about whether I should just reset the timer for a more intuitive sleep timer instead though, or even add a third flag to allow either behavior. Any opinions?

Copy link
Copy Markdown
Contributor

@caksoylar caksoylar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this behavior is fine; I wouldn't overcomplicate it with more options.

Also please check the failed pre-commit again -- running prettier after editing the tables as below should fix it.

Comment thread docs/docs/config/power.md Outdated
| `CONFIG_ZMK_SLEEP` | bool | Enable deep sleep support | n |
| `CONFIG_ZMK_IDLE_SLEEP_TIMEOUT` | int | Milliseconds of inactivity before entering deep sleep | 900000 |
| `CONFIG_ZMK_PM_SOFT_OFF` | bool | Enable soft off functionality from the keymap or dedicated hardware | n |
| `CONFIG_ZMK_BLE_PREVENT_SLEEP_WHILE_ACTIVE_CONNECTED` | bool | Prevent sleeping while the active BT profile is connected. If the device loses connection after the sleep timer, it will sleep immediately | n |
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
| `CONFIG_ZMK_BLE_PREVENT_SLEEP_WHILE_ACTIVE_CONNECTED` | bool | Prevent sleeping while the active BT profile is connected. If the device loses connection after the sleep timer, it will sleep immediately | n |
| `CONFIG_ZMK_SLEEP_PREVENT_WHILE_BLE_CONNECTED` | bool | Prevent sleeping while the active BT profile is connected. If the device loses connection after the sleep timer, it will sleep immediately | n |

mismatching the Kconfig file; also will need a prettier pass to fix table formatting

Comment thread docs/docs/config/power.md Outdated
| `CONFIG_ZMK_IDLE_SLEEP_TIMEOUT` | int | Milliseconds of inactivity before entering deep sleep | 900000 |
| `CONFIG_ZMK_PM_SOFT_OFF` | bool | Enable soft off functionality from the keymap or dedicated hardware | n |
| `CONFIG_ZMK_BLE_PREVENT_SLEEP_WHILE_ACTIVE_CONNECTED` | bool | Prevent sleeping while the active BT profile is connected. If the device loses connection after the sleep timer, it will sleep immediately | n |
| `CONFIG_ZMK_USB_PREVENT_SLEEP_WHILE_POWERED` | bool | Prevent sleeping while USB power is connected | y |
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
| `CONFIG_ZMK_USB_PREVENT_SLEEP_WHILE_POWERED` | bool | Prevent sleeping while USB power is connected | y |
| `CONFIG_ZMK_SLEEP_PREVENT_WHILE_USB_POWERED` | bool | Prevent sleeping while USB power is connected | y |

renamed flag names in docs to match previous changes
Comment thread app/Kconfig Outdated
config ZMK_SLEEP_PREVENT_WHILE_BLE_CONNECTED
bool "Prevent sleeping while the active BT profile is connected"
depends on ZMK_BLE
default n
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing I missed; can you remove? See #2161

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
default n

removed the redundante Kconfig "default n" value from the ZMK_SLEEP_PREVENT_WHILE_BLE_CONNECTED flag
Copy link
Copy Markdown
Contributor

@caksoylar caksoylar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@veypatch
Copy link
Copy Markdown
Contributor

veypatch commented Apr 3, 2026

Been using this for a couple days now. Amazing feature and works great! Quite huge battery savings as well as the keyboard won't stay in "search" mode when the computer has already been turned off and waiting for the sleep timeout.

MaXMaNDeV added a commit to MaXMaNDeV/zmk that referenced this pull request Apr 5, 2026
)

  Adds CONFIG_ZMK_SLEEP_PREVENT_WHILE_BLE_CONNECTED to prevent
  deep sleep while the active BT profile is connected.
  Avoids Issue zmkfirmware#3207 (Central fails to wake from deep sleep).

  Based on zmkfirmware#3288 by @jkosh44
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.

3 participants