Using ESP32 as a bluetooth CLASSIC keyboard - I need a code

Hi there. I am trying to find how to use a Bluetooth classic connection to smartphone with ESP working as a simple keyboard. I have problems with compiling code for HID and Bluetooth Classic. Tried IDF profile and Arduino and some example code and still not working.
Has somebody working piece of code for that configuration?
I need classic Bluetooth because BLE has to short range for me.

You can easily take the ESP-IDF example for Bluetooth Classic + HID mouse

https://github.com/espressif/esp-idf/blob/v5.5/examples/bluetooth/bluedroid/classic_bt/bt_hid_mouse_device

Then change up the HID descriptor to be a keyboard instead of a mouse, based on

https://gist.github.com/manuelbl/66f059effc8a7be148adb1f104666467

And then you get a working Bluetooth Classic HID keyboard

https://github.com/maxgerhardt/esp32-btc-hid-example

It can pair with my PC perfectly fine and types ‘a’ once every two seconds.

1 Like

Thanks, I used your project.

It works.

// Task to send HID codes from 0x00 to 0xFF every 2 second
void keyboard_task(void *pvParameters)
{
    const char *TAG = "keyboard_task";
    ESP_LOGI(TAG, "starting HEX loop");

    // 
    InputReport report = { .modifiers = 0, .reserved = 0, .pressedKeys = {0, 0, 0, 0, 0, 0} };
    uint8_t current_hex = 0x00;

    for (;;) {
        // 
        report.pressedKeys[0] = current_hex;

        // 1. 
        send_keyboard_report(&report);
        ESP_LOGI(TAG, "Sent HID code: 0x%02X", current_hex);

        // 2. 
        vTaskDelay(100 / portTICK_PERIOD_MS);

        // 3. 
        report.pressedKeys[0] = 0;
        send_keyboard_report(&report);

        // 4. 
        current_hex++;

        // 5. 
        vTaskDelay(2000 / portTICK_PERIOD_MS);
    }
}

Max Gerhardt took an official example from the chip manufacturer (Espressif)—which was originally designed to work only within the native ESP-IDF environment—and restructured it so that you can easily run it using PlatformIO inside VS Code.

In short, he has “translated” a complex professional example into a format that the community.platformio ecosystem can understand, allowing you to build and flash the Bluetooth HID keyboard firmware without manually setting up the entire ESP-IDF toolchain.