SkillAgentSearch skills...

MicroPythonBLEHID

Human Interface Device (HID) over Bluetooth Low Energy (BLE) GATT library for MicroPython.

Install / Use

/learn @Heerkog/MicroPythonBLEHID
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

<div id="top"></div> <!-- PROJECT SHIELDS -->

[![Contributors][contributors-shield]][contributors-url] [![Forks][forks-shield]][forks-url] [![Stargazers][stars-shield]][stars-url] [![Issues][issues-shield]][issues-url] [![GPL License][license-shield]][license-url] [![Website][website-shield]][website-url]

<!-- PROJECT HEADER --> <br /> <div align="center"> <h3 align="center">MicroPython Human Interface Device library</h3> <p align="center"> A library that offers implementations of Human Interface Devices (HID) over Bluetooth Low Energy (BLE) GATT for MicroPython. </p> </div> <!-- TABLE OF CONTENTS --> <details> <summary>Table of Contents</summary> <ol> <li><a href="#about-the-project">About The Project</a></li> <li><a href="#requirements">Requirements</a></li> <li><a href="#faq">FAQ</a></li> <li><a href="#getting-started">Getting Started</a></li> <li><a href="#usage">Usage</a></li> <li><a href="#contributing">Contributing</a></li> <li><a href="#license">License</a></li> <li><a href="#contact">Contact</a></li> <li><a href="#acknowledgments">Acknowledgments</a></li> </ol> </details> <!-- ABOUT THE PROJECT -->

About The Project

This library offers implementations of Human Interface Devices (HID) over Bluetooth Low Energy (BLE) GATT for MicroPython. The library has been tested using an ESP32 development board (TinyPICO) as the peripheral and Windows 10 as the central. Examples and basic implementations of HID devices are available for

  • Keyboard,
  • Mouse, and
  • Joystick.

This library is NOT intended to offer functionality for every possible HID device configuration. Instead, the library is designed to offer basic well-documented classes that you can extend to fit your HID device needs. For example, the Mouse class offers a three button mouse with vertical scroll wheel. If you plan on developing a gaming mouse with eight buttons and both vertical and horizontal wheels, you will need to extend the Mouse class and overwrite the required functions to include a new HID report descriptor.

<p align="right">(<a href="#top">back to top</a>)</p> <!-- REQUIREMENTS -->

Requirements

The following is required to use this library:

  • ESP32 chip with
    • Bluetooth
    • 512 kB SRAM or more
  • Micropython v1.18 or higher
<p align="right">(<a href="#top">back to top</a>)</p> <!-- FAQ -->

FAQ

My device doesn't reconnect after power cycling

Please make sure bonding and LE secure is enabled (default). If that doesn't work, please try using NVS storage before calling start():

from hid_keystores import NVSKeyStore

ks = NVSKeyStore()
self.keyboard.set_keystore(ks)

Please make sure to remove the device from the client before connecting. If you do not, the device will expect the keys from the old key store and immediately disconnect.

Using a keyboard and mouse simultaneously

This is not immediately supported. You will need to create a new device that has a multi-device HID descriptor and report. Please see this tutorial for more information and use this tool to create the descriptor.

<p align="right">(<a href="#top">back to top</a>)</p> <!-- GETTING STARTED -->

Getting Started

To get a local copy simply clone this repository.

git clone https://github.com/heerkog/MicroPythonBLEHID.git

The repository is structured as followed:

  • examples/ directory containing some examples.
    • async/ directory containing asynchronous examples.
      • joystick_example.py
      • keyboard_example.py
      • mouse_example.py
    • simple/ directory containing simple examples.
      • joystick_example.py
      • keyboard_example.py
      • mouse_example.py
    • tinypico/ directory containing TinyPICO specific examples. These are mostly personal projects.
  • hid_services.py the library.
  • hid_keystores.py different key stores to use with the library.
  • LICENSE the license.
  • readme.md
<p align="right">(<a href="#top">back to top</a>)</p> <!-- USAGE EXAMPLES -->

Usage

The library offers functionality for creating HID services, advertising them, and setting and notifying the central of HID events. The library does not offer functionality to, for example, send a string of characters to the central using the keyboard service (eventhough this is included in the keyboard example). The reason for this is that such functionality is entirely dependent on the intended use of the services and should be kept outside of this library.

The library consists of five classes with the following functions:

  • HumanInterfaceDevice (Superclass for the HID service classes, implements the Device Information and Battery services, and sets up BLE and advertisement)

    • __init__(device_name) (Initialize the superclass)
    • ble_irq(event, data) (Internal callback function that catches BLE interrupt requests)
    • start() (Starts Device Information and Battery services)
    • stop() (Stops Device Information and Battery services)
    • write_service_characteristics(handles) (Writes Device Information and Battery service characteristics)
    • load_secrets() (Loads stored secrets for Bluetooth bonding)
    • save_secrets() (Saves secrets for Bluetooth bonding)
    • start_advertising() (Starts Bluetooth advertisement)
    • stop_advertising() (Stops Bluetooth advertisement)
    • is_running() (Returns True if services are running, otherwise False)
    • is_connected() (Returns True if a client is connected, otherwise False)
    • is_advertising() (Returns True if advertising, otherwise False)
    • set_state(state) (Sets one of the HumanInterfaceDevice constants DEVICE_STOPPED, DEVICE_IDLE, DEVICE_ADVERTISING, or DEVICE_CONNECTED. Doesn't change the actual function. Used internally)
    • get_state() (Returns one of the HumanInterfaceDevice constants DEVICE_STOPPED, DEVICE_IDLE, DEVICE_ADVERTISING, or DEVICE_CONNECTED)
    • set_state_change_callback(callback) (Sets a callback function that is called when the HumanInterfaceDevice state changes between constants DEVICE_STOPPED, DEVICE_IDLE, DEVICE_ADVERTISING, or DEVICE_CONNECTED))
    • get_device_name() (Returns the device name)
    • get_services_uuids() (Returns the service UUIDs)
    • get_appearance() (Returns the device appearance id)
    • get_battery_level() (Returns the battery level)
    • set_device_information(manufacture_name, model_number, serial_number) (Sets the basic Device Information characteristics. Must be called before calling start())
    • set_device_revision(firmware_revision, hardware_revision, software_revision) (Sets the Device Information revision characteristics. Must be called before calling start())
    • set_device_pnp_information(pnp_manufacturer_source, pnp_manufacturer_uuid, pnp_product_id, pnp_product_version) (Sets the Device Information PnP characteristics. Must be called before calling start())
    • set_bonding(bond) (Set whether to use Bluetooth bonding)
    • set_le_secure(le_secure) (Set whether to use LE secure pairing)
    • set_io_capability(io_capability) (Set input/output capability of this device Determines the pairing procedure, e.g., accept connection/passkey entry/just works. Must be called before calling start())
    • set_passkey_callback(passkey_callback) (Set callback function for pairing events. Callback function should return boolean to accept connection or passkey depending on I/O capability used)
    • set_passkey(passkey) (Set the passkey to use for pairing)
    • set_keystore(keystore) (Sets the key store to use from hid_keystores.py. Default JSONKeyStore)
    • forget_clients() (Removes all client keys from the key store)
    • set_battery_level(level) (Sets the battery level internally)
    • notify_battery_level() (Notifies the client of the current battery level. Call after setting battery level)
    • notify_hid_report() (Function for subclasses to override)
  • Joystick (Subclass of HumanInterfaceDevice, implements joystick service)

    • __init__(name) (Initialize the joystick)
    • start() (Starts the HID service using joystick characteristics. Calls HumanInterfaceDevice.start())
    • write_service_characteristics(handles) (Writes the joystick HID service characteristics. Calls HumanInterfaceDevice.write_service_characteristics(handles))
    • notify_hid_report() (Notifies the client of the internal HID joystick status)
    • set_axes(x, y) (Sets the joystick axes internally)
    • set_buttons(b1, b2, b3, b4, b5, b6, b7, b8) (Sets the joystick buttons internally)
  • Mouse (Subclass of HumanInterfaceDevice, implements mouse service)

    • __init__(name) (Initialize the mouse)
    • start() (Starts the HID service using mouse characteristics. Calls HumanInterfaceDevice.start())
    • write_service_characteristics(handles) (Writes the mouse HID service characteristics. Calls HumanInterfaceDevice.write_service_characteristics(handles))
    • notify_hid_report() (Notifies the client of the internal HID mouse status)
    • set_axes(x, y) (Sets the mouse axes movement internally)
    • set_wheel(w) (Sets the mouse wheel movement internally)
    • set_buttons(b1, b2, b3) (Sets the mouse buttons internally)
  • Keyboard (Subclass of HumanInterfaceDevice, implements keyboard service)

    • __init__(name) (Initialize the keyboard)
    • start() (Starts the HID service using keyboard characteristics. Calls HumanInterfaceDevice.start())
    • write_service_characteristics(handles) (Writes the keyboard HID service characteristics. Calls HumanInterfaceDevice.write_service_characteristics(handles))
    • notify_hid_report() (Notifies the client of the internal HID keyboard status)
    • set_modifiers(right_gui, right_alt, right_shift, right_control, left_gui, left_alt, left_shift, left_control) (Sets the keyboard

Related Skills

View on GitHub
GitHub Stars321
CategoryDevelopment
Updated12d ago
Forks51

Languages

Python

Security Score

100/100

Audited on Mar 11, 2026

No findings