mirror of
https://github.com/T-vK/ESP32-BLE-Keyboard.git
synced 2026-02-20 01:41:32 +01:00
Implemented NimBLE (optional)
removed xTaskCreate in begin() removed BleConnectionStatus removed KeyboardOutputCallbacks implemented callbacks directly in BleKeyboard
This commit is contained in:
@@ -1,25 +0,0 @@
|
||||
#include "BleConnectionStatus.h"
|
||||
|
||||
BleConnectionStatus::BleConnectionStatus(void) {
|
||||
}
|
||||
|
||||
void BleConnectionStatus::onConnect(BLEServer* pServer)
|
||||
{
|
||||
this->connected = true;
|
||||
BLE2902* desc = (BLE2902*)this->inputKeyboard->getDescriptorByUUID(BLEUUID((uint16_t)0x2902));
|
||||
desc->setNotifications(true);
|
||||
|
||||
desc = (BLE2902*)this->inputMediaKeys->getDescriptorByUUID(BLEUUID((uint16_t)0x2902));
|
||||
desc->setNotifications(true);
|
||||
}
|
||||
|
||||
void BleConnectionStatus::onDisconnect(BLEServer* pServer)
|
||||
{
|
||||
this->connected = false;
|
||||
BLE2902* desc = (BLE2902*)this->inputKeyboard->getDescriptorByUUID(BLEUUID((uint16_t)0x2902));
|
||||
desc->setNotifications(false);
|
||||
|
||||
desc = (BLE2902*)this->inputMediaKeys->getDescriptorByUUID(BLEUUID((uint16_t)0x2902));
|
||||
desc->setNotifications(false);
|
||||
pAdvertising->start();
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
#ifndef ESP32_BLE_CONNECTION_STATUS_H
|
||||
#define ESP32_BLE_CONNECTION_STATUS_H
|
||||
#include "sdkconfig.h"
|
||||
#if defined(CONFIG_BT_ENABLED)
|
||||
|
||||
#include <BLEServer.h>
|
||||
#include "BLE2902.h"
|
||||
#include "BLECharacteristic.h"
|
||||
|
||||
class BleConnectionStatus : public BLEServerCallbacks
|
||||
{
|
||||
public:
|
||||
BleConnectionStatus(void);
|
||||
bool connected = false;
|
||||
void onConnect(BLEServer* pServer);
|
||||
void onDisconnect(BLEServer* pServer);
|
||||
BLECharacteristic* inputKeyboard;
|
||||
BLECharacteristic* outputKeyboard;
|
||||
BLECharacteristic* inputMediaKeys;
|
||||
BLEAdvertising *pAdvertising;
|
||||
};
|
||||
|
||||
#endif // CONFIG_BT_ENABLED
|
||||
#endif // ESP32_BLE_CONNECTION_STATUS_H
|
||||
124
BleKeyboard.cpp
124
BleKeyboard.cpp
@@ -1,14 +1,18 @@
|
||||
#if defined(USE_NIMBLE)
|
||||
#include <NimBLEDevice.h>
|
||||
#include <NimBLEServer.h>
|
||||
#include <NimBLEUtils.h>
|
||||
#else
|
||||
#include <BLEDevice.h>
|
||||
#include <BLEUtils.h>
|
||||
#include <BLEServer.h>
|
||||
#include "BLE2902.h"
|
||||
#include "BLEHIDDevice.h"
|
||||
#endif // USE_NIMBLE
|
||||
#include "HIDTypes.h"
|
||||
#include <driver/adc.h>
|
||||
#include "sdkconfig.h"
|
||||
|
||||
#include "BleConnectionStatus.h"
|
||||
#include "KeyboardOutputCallbacks.h"
|
||||
#include "BleKeyboard.h"
|
||||
|
||||
#if defined(CONFIG_ARDUHAL_ESP_LOG)
|
||||
@@ -89,17 +93,47 @@ static const uint8_t _hidReportDescriptor[] = {
|
||||
END_COLLECTION(0) // END_COLLECTION
|
||||
};
|
||||
|
||||
BleKeyboard::BleKeyboard(std::string deviceName, std::string deviceManufacturer, uint8_t batteryLevel) : hid(0)
|
||||
{
|
||||
this->deviceName = deviceName;
|
||||
this->deviceManufacturer = deviceManufacturer;
|
||||
this->batteryLevel = batteryLevel;
|
||||
this->connectionStatus = new BleConnectionStatus();
|
||||
}
|
||||
BleKeyboard::BleKeyboard(std::string deviceName, std::string deviceManufacturer, uint8_t batteryLevel)
|
||||
: hid(0)
|
||||
, deviceName(deviceName)
|
||||
, deviceManufacturer(deviceManufacturer)
|
||||
, batteryLevel(batteryLevel) {}
|
||||
|
||||
void BleKeyboard::begin(void)
|
||||
{
|
||||
xTaskCreate(this->taskServer, "server", 20000, (void *)this, 5, NULL);
|
||||
BLEDevice::init(deviceName);
|
||||
BLEServer* pServer = BLEDevice::createServer();
|
||||
pServer->setCallbacks(this);
|
||||
|
||||
hid = new BLEHIDDevice(pServer);
|
||||
inputKeyboard = hid->inputReport(KEYBOARD_ID); // <-- input REPORTID from report map
|
||||
outputKeyboard = hid->outputReport(KEYBOARD_ID);
|
||||
inputMediaKeys = hid->inputReport(MEDIA_KEYS_ID);
|
||||
|
||||
outputKeyboard->setCallbacks(this);
|
||||
|
||||
hid->manufacturer()->setValue(deviceManufacturer);
|
||||
|
||||
hid->pnp(0x02, 0xe502, 0xa111, 0x0210);
|
||||
hid->hidInfo(0x00, 0x01);
|
||||
|
||||
BLESecurity* pSecurity = new BLESecurity();
|
||||
|
||||
pSecurity->setAuthenticationMode(ESP_LE_AUTH_BOND);
|
||||
|
||||
hid->reportMap((uint8_t*)_hidReportDescriptor, sizeof(_hidReportDescriptor));
|
||||
hid->startServices();
|
||||
|
||||
onStarted(pServer);
|
||||
|
||||
advertising = pServer->getAdvertising();
|
||||
advertising->setAppearance(HID_KEYBOARD);
|
||||
advertising->addServiceUUID(hid->hidService()->getUUID());
|
||||
advertising->setScanResponse(false);
|
||||
advertising->start();
|
||||
hid->setBatteryLevel(batteryLevel);
|
||||
|
||||
ESP_LOGD(LOG_TAG, "Advertising started!");
|
||||
}
|
||||
|
||||
void BleKeyboard::end(void)
|
||||
@@ -107,7 +141,7 @@ void BleKeyboard::end(void)
|
||||
}
|
||||
|
||||
bool BleKeyboard::isConnected(void) {
|
||||
return this->connectionStatus->connected;
|
||||
return this->connected;
|
||||
}
|
||||
|
||||
void BleKeyboard::setBatteryLevel(uint8_t level) {
|
||||
@@ -121,54 +155,16 @@ void BleKeyboard::setName(std::string deviceName) {
|
||||
this->deviceName = deviceName;
|
||||
}
|
||||
|
||||
void BleKeyboard::taskServer(void* pvParameter) {
|
||||
BleKeyboard* bleKeyboardInstance = (BleKeyboard *) pvParameter; //static_cast<BleKeyboard *>(pvParameter);
|
||||
BLEDevice::init(bleKeyboardInstance->deviceName);
|
||||
BLEServer *pServer = BLEDevice::createServer();
|
||||
pServer->setCallbacks(bleKeyboardInstance->connectionStatus);
|
||||
|
||||
bleKeyboardInstance->hid = new BLEHIDDevice(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(pServer);
|
||||
|
||||
bleKeyboardInstance->connectionStatus->pAdvertising = pServer->getAdvertising();
|
||||
bleKeyboardInstance->connectionStatus->pAdvertising->setAppearance(HID_KEYBOARD);
|
||||
bleKeyboardInstance->connectionStatus->pAdvertising->addServiceUUID(bleKeyboardInstance->hid->hidService()->getUUID());
|
||||
bleKeyboardInstance->connectionStatus->pAdvertising->setScanResponse(false);
|
||||
bleKeyboardInstance->connectionStatus->pAdvertising->start();
|
||||
bleKeyboardInstance->hid->setBatteryLevel(bleKeyboardInstance->batteryLevel);
|
||||
|
||||
ESP_LOGD(LOG_TAG, "Advertising started!");
|
||||
vTaskDelay(portMAX_DELAY); //delay(portMAX_DELAY);
|
||||
}
|
||||
|
||||
void BleKeyboard::sendReport(KeyReport* keys)
|
||||
{
|
||||
if (this->isConnected())
|
||||
{
|
||||
this->inputKeyboard->setValue((uint8_t*)keys, sizeof(KeyReport));
|
||||
this->inputKeyboard->notify();
|
||||
}
|
||||
#if defined(USE_NIMBLE)
|
||||
vTaskDelay(1);
|
||||
#endif // USE_NIMBLE
|
||||
}
|
||||
}
|
||||
|
||||
void BleKeyboard::sendReport(MediaKeyReport* keys)
|
||||
@@ -177,7 +173,10 @@ void BleKeyboard::sendReport(MediaKeyReport* keys)
|
||||
{
|
||||
this->inputMediaKeys->setValue((uint8_t*)keys, sizeof(MediaKeyReport));
|
||||
this->inputMediaKeys->notify();
|
||||
}
|
||||
#if defined(USE_NIMBLE)
|
||||
vTaskDelay(1);
|
||||
#endif // USE_NIMBLE
|
||||
}
|
||||
}
|
||||
|
||||
extern
|
||||
@@ -466,3 +465,20 @@ size_t BleKeyboard::write(const uint8_t *buffer, size_t size) {
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
void BleKeyboard::onConnect(BLEServer* pServer) {
|
||||
this->connected = true;
|
||||
}
|
||||
|
||||
void BleKeyboard::onDisconnect(BLEServer* pServer) {
|
||||
this->connected = false;
|
||||
#if !defined(USE_NIMBLE)
|
||||
advertising->start();
|
||||
#endif // !USE_NIMBLE
|
||||
}
|
||||
|
||||
void BleKeyboard::onWrite(BLECharacteristic* me) {
|
||||
uint8_t* value = (uint8_t*)(me->getValue().c_str());
|
||||
(void)value;
|
||||
ESP_LOGI(LOG_TAG, "special keys: %d", *value);
|
||||
}
|
||||
@@ -3,9 +3,26 @@
|
||||
#include "sdkconfig.h"
|
||||
#if defined(CONFIG_BT_ENABLED)
|
||||
|
||||
#include "BleConnectionStatus.h"
|
||||
#if defined(USE_NIMBLE)
|
||||
|
||||
#include "NimBLECharacteristic.h"
|
||||
#include "NimBLEHIDDevice.h"
|
||||
|
||||
#define BLEDevice NimBLEDevice
|
||||
#define BLEServerCallbacks NimBLEServerCallbacks
|
||||
#define BLECharacteristicCallbacks NimBLECharacteristicCallbacks
|
||||
#define BLEHIDDevice NimBLEHIDDevice
|
||||
#define BLECharacteristic NimBLECharacteristic
|
||||
#define BLEAdvertising NimBLEAdvertising
|
||||
#define BLEServer NimBLEServer
|
||||
|
||||
#else
|
||||
|
||||
#include "BLEHIDDevice.h"
|
||||
#include "BLECharacteristic.h"
|
||||
|
||||
#endif // USE_NIMBLE
|
||||
|
||||
#include "Print.h"
|
||||
|
||||
|
||||
@@ -86,17 +103,21 @@ typedef struct
|
||||
uint8_t keys[6];
|
||||
} KeyReport;
|
||||
|
||||
class BleKeyboard : public Print
|
||||
class BleKeyboard : public Print, public BLEServerCallbacks, public BLECharacteristicCallbacks
|
||||
{
|
||||
private:
|
||||
BleConnectionStatus* connectionStatus;
|
||||
BLEHIDDevice* hid;
|
||||
BLECharacteristic* inputKeyboard;
|
||||
BLECharacteristic* outputKeyboard;
|
||||
BLECharacteristic* inputMediaKeys;
|
||||
BLEAdvertising* advertising;
|
||||
KeyReport _keyReport;
|
||||
MediaKeyReport _mediaKeyReport;
|
||||
static void taskServer(void* pvParameter);
|
||||
std::string deviceName;
|
||||
std::string deviceManufacturer;
|
||||
uint8_t batteryLevel;
|
||||
bool connected = false;
|
||||
|
||||
public:
|
||||
BleKeyboard(std::string deviceName = "ESP32 BLE Keyboard", std::string deviceManufacturer = "Espressif", uint8_t batteryLevel = 100);
|
||||
void begin(void);
|
||||
@@ -114,11 +135,12 @@ public:
|
||||
bool isConnected(void);
|
||||
void setBatteryLevel(uint8_t level);
|
||||
void setName(std::string deviceName);
|
||||
uint8_t batteryLevel;
|
||||
std::string deviceManufacturer;
|
||||
std::string deviceName;
|
||||
protected:
|
||||
virtual void onStarted(BLEServer *pServer) { };
|
||||
virtual void onConnect(BLEServer* pServer) override;
|
||||
virtual void onDisconnect(BLEServer* pServer) override;
|
||||
virtual void onWrite(BLECharacteristic* me) override;
|
||||
|
||||
};
|
||||
|
||||
#endif // CONFIG_BT_ENABLED
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
#include "KeyboardOutputCallbacks.h"
|
||||
|
||||
#if defined(CONFIG_ARDUHAL_ESP_LOG)
|
||||
#include "esp32-hal-log.h"
|
||||
#define LOG_TAG ""
|
||||
#else
|
||||
#include "esp_log.h"
|
||||
static const char* LOG_TAG = "BLEDevice";
|
||||
#endif
|
||||
|
||||
KeyboardOutputCallbacks::KeyboardOutputCallbacks(void) {
|
||||
}
|
||||
|
||||
void KeyboardOutputCallbacks::onWrite(BLECharacteristic* me) {
|
||||
uint8_t* value = (uint8_t*)(me->getValue().c_str());
|
||||
ESP_LOGI(LOG_TAG, "special keys: %d", *value);
|
||||
}
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
#ifndef ESP32_BLE_KEYBOARD_OUTPUT_CALLBACKS_H
|
||||
#define ESP32_BLE_KEYBOARD_OUTPUT_CALLBACKS_H
|
||||
#include "sdkconfig.h"
|
||||
#if defined(CONFIG_BT_ENABLED)
|
||||
|
||||
#include <BLEServer.h>
|
||||
#include "BLE2902.h"
|
||||
#include "BLECharacteristic.h"
|
||||
|
||||
class KeyboardOutputCallbacks : public BLECharacteristicCallbacks
|
||||
{
|
||||
public:
|
||||
KeyboardOutputCallbacks(void);
|
||||
void onWrite(BLECharacteristic* me);
|
||||
};
|
||||
|
||||
#endif // CONFIG_BT_ENABLED
|
||||
#endif // ESP32_BLE_KEYBOARD_OUTPUT_CALLBACKS_H
|
||||
Reference in New Issue
Block a user