Hi everyone,
I'm an engineer on the TensorFlow Lite Micro team, and I'm interested in extending one of our examples (the magic wand) to appear as a BLE keyboard. For example if a user wrote a "W" using the wand, the Arduino would simulate a "W" keypress and send it to the connected system. The standard Arduino board we use is the Nano BLE Sense 33, and so far I've managed to use some Bluetooth examples to get it to show up as a beacon (so I can set up a name to show up when you scan for devices).
Emulating a Bluetooth keyboard is attractive because it enables a wide variety of systems to be controlled from the board, without requiring custom coding on the receiving side. I'm having trouble finding existing examples that show how to do this on a Nano BLE board though. The closest project I've found is this for the ESP32:
The questions I have are:
Are there any technical blockers I'm not aware of that would prevent me from emulating a keyboard on the Nano BLE?
Are there any existing examples showing how to do this that I've missed?
I'm a BLE noob, but does it seem reasonable for me to attempt to port the ESP32 library to a Nano-compatible version, or would you recommend a different approach?
// simple MIDI over BLE from :-
// https://tigoe.github.io/SoundExamples/midi-ble.html
// see this web page for CC messages as well.
#include <ArduinoBLE.h>
byte midiData[] = {0x80, 0x80, 0x00, 0x00, 0x00};
// set up the MIDI service and MIDI message characteristic:
BLEService midiService("03B80E5A-EDE8-4B33-A751-6CE34EC4C700");
BLECharacteristic midiCharacteristic("7772E5DB-3868-4112-A1A9-F2669D106BF3",
BLEWrite | BLEWriteWithoutResponse |
BLENotify | BLERead, sizeof(midiData));
int bpm = 72; // beats per minute
// duration of a beat in ms
float beatDuration = 60.0 / bpm * 1000;
// the melody sequence:
int melody[] = {64, 66, 71, 73, 74, 66, 64, 73, 71, 66, 74, 73};
// which note of the melody to play:
int noteCounter = 0;
void setup() {
// initialize serial communication
Serial.begin(9600);
// initialize built in LED:
pinMode(LED_BUILTIN, OUTPUT);
// Initialize BLE:
if (!BLE.begin()) {
Serial.println("starting BLE failed!");
while (true);
}
// set local name and advertised service for BLE:
BLE.setLocalName("MIDI_BLE_Nano33_sense");
BLE.setAdvertisedService(midiService);
// add the characteristic and service:
midiService.addCharacteristic(midiCharacteristic);
BLE.addService(midiService);
// start advertising
BLE.advertise();
}
void loop() {
// wait for a BLE central
BLEDevice central = BLE.central();
// if a central is connected to the peripheral:
if (central) {
// turn on LED to indicate connection:
digitalWrite(LED_BUILTIN, HIGH);
// play a note from the melody:
midiCommand(0x90, melody[noteCounter], 127);
// all the notes in this are sixteenth notes,
// which is 1/4 of a beat, so:
int noteDuration = beatDuration / 4;
// keep it on for the appropriate duration:
delay(noteDuration);
// turn the note off:
midiCommand(0x80, melody[noteCounter], 0);
// increment the note number for next time through the loop:
noteCounter++;
// keep the note in the range from 0 - 11 using modulo:
noteCounter = noteCounter % 12;
}
// when the central disconnects, turn off the LED:
digitalWrite(LED_BUILTIN, LOW);
}
// send a 3-byte midi message
void midiCommand(byte cmd, byte data1, byte data2) {
// MIDI data goes in the last three bytes of the midiData array:
midiData[2] = cmd;
midiData[3] = data1;
midiData[4] = data2;
midiCharacteristic.setValue(midiData, sizeof(midiData));
}
I tried it out, and the "Shining Keyboard" example worked perfectly for my purposes (though I had to downgrade my Nano BLE board package to v1.1.6 to avoid compilation issues). I'll be taking this as a starting point and hopefully will be able to make progress from here. Thanks Grumpy_Mike and everyone for your help!
Hi everyone,
I'm new of this forum .
peterwarden , as you I'm interested to use the Arduino BLE as a keyboard an tried the Shining Keyboard , but my PC that run W7 and use CRS Harmony usb adaper for the BT isn't able to connect the Shining Keyboard , that is visible as devices becouse I got the message that was entered a wrong code pin for the pairing , but no pin request is showed at all.
Did you face the same problem ?
Tks. Roberto
I didn't see the problem with the PIN, though I didn't yet try Windows.
I've also tinkered with the original repo, and have upgraded it to work with the latest Mbed board packages, so you no longer have to downgrade to v1.1.6 to use it: