### Quick Start Example Source: https://github.com/hijelhub/hijelhid_blekeyboard/blob/main/README.md A basic example demonstrating how to initialize the keyboard, check connection status, and send keystrokes including text, modifier keys, and key combinations. ```APIDOC ## Quick Start Example ### Description Demonstrates basic usage of the HijelHID_BLEKeyboard library: initializing the keyboard, checking connection, and sending keystrokes. ### Code ```.ino #include HijelHID_BLEKeyboard keyboard; void setup() { keyboard.begin(); } void loop() { delay(5000); // Open a text editor on your host device if (keyboard.isConnected()) { // Print "Hello, World!" keyboard.print("Hello, "); keyboard.println("World!"); // Press and Tap "ESP32!" keyboard.press(KEY_LSHIFT); delay(25); keyboard.tap(KEY_E); keyboard.tap(KEY_S); keyboard.tap(KEY_P); keyboard.release(KEY_LSHIFT); keyboard.tap(KEY_3); keyboard.tap(KEY_2); // tap an exclamation point "!" keyboard.tap(KEY_1, KEY_MOD_LSHIFT); keyboard.tap(KEY_RETURN); keyboard.releaseAll(); } } ``` ``` -------------------------------- ### Lifecycle: begin() in setup() Source: https://github.com/hijelhub/hijelhid_blekeyboard/blob/main/README.md Call begin() once in setup() to start BLE advertising. The device becomes discoverable and ready to pair. ```ino void setup() { keyboard.begin(); } ``` -------------------------------- ### Lifecycle: begin() Source: https://github.com/hijelhub/hijelhid_blekeyboard/blob/main/README.md Initialises BLE and starts advertising, making the device discoverable and ready to pair. Call once in setup(). ```APIDOC ## Lifecycle: begin() ### Description Initialises BLE and starts advertising. The device becomes discoverable and ready to pair. Call once in setup(). ### Method begin ### Parameters None ### Request Example ```cpp void setup() { keyboard.begin(); } ``` ### Response No return value. ``` -------------------------------- ### Enable debug logging with setLogLevel before begin() Source: https://github.com/hijelhub/hijelhid_blekeyboard/blob/main/README.md Call in setup() before keyboard.begin() to enable Serial logging for connection and pairing events. Uses HIDLogLevel::Normal to log connection, pairing, and advertising events. Requires Serial.begin(115200) to be called first. ```ino void setup() { Serial.begin(115200); keyboard.setLogLevel(HIDLogLevel::Normal); // Connection and pairing events keyboard.begin(); } ``` -------------------------------- ### Quick Start: HijelHID BLEKeyboard Arduino Sketch Source: https://github.com/hijelhub/hijelhid_blekeyboard/blob/main/README.md Arduino sketch that initializes the HijelHID BLEKeyboard, waits for a connection, and sends 'Hello, World!' followed by 'ESP32!' with shifted characters. It demonstrates print, println, press, tap, release, and releaseAll methods. The sketch waits 5 seconds before sending, and only sends if the keyboard is connected. ```ino #include HijelHID_BLEKeyboard keyboard; void setup() { keyboard.begin(); } void loop() { delay(5000); // Open a text editor on your host device if (keyboard.isConnected()) { // Print "Hello, World!" keyboard.print("Hello, "); keyboard.println("World!"); // Press and Tap "ESP32!" keyboard.press(KEY_LSHIFT); delay(25); keyboard.tap(KEY_E); keyboard.tap(KEY_S); keyboard.tap(KEY_P); keyboard.release(KEY_LSHIFT); keyboard.tap(KEY_3); keyboard.tap(KEY_2); // tap an exclamation point "!" keyboard.tap(KEY_1, KEY_MOD_LSHIFT); keyboard.tap(KEY_RETURN); keyboard.releaseAll(); } } ``` -------------------------------- ### Type strings with print() and println() Source: https://github.com/hijelhub/hijelhid_blekeyboard/blob/main/README.md print() and println() handle uppercase, punctuation, and spaces automatically; println() adds a newline. This example sends two lines of text. ```Arduino keyboard.print("Hello, World!"); keyboard.println("This line ends with Enter"); ``` -------------------------------- ### Deep Sleep with beforeSleep and Reconnect on Wakeup Source: https://github.com/hijelhub/hijelhid_blekeyboard/blob/main/README.md No special library calls are needed for deep sleep, but call beforeSleep() before sleeping to release held keys cleanly. On wakeup, setup() runs as normal and begin() reconnects via the stored bond automatically. ```ino // Before sleeping: keyboard.beforeSleep(); esp_deep_sleep_start(); // On wakeup, setup() runs as normal: void setup() { keyboard.begin(); // reconnects via stored bond automatically } ``` -------------------------------- ### Check connection with isConnected() Source: https://github.com/hijelhub/hijelhid_blekeyboard/blob/main/README.md Use isConnected() to verify a host is connected before sending keys. In this example, the sketch taps the 'A' key every 2 seconds only when connected. ```Arduino void loop() { if (keyboard.isConnected()) { keyboard.tap(KEY_A); delay(2000); } } ``` -------------------------------- ### Light Sleep with beforeSleep and afterWake Source: https://github.com/hijelhub/hijelhid_blekeyboard/blob/main/README.md Call beforeSleep() immediately before entering light sleep and afterWake() immediately after. afterWake() blocks until the host has fully reconnected and the HID stack has settled, or until the default timeout (15000ms) expires. Change the timeout with setAfterWakeTimeout() in setup(). ```ino keyboard.beforeSleep(); esp_light_sleep_start(); keyboard.afterWake(); // blocks until host is ready keyboard.println("Woke from light sleep!"); ``` -------------------------------- ### Check Idle Time for Light Sleep Decision Source: https://github.com/hijelhub/hijelhid_blekeyboard/blob/main/README.md Uses getIdleTime() to check how long the keyboard has been idle, for example to decide when to enter light or deep sleep. The example enters light sleep after 30 seconds of inactivity. ```ino if (keyboard.isPaired() && keyboard.getIdleTime() > 30000) { // No key sent for 30 seconds — enter light sleep keyboard.beforeSleep(); esp_light_sleep_start(); keyboard.afterWake(); } ``` -------------------------------- ### Enter sleep after idle time with getIdleTime() Source: https://github.com/hijelhub/hijelhid_blekeyboard/blob/main/README.md getIdleTime() returns milliseconds since the last HID report. This example enters light sleep when the keyboard is paired and idle for over 30 seconds, then wakes up. ```Arduino if (keyboard.isPaired() && keyboard.getIdleTime() > 30000) { keyboard.beforeSleep(); esp_light_sleep_start(); keyboard.afterWake(); } ``` -------------------------------- ### Read LED state and react to changes with onLEDChange callback Source: https://github.com/hijelhub/hijelhid_blekeyboard/blob/main/README.md Use to check Caps Lock status or set a callback that fires whenever the host updates LED state. The callback receives a bitmask of LED states; check against HID_LED_CAPS_LOCK to detect Caps Lock. The example prints status messages to Serial. ```ino // Read current state if (keyboard.isCapsLockOn()) { Serial.println("Caps Lock is ON"); } // React to changes with a callback keyboard.onLEDChange([](uint8_t leds) { if (leds & HID_LED_CAPS_LOCK) { Serial.println("Caps Lock ON"); } else { Serial.println("Caps Lock OFF"); } }); ``` -------------------------------- ### Constructor: Create keyboard object with custom name and manufacturer Source: https://github.com/hijelhub/hijelhid_blekeyboard/blob/main/README.md Shows how to instantiate a HijelHID_BLEKeyboard object. The default constructor uses 'HijelHID KB' as the device name and 'Hijel' as the manufacturer. Custom parameters allow setting a device name (max 29 chars), manufacturer (max 512 chars), and battery level (1-100). ```ino // Default — shows as "HijelHID KB" when pairing HijelHID_BLEKeyboard keyboard; // Custom name and manufacturer HijelHID_BLEKeyboard keyboard("My Keyboard", "My Company", 100); ``` -------------------------------- ### Lifecycle: end() and begin() for pause/resume Source: https://github.com/hijelhub/hijelhid_blekeyboard/blob/main/README.md Shows how to pause and resume BLE advertising. end() disconnects and stops advertising but keeps the BLE stack in memory, allowing begin() to restart quickly without full reinitialization. ```ino keyboard.end(); // pause — BLE stack stays alive keyboard.begin(); // restart — fast, no full reinit ``` -------------------------------- ### Tap and hold media keys with MEDIA_* constants Source: https://github.com/hijelhub/hijelhid_blekeyboard/blob/main/README.md Media keys work with both tap() and press(). Use MEDIA_* constants from src/BLEHIDMediaKeys.h. tap() automatically presses and releases, while press() holds the key until releaseAll() is called. ```Arduino // Tap a media key (press and release automatically) keyboard.tap(MEDIA_PLAY_PAUSE); keyboard.tap(MEDIA_VOLUME_UP); keyboard.tap(MEDIA_VOLUME_DOWN); keyboard.tap(MEDIA_MUTE); keyboard.tap(MEDIA_NEXT_TRACK); keyboard.tap(MEDIA_PREV_TRACK); // Hold a media key down, then release keyboard.press(MEDIA_VOLUME_UP); delay(500); keyboard.releaseAll(); ``` -------------------------------- ### Enable passkey security mode Source: https://github.com/hijelhub/hijelhid_blekeyboard/blob/main/README.md Configures the keyboard to require a passkey challenge. setSecurityMode() must be called before begin(). The passkey is printed to Serial automatically when using HIDSecurity::Passkey. ```ino void setup() { Serial.begin(115200); keyboard.setSecurityMode(HIDSecurity::Passkey); // Must be before begin() keyboard.begin(); } ``` -------------------------------- ### Constructor: HijelHID_BLEKeyboard(deviceName, manufacturer, batteryLevel) Source: https://github.com/hijelhub/hijelhid_blekeyboard/blob/main/README.md Creates a keyboard object with optional custom name, manufacturer, and battery level. Defaults to 'HijelHID KB', 'Hijel', and 100 respectively. ```APIDOC ## Constructor: HijelHID_BLEKeyboard(deviceName, manufacturer, batteryLevel) ### Description Creates a keyboard object with an optional custom name, manufacturer, and battery level. The device name is shown to the host when pairing. ### Method Constructor ### Parameters - **deviceName** (String) - Optional - Name shown to the host when pairing. Max 29 chars. Default: "HijelHID KB" - **manufacturer** (String) - Optional - Manufacturer name. Max 512 chars. Default: "Hijel" - **batteryLevel** (int) - Optional - Starting battery level (1–100). Default: 100 ### Request Example ```cpp // Default — shows as "HijelHID KB" when pairing HijelHID_BLEKeyboard keyboard; // Custom name and manufacturer HijelHID_BLEKeyboard keyboard("My Keyboard", "My Company", 100); ``` ### Response No return value. ``` -------------------------------- ### Handle passkey and pairing callbacks Source: https://github.com/hijelhub/hijelhid_blekeyboard/blob/main/README.md Registers callbacks for passkey display and pairing completion. The passkey callback is required when using HIDLogLevel::Off in Passkey mode, otherwise the passkey will not be displayed. The pairing callback reports success or failure. ```ino #include HijelHID_BLEKeyboard keyboard; // Called when a passkey needs to be displayed to the user. // Show it however makes sense for your project — Serial, display, LEDs, etc. void onPassKey(uint32_t passkey) { Serial.print("Does this passkey match on your device? "); Serial.println(passkey); } // Called when pairing completes or fails. void onPairingComplete(bool success) { if (success) { Serial.println("Pairing successful — keyboard is ready."); } else { Serial.println("Pairing failed. Try removing and re-pairing."); } } void setup() { Serial.begin(115200); keyboard.setSecurityMode(HIDSecurity::Passkey); keyboard.setPasskeyCallback(onPassKey); keyboard.onPairingComplete(onPairingComplete); keyboard.begin(); } ``` -------------------------------- ### Lifecycle: kill() Source: https://github.com/hijelhub/hijelhid_blekeyboard/blob/main/README.md Permanently shuts down and deinitialises the BLE stack, freeing all BLE memory. begin() cannot be called after kill(). ```APIDOC ## Lifecycle: kill() ### Description Permanently shuts down and deinitialises the BLE stack, freeing all BLE memory. After kill(), begin() is refused and logs a warning. ### Method kill ### Parameters None ### Request Example ```cpp keyboard.kill(); // permanent shutdown — frees ~38KB of heap // keyboard.begin(); // ← refused after kill(), logs a warning ``` ### Response No return value. ``` -------------------------------- ### Press and release keys with keyboard.press() and releaseAll() Source: https://github.com/hijelhub/hijelhid_blekeyboard/blob/main/README.md Use press() and release() for precise hold timing. Must add delay() calls manually. releaseAll() releases all held keys at once, useful as a safety call to clear stuck keys. ```Arduino // Hold Shift while pressing a key, then release keyboard.press(KEY_H, KEY_MOD_LSHIFT); delay(25); keyboard.releaseAll(); delay(25); keyboard.press(KEY_I); delay(25); keyboard.release(KEY_I); delay(25); keyboard.press(KEY_I); delay(25); keyboard.releaseAll(); ``` ```Arduino keyboard.releaseAll(); ``` -------------------------------- ### print() and println() Source: https://github.com/hijelhub/hijelhid_blekeyboard/blob/main/README.md Type a string of characters. Upper case, punctuation, and spaces are handled automatically. println() adds a newline at the end. ```APIDOC ## print() and println() ### Description Type a string of characters. Upper case, punctuation, and spaces are handled automatically. `println()` adds a newline at the end. ### Methods - `print(data)` - Types the given string. - `println(data)` - Types the given string followed by a newline. ### Parameters - **data** (string) - The text to type. ### Example ```.ino keyboard.print("Hello, World!"); keyboard.println("This line ends with Enter"); ``` ``` -------------------------------- ### Lifecycle: end() Source: https://github.com/hijelhub/hijelhid_blekeyboard/blob/main/README.md Disconnects and stops advertising while keeping the BLE stack in memory, allowing fast restart via begin(). ```APIDOC ## Lifecycle: end() ### Description Disconnects and stops advertising. The BLE stack stays in memory so begin() can restart quickly without reinitialisation. ### Method end ### Parameters None ### Request Example ```cpp keyboard.end(); // pause — BLE stack stays alive keyboard.begin(); // restart — fast, no full reinit ``` ### Response No return value. ``` -------------------------------- ### Lifecycle: kill() for permanent shutdown Source: https://github.com/hijelhub/hijelhid_blekeyboard/blob/main/README.md Permanently shuts down and deinitializes the BLE stack, freeing all BLE memory. After kill(), begin() cannot be called and will log a warning. Note: a small bounded memory leak (~308 bytes) remains due to an upstream NimBLE issue. ```ino keyboard.kill(); // permanent shutdown — frees ~38KB of heap // keyboard.begin(); // ← refused after kill(), logs a warning ``` -------------------------------- ### write() Source: https://github.com/hijelhub/hijelhid_blekeyboard/blob/main/README.md Send characters one at a time using write(). ```APIDOC ## write() ### Description Send characters one at a time using `write()`. ### Method write(character) ### Parameters - **character** (uint8_t) - The character to send. ### Example ```.ino const char* str = "Writing Hello from ESP32!"; for (int i = 0; str[i] != '\0'; i++) { keyboard.write((uint8_t)str[i]); } keyboard.tap(KEY_RETURN); ``` ``` -------------------------------- ### isConnected() Source: https://github.com/hijelhub/hijelhid_blekeyboard/blob/main/README.md Checks whether a host is connected before sending keys. Returns true if a host is connected. ```APIDOC ## isConnected() ### Description Check whether a host is connected before sending keys. ### Method isConnected() ### Parameters None ### Returns - **bool** - true if a host is connected, false otherwise. ### Example ```.ino void loop() { if (keyboard.isConnected()) { keyboard.tap(KEY_A); delay(2000); } } ``` ``` -------------------------------- ### Adjust tap timing globally and per-call Source: https://github.com/hijelhub/hijelhid_blekeyboard/blob/main/README.md Shows how to change the hold time and gap between key taps, both globally and for a single tap() call. The default hold is 25ms and the default gap is 25ms. Overrides are passed as extra arguments to tap(). ```ino // Change timing globally (affects all tap() and print/println calls) keyboard.setTapDelay(40); // hold each key for 40ms keyboard.setKeyGap(40); // wait 40ms after each release // Override timing for a single tap keyboard.tap(KEY_A); // uses global timing keyboard.tap(KEY_A, 0, 60, 40); // hold 60ms, gap 40ms keyboard.tap(KEY_A, KEY_MOD_LSHIFT, 60, 40); // with modifier + custom timing keyboard.tap(MEDIA_VOLUME_UP, 60, 40); // media key with custom timing ``` -------------------------------- ### Wait for full authentication with isPaired() Source: https://github.com/hijelhub/hijelhid_blekeyboard/blob/main/README.md isPaired() is more reliable than isConnected() because it returns true only after the host has fully authenticated. This snippet waits until the keyboard is fully ready before sending a message. ```Arduino // Wait until fully ready before sending while (!keyboard.isPaired()) { delay(10); } keyboard.println("Ready!"); ``` -------------------------------- ### Send characters one by one with write() Source: https://github.com/hijelhub/hijelhid_blekeyboard/blob/main/README.md write() sends individual characters. This loop writes each character of a string, then taps Return to send the line. ```Arduino const char* str = "Writing Hello from ESP32!"; for (int i = 0; str[i] != '\0'; i++) { keyboard.write((uint8_t)str[i]); } keyboard.tap(KEY_RETURN); ``` -------------------------------- ### Set battery level Source: https://github.com/hijelhub/hijelhid_blekeyboard/blob/main/README.md Updates the battery percentage reported to the host. Call setBatteryLevel() with a value from 0 to 100. ```ino keyboard.setBatteryLevel(85); // Report 85% battery ``` -------------------------------- ### Tap a single key with modifiers using keyboard.tap() Source: https://github.com/hijelhub/hijelhid_blekeyboard/blob/main/README.md Use tap() to press and release a key automatically. Supports KEY_* constants and optional modifiers like KEY_MOD_LSHIFT, KEY_MOD_LCTRL, and combined modifiers with |. Ideal for simple key presses without manual timing. ```Arduino // Tap a single key keyboard.tap(KEY_RETURN); keyboard.tap(KEY_SPACE); keyboard.tap(KEY_ESCAPE); // Tap with a modifier (Shift, Ctrl, Alt, etc.) keyboard.tap(KEY_A, KEY_MOD_LSHIFT); // Types uppercase 'A' keyboard.tap(KEY_C, KEY_MOD_LCTRL); // Ctrl+C (copy) keyboard.tap(KEY_Z, KEY_MOD_LCTRL); // Ctrl+Z (undo) // Multiple modifiers — combine with | keyboard.tap(KEY_DELETE, KEY_MOD_LCTRL | KEY_MOD_LALT); // Ctrl+Alt+Del ``` -------------------------------- ### Check if a bond is stored Source: https://github.com/hijelhub/hijelhid_blekeyboard/blob/main/README.md Checks whether a device is already bonded. If true, a device is already paired and can be used without re-pairing. ```ino if (keyboard.isBonded()) { Serial.println("A device is already bonded."); } ``` -------------------------------- ### Clear all bonded devices Source: https://github.com/hijelhub/hijelhid_blekeyboard/blob/main/README.md Forgets all previously paired devices and forces re-pairing on the next connection. ```ino keyboard.clearBonds(); ``` -------------------------------- ### isPaired() Source: https://github.com/hijelhub/hijelhid_blekeyboard/blob/main/README.md A more reliable ready-to-send signal than isConnected(). Returns true only after the host has fully authenticated. ```APIDOC ## isPaired() ### Description A more reliable ready-to-send signal than `isConnected()`. It returns `true` only after the host has fully authenticated — `isConnected()` becomes true briefly before the encryption handshake completes on reconnect, which can cause the first report to be dropped. ### Method isPaired() ### Parameters None ### Returns - **bool** - true if the host is fully authenticated and ready, false otherwise. ### Example ```.ino // Wait until fully ready before sending while (!keyboard.isPaired()) { delay(10); } keyboard.println("Ready!"); ``` ``` -------------------------------- ### Set TX Power Level Source: https://github.com/hijelhub/hijelhid_blekeyboard/blob/main/README.md Reduces BLE radio transmit power to save energy at close range. Valid levels are 1–8, with 1 being the lowest TX power and 8 the default. Use lower values for lower power consumption. ```ino keyboard.setTxPower(1); // -12 dBm, lowest range/setting keyboard.setTxPower(8); // +9 dBm, maximum range/setting (default) ``` -------------------------------- ### getIdleTime() Source: https://github.com/hijelhub/hijelhid_blekeyboard/blob/main/README.md Returns the number of milliseconds since the last HID report was sent. Use it to decide when to enter light or deep sleep. ```APIDOC ## getIdleTime() ### Description Returns the number of milliseconds since the last HID report was sent. Use it in your sketch to decide when to enter light or deep sleep. ### Method getIdleTime() ### Parameters None ### Returns - **unsigned long** - milliseconds since the last HID report was sent. ### Example ```.ino if (keyboard.isPaired() && keyboard.getIdleTime() > 30000) { keyboard.beforeSleep(); esp_light_sleep_start(); keyboard.afterWake(); } ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.