ESP32-BLE-Keyboard
ESP32-BLE-Keyboard copied to clipboard
Some optimization suggestions
hi @T-vK ,This is a great library,I have some problems using this,I made some modifications to make it work better, https://github.com/T-vK/ESP32-BLE-Keyboard/blob/928c09320e3e6e344a63640c7de21d52a20c1d47/BleKeyboard.cpp#L102 Here the task to create a 20000byte, this should not be necessary, it takes up a lot of memory,https://github.com/T-vK/ESP32-BLE-Keyboard/issues/40 I modified it to
void BleKeyboard::begin(void)
{
// xTaskCreate(this->taskServer, "server", 20000, (void *)this, 5, NULL);
this->taskServer((void *)this);
}
It also works 2.In the course of use, they seem to be difficult to be found and connected successfully, and they will not be disconnected and reconnected. Reference bledemo, are missing some steps
I modified it to BleKeyboard.h
class BleKeyboard : public Print
{
private:
BleConnectionStatus* connectionStatus;
BLEHIDDevice* hid;
BLECharacteristic* inputKeyboard;
BLECharacteristic* outputKeyboard;
BLECharacteristic* inputMediaKeys;
KeyReport _keyReport;
MediaKeyReport _mediaKeyReport;
//add
BLEServer *pServer = NULL;
static void taskServer(void* pvParameter);
public:
BleKeyboard(std::string deviceName = "RTSCAN Scanner", std::string deviceManufacturer = "RTSCAN", uint8_t batteryLevel = 100);
void begin(void);
void end(void);
void sendReport(KeyReport* keys);
void sendReport(MediaKeyReport* keys);
size_t press(uint8_t k);
size_t press(const MediaKeyReport k);
size_t release(uint8_t k);
size_t release(const MediaKeyReport k);
size_t write(uint8_t c);
size_t write(const MediaKeyReport c);
size_t write(const uint8_t *buffer, size_t size);
void releaseAll(void);
bool isConnected(void);
void setBatteryLevel(uint8_t level);
uint8_t batteryLevel;
std::string deviceManufacturer;
std::string deviceName;
//add
void startAdvertising();
protected:
virtual void onStarted(BLEServer *pServer) { };
};
add
BLEServer *pServer = NULL;
void startAdvertising();
BleKeyboard.cpp
void BleKeyboard::taskServer(void* pvParameter) {
BleKeyboard* bleKeyboardInstance = (BleKeyboard *) pvParameter; //static_cast<BleKeyboard *>(pvParameter);
BLEDevice::init(bleKeyboardInstance->deviceName);
//modify
bleKeyboardInstance->pServer = BLEDevice::createServer();
bleKeyboardInstance->pServer->setCallbacks(bleKeyboardInstance->connectionStatus);
bleKeyboardInstance->hid = new BLEHIDDevice(bleKeyboardInstance->pServer);
bleKeyboardInstance->inputKeyboard = bleKeyboardInstance->hid->inputReport(KEYBOARD_ID); // <-- input REPORTID from report map
bleKeyboardInstance->outputKeyboard = bleKeyboardInstance->hid->outputReport(KEYBOARD_ID);
bleKeyboardInstance->inputMediaKeys = bleKeyboardInstance->hid->inputReport(MEDIA_KEYS_ID);
bleKeyboardInstance->connectionStatus->inputKeyboard = bleKeyboardInstance->inputKeyboard;
bleKeyboardInstance->connectionStatus->outputKeyboard = bleKeyboardInstance->outputKeyboard;
bleKeyboardInstance->connectionStatus->inputMediaKeys = bleKeyboardInstance->inputMediaKeys;
bleKeyboardInstance->outputKeyboard->setCallbacks(new KeyboardOutputCallbacks());
bleKeyboardInstance->hid->manufacturer()->setValue(bleKeyboardInstance->deviceManufacturer);
bleKeyboardInstance->hid->pnp(0x02, 0xe502, 0xa111, 0x0210);
bleKeyboardInstance->hid->hidInfo(0x00,0x01);
BLESecurity *pSecurity = new BLESecurity();
pSecurity->setAuthenticationMode(ESP_LE_AUTH_BOND);
bleKeyboardInstance->hid->reportMap((uint8_t*)_hidReportDescriptor, sizeof(_hidReportDescriptor));
bleKeyboardInstance->hid->startServices();
bleKeyboardInstance->onStarted(bleKeyboardInstance->pServer);
BLEAdvertising *pAdvertising = bleKeyboardInstance->pServer->getAdvertising();
pAdvertising->setAppearance(HID_KEYBOARD);
pAdvertising->addServiceUUID(bleKeyboardInstance->hid->hidService()->getUUID());
pAdvertising->setScanResponse(false);
pAdvertising->start();
bleKeyboardInstance->hid->setBatteryLevel(bleKeyboardInstance->batteryLevel);
ESP_LOGD(LOG_TAG, "Advertising started!");
// vTaskDelay(portMAX_DELAY); //delay(portMAX_DELAY);
}
//add
void BleKeyboard::startAdvertising()
{
this->pServer->startAdvertising();
}
ex
#include <BleKeyboard.h>
BleKeyboard bleKeyboard;
bool oldstatus = false;
void setup() {
Serial.begin(115200);
Serial.println("Starting BLE work!");
bleKeyboard.begin();
}
void loop() {
if(bleKeyboard.isConnected()) {
if(oldstatus){
oldstatus = false;
bleKeyboard.startAdvertising();
}
Serial.println("Sending 'Hello world'...");
bleKeyboard.print("Hello world");
delay(1000);
bleKeyboard.releaseAll();
}else{
oldstatus = true;
}
Serial.println("Waiting 5 seconds...");
delay(5000);
}
They work very well after modification, and can automatically connect soon after disconnecting,You can refer to these changes
Sounds great. Can you make a Pull Request for this? We need to test if this still works on Windows, Linux, Mac OS, iOS and Android. Can you help with that? I could only test for Linux and Android.