WIP: examples/bluetooth: Add BLE HID mouse and keyboard examples.#6559
WIP: examples/bluetooth: Add BLE HID mouse and keyboard examples.#6559dpgeorge wants to merge 1 commit intomicropython:masterfrom
Conversation
Signed-off-by: Damien George <[email protected]>
jimmo
left a comment
There was a problem hiding this comment.
Thanks! This has been on my TODO list for a long time :)
| b"\x09\x09MP-mouse" # complete local name | ||
| ) | ||
| conn_handle = None | ||
| ble.gap_advertise(100_000, adv) |
There was a problem hiding this comment.
import ble_advertising
_ADV_APPEARANCE_MOUSE = const(962)
adv = ble_advertising.advertising_payload(services=[UUID(0x1812)], appearance=_ADV_APPEARANCE_MOUSE, name="MP-mouse")There was a problem hiding this comment.
I kind of wanted this example to be raw and just use the bluetooth module as-is, without any helpers. As a way to show exactly what's needed to make a BLE HID, to show there's no complexity hiding anywhere. IMO that's valuable for learning about BLE (and HID). That's also why I added mouse and keyboard as separate but very similar files.
So maybe we can have a "raw" example (perhaps just mouse, it's simpler) and then a more "friendly" version which has both keyboard and mouse support (selectable via some option).
And then eventually also a proper (async) HID library, but that's for the future.
| b"\x0c\x09MP-keyboard" # complete local name | ||
| ) | ||
| conn_handle = None | ||
| ble.gap_advertise(100_000, adv) |
There was a problem hiding this comment.
import ble_advertising
_ADV_APPEARANCE_KEYBOARD = const(961)
adv = ble_advertising.advertising_payload(services=[UUID(0x1812)], appearance=_ADV_APPEARANCE_KEYBOARD, name="MP-keyboard")|
|
||
|
|
||
| def send_mouse(button_mask, x, y, wheel): | ||
| ble.gatts_notify(conn_handle, h_rep, struct.pack("4B", button_mask, x, y, wheel)) |
|
|
||
| def ble_irq(event, data): | ||
| global conn_handle | ||
| if event == 1: |
There was a problem hiding this comment.
_IRQ_CENTRAL_CONNECT = const(1)
_IRQ_CENTRAL_DISCONNECT = const(2)
if event == _IRQ_CENTRAL_CONNECT:
print("connect")
conn_handle, _, _ = data
elif event == _IRQ_CENTRAL_DISCONNECT:
conn_handle = None
There was a problem hiding this comment.
Another suggestion would be to start again advertising upon disconnect.
def advertise():
global conn_handle
conn_handle = None
adv = (
b"\x02\x01\x06"
b"\x03\x03\x12\x18" # complete list of 16-bit service UUIDs: 0x1812
b"\x03\x19\xc2\x03" # appearance: mouse
b"\x09\x09MP-mouse" # complete local name
)
ble.gap_advertise(100_000, adv)
Create global variable conn_handle and call advertise() here (in disconnect) in addition to calling it after registering the service. Thanks for this example, it was exactly what I needed to get myself started!
|
from HOGP_SPEC_V10.pdf - Table 3.1: HID Device Service Requirements HID Service M Both Bonding and encryption are Mandatory. These are required for ios and for windows. |
|
What about this is still a WIP? |
|
Hello, I tried to use "ble_hid_keyboard.py" to create a Bluetooth keyboard, but windows shows a driver error when it is connected to the Bluetooth keyboard. |
|
I am looking for similar simple sample code to "consume" HID output reports like the Keyboard LEDs, which are specified in the HID descriptor. |
This PR adds simple BLE HID mouse and keyboard examples. It is work-in-progress, the examples work with an Android phone (tested with PYBD-SF6) but may not work with other devices.