BLE HID Keyboard
Overview
The BLE HID Keyboard sample demonstrates how to use the GATT Human Interface Device (HID) Service to implement a keyboard input device that you can connect to your computer.
This sample code enumerates the nRF52840 Connect Kit into a BLE HID keyboard that has an A key connected to the USER button.
Tip
adafruit_ble
and adafruit_hid
are pre-built into CircuitPython as frozen modules, so that they can be imported in the code directly.
Requirements
Before you start, check that you have the required hardware and software:
Running the code
To run the code, complete the following steps:
- Connect nRF52840 Connect Kit to your computer using the USB-C Cable.
- Start Mu Editor, click Load to open
code.py
in the CIRCUITPY drive. -
Copy and paste the following code into code.py
and click Save:
CIRCUITPY/code.py |
---|
| import board
import digitalio
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.keycode import Keycode
import adafruit_ble
from adafruit_ble.advertising import Advertisement
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
from adafruit_ble.services.standard.hid import HIDService
from adafruit_ble.services.standard.device_info import DeviceInfoService
# USER button acts as A key on a keyboard
key_a = digitalio.DigitalInOut(board.USER)
key_a.direction = digitalio.Direction.INPUT
key_a.pull = digitalio.Pull.UP
# LED0 indicates A key is pressed
led = digitalio.DigitalInOut(board.LED0)
led.direction = digitalio.Direction.OUTPUT
led.value = True
# Use default HID descriptor
hid = HIDService()
device_info = DeviceInfoService(
software_revision=adafruit_ble.__version__, manufacturer="Makerdiary"
)
ble = adafruit_ble.BLERadio()
ble.name = 'CIRCUITPY KEYBOARD'
advertisement = ProvideServicesAdvertisement(hid)
advertisement.complete_name = ble.name
advertisement.appearance = 961
scan_response = Advertisement()
if ble.connected:
for c in ble.connections:
c.disconnect()
print("advertising")
ble.start_advertising(advertisement, scan_response)
keyboard = Keyboard(hid.devices)
while True:
while not ble.connected:
pass
while ble.connected:
if not key_a.value:
print("Key A pressed!")
# Turn on LED0
led.value = False
while not key_a.value:
pass # Wait for key A released
# Type keycode `Shift+A`
keyboard.press(Keycode.SHIFT, Keycode.A)
keyboard.release_all()
# Turn off LED0
led.value = True
ble.start_advertising(advertisement)
|
-
Your code will run as soon as the file is done saving. A Bluetooth keyboard with the name CIRCUITPY KEYBOARD
can be discovered by your computer. Connect to the keyboard.
-
Open a text editor and press USER button on the board. Every button press sends a character A
to your computer, and this will be displayed in the text editor.