Skip to content

Fix: add BLE state cleanup to ducky_typer functions - #2669

Merged
bmorcelli merged 41 commits into
BruceDevices:devfrom
Ninja-jr:Ble_fixes_2
Jul 22, 2026
Merged

Fix: add BLE state cleanup to ducky_typer functions#2669
bmorcelli merged 41 commits into
BruceDevices:devfrom
Ninja-jr:Ble_fixes_2

Conversation

@Ninja-jr

Copy link
Copy Markdown
Contributor

This PR adds a shared cleanup function (cleanupDuckyBLE()) to properly reset BLE state when exiting BLE Keyboard, Media Commands, and iBeacon functions in ducky_typer.cpp.

It sorts out the remaining issues i detected on the ble modules.

Problem
When using BLE Keyboard, Media Commands, or iBeacon, the BLE stack was left in a dirty state after exiting. This caused subsequent BLE features to fail with "Resource busy" or "Already connected" errors.

Solution

· Added cleanupDuckyBLE() function inside ducky_typer.cpp
· Function stops advertising, cleans clients, resets flags
· Does NOT deinitialize BLE stack (shared resource)
· Called on exit from:
· ducky_keyboard() (BLE Keyboard)
· MediaCommands() (Media Commands)
· iBeacon (via inline cleanup)

Why This Approach

· Keeps fix isolated to the problem modules
· Does not touch working ble_common code
· Preserves scan results for menu display
· Minimal flash impact

Ninja-jr added 2 commits July 19, 2026 18:13
Added cleanup function for BLE state management in ducky_typer.
Added cleanup function for ducky_typer BLE operations.
@Ninja-jr
Ninja-jr marked this pull request as draft July 19, 2026 16:31
Ninja-jr added 10 commits July 19, 2026 18:33
Removed unnecessary cleanup of BLE state on exit in multiple functions.
Removed unnecessary comments and cleaned up the code for better readability. Adjusted some logic for handling BLE connections and key inputs.
Refactor ibeacon function to avoid reinitializing BLE stack and improve advertising logic.
Refactor ibeacon function to use BLEDevice::init() and adjust advertising logic.
@Ninja-jr
Ninja-jr marked this pull request as ready for review July 19, 2026 22:20
Ninja-jr added 2 commits July 20, 2026 00:27
Refactor cleanupDuckyBLE to delete hid_ble and log the action.
@Ninja-jr
Ninja-jr marked this pull request as draft July 19, 2026 23:07
@Ninja-jr

Copy link
Copy Markdown
Contributor Author

This needs fixing but im running into a struggle here...if i fix this this way its better but still a bit sloppy...and it messes up with ibeacon somehow that as soon as we start it kicks us back to main menu...just like it used to do a while ago.

So for now ill close this and try to think on a new solution eventually .

@Ninja-jr Ninja-jr closed this Jul 19, 2026
@Huzzla101

Huzzla101 commented Jul 20, 2026 via email

Copy link
Copy Markdown

@Ninja-jr

Copy link
Copy Markdown
Contributor Author

Thanks alot. Well i just cant see an issue when I use the firmware and not try to sort it...but its true when we start running in circles is better to take a step back, get some rest enjoy life and eventually we or someone will find the solutions. Im sure its not that big of a deal but I couldn't fetch it yet.
Anyway thanks for your kind words and support

@Ninja-jr Ninja-jr reopened this Jul 21, 2026
Ninja-jr added 8 commits July 21, 2026 04:29
Refactor Ducky Typer code to optimize memory usage by storing large command structures in PROGMEM. Update command parsing and handling to improve performance and reduce heap fragmentation.
Updated cleanupDuckyBLE and ducky_startKb functions for better RAM management and clearer logging. Refactored key_input function to improve string handling and command parsing.
Refactored BLE iBeacon functionality to ensure proper button handling and stack initialization/deinitialization. Added necessary includes and improved readability.
Refactor BLE initialization and scanning logic for improved memory management and error handling.
Made various fixes throughout the code to ensure proper initialization and handling of BLE state and attacks.
@Ninja-jr

Copy link
Copy Markdown
Contributor Author

@bmorcelli i think we can merge this if you think the code is ok. It sorts out the errors on a subsequent connection on another module that error out even with the latest beta.
Only ibeacon issue not sorted out (it starts but kicks out to main menu right away) but that issue exists also on 1.14, 1.15 and latest beta...ill take a look at it later on, but i think the issue is DMA fragmentation as it works if you start it right after boot but not if you start it again.

Also made some memory optimization to avoid getting low ram warnings on some devices that then forced the user to turn off wifi in order to subsequently use other ble functions. It now does the checks and if low memory for ble then frees up DMA memory by disconnecting from wifi and rechecks. If still low only then it shows the low ram warning.

This is as much as i can do for now to get it all working as its supposed to. You can check it and eventually merge if you think the fixes are good.

@bmorcelli

Copy link
Copy Markdown
Member

I used a Cardputer ADV to test

First test
- Turn On device
- BLE >  Media Cmds   -> Pairing works       -> Keys working    -> Leave to menu -> Device disconnects from host
- BLE >  Media Cmds   -> Pairs automatically -> **do not work** -> Leave to menu -> ...
- BLE >  BLE Keyboard -> Pairs automatically -> **do not work** -> Leave to menu -> ...

- Reboot device
- BLE >  BLE Keyboard -> Pairs automatically -> Keys working    -> Leave to menu -> Device disconnects from host
- BLE >  Media Cmds   -> Pairs automatically -> **do not work** -> Leave to menu -> ...
- BLE >  Media Cmds   -> Pairs automatically -> **do not work** -> ...

Probably need to work on the BLEKeyboard destructor (which does not exist) or uses ble_hid->end(); before deleting it here

maybe it is keeping an old notifier or something that is not being rebuilt/reconfigured between reconections

Updated cleanupDuckyBLE to optionally perform a full cleanup. Enhanced BLE stack management during cleanup and initialization processes.
@Ninja-jr

Copy link
Copy Markdown
Contributor Author

I used a Cardputer ADV to test

First test
- Turn On device
- BLE >  Media Cmds   -> Pairing works       -> Keys working    -> Leave to menu -> Device disconnects from host
- BLE >  Media Cmds   -> Pairs automatically -> **do not work** -> Leave to menu -> ...
- BLE >  BLE Keyboard -> Pairs automatically -> **do not work** -> Leave to menu -> ...

- Reboot device
- BLE >  BLE Keyboard -> Pairs automatically -> Keys working    -> Leave to menu -> Device disconnects from host
- BLE >  Media Cmds   -> Pairs automatically -> **do not work** -> Leave to menu -> ...
- BLE >  Media Cmds   -> Pairs automatically -> **do not work** -> ...

Probably need to work on the BLEKeyboard destructor (which does not exist) or uses ble_hid->end(); before deleting it here

maybe it is keeping an old notifier or something that is not being rebuilt/reconfigured between reconections

Noted and i'm trying to handle that then. To me it seemed ok the first time but when i read your message and i tested on a cardputer adv aswell (yes it came through after all so now i can actually test) it behaved as you described to im trying to tackle that now

@Ninja-jr

Copy link
Copy Markdown
Contributor Author

I'm trying a new approach, some things in the line of some issues had tried but some new ones to see if i can get it to work. Thing is as is on the beta it just can't stay that way and that's why im trying to get this fixed so we get mostly done with the ble functions for a while

Ninja-jr added 14 commits July 21, 2026 16:31
Updated comments for clarity and consistency regarding function IDs and BLE initialization. Improved safety checks during BLE cleanup and initialization.
Refactor BLE cleanup and HID instance management.
Added extern declaration for active BLE instance.
Added support for a BLE HID instance and updated cleanup logic.
Refactor cleanup functions for BLE HID instances to ensure proper deinitialization and safety. Implement double cleanup with delays to handle lingering instances and prevent memory leaks.
Added a new function for safe cleanup of DuckyBLE instances with a cooling delay.
Removed unused Bluetooth headers from ducky_typer.cpp
@Ninja-jr
Ninja-jr marked this pull request as draft July 21, 2026 21:46
@Ninja-jr

Copy link
Copy Markdown
Contributor Author

Really couldn't get this to work as intended...reduced the errors to virtually zero but still can't find a way to get this to work after we leave the first function and try to use a different one...tried many options and ways but till now couldn't seem to find the right way to do this

@bmorcelli
bmorcelli marked this pull request as ready for review July 22, 2026 17:16
@bmorcelli

Copy link
Copy Markdown
Member

Fixed, tested and working.. merging now 😉

@bmorcelli
bmorcelli merged commit 84e2631 into BruceDevices:dev Jul 22, 2026
7 checks passed
@Ninja-jr

Ninja-jr commented Jul 22, 2026

Copy link
Copy Markdown
Contributor Author

Yup that sorted it all out. You sorted what got me stuck all this time @bmorcelli great job. Good thing in the middle of this is with all my attempts we actually achieved services separation...each one has its own service which makes each less dependent on others...if one gets broken it shall not affect others and hardcoding the device name instead of trying to fetch it from the config file is a good call i think so all in all i dont regret spending 2 blank nights on this 😜

@Ninja-jr
Ninja-jr deleted the Ble_fixes_2 branch July 23, 2026 16:57
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