Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions BleKeyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ static const uint8_t _hidReportDescriptor[] = {
END_COLLECTION(0) // END_COLLECTION
};

uint32_t BleKeyboard::_pin = 0;

BleKeyboard::BleKeyboard(std::string deviceName, std::string deviceManufacturer, uint8_t batteryLevel) : hid(0)
{
this->deviceName = deviceName;
Expand All @@ -97,11 +99,17 @@ BleKeyboard::BleKeyboard(std::string deviceName, std::string deviceManufacturer,
this->connectionStatus = new BleConnectionStatus();
}

void BleKeyboard::begin(void)
void BleKeyboard::begin()
{
xTaskCreate(this->taskServer, "server", 20000, (void *)this, 5, NULL);
}

void BleKeyboard::beginPIN(uint32_t pin)
{
_pin = pin;
begin();
}

void BleKeyboard::end(void)
{
}
Expand Down Expand Up @@ -139,7 +147,19 @@ void BleKeyboard::taskServer(void* pvParameter) {

BLESecurity *pSecurity = new BLESecurity();

pSecurity->setAuthenticationMode(ESP_LE_AUTH_BOND);
if(_pin)
{
pSecurity->setAuthenticationMode(ESP_LE_AUTH_REQ_SC_BOND);
esp_ble_gap_set_security_param(ESP_BLE_SM_SET_STATIC_PASSKEY, &_pin, sizeof(_pin));
pSecurity->setCapability(ESP_IO_CAP_OUT);
pSecurity->setKeySize();
pSecurity->setAuthenticationMode(ESP_LE_AUTH_REQ_SC_ONLY);
pSecurity->setInitEncryptionKey(ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK);
}
else
{
pSecurity->setAuthenticationMode(ESP_LE_AUTH_BOND);
}

bleKeyboardInstance->hid->reportMap((uint8_t*)_hidReportDescriptor, sizeof(_hidReportDescriptor));
bleKeyboardInstance->hid->startServices();
Expand Down
4 changes: 3 additions & 1 deletion BleKeyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,12 @@ class BleKeyboard : public Print
BLECharacteristic* inputMediaKeys;
KeyReport _keyReport;
MediaKeyReport _mediaKeyReport;
static uint32_t _pin;
static void taskServer(void* pvParameter);
public:
BleKeyboard(std::string deviceName = "ESP32 BLE Keyboard", std::string deviceManufacturer = "Espressif", uint8_t batteryLevel = 100);
void begin(void);
void begin();
void beginPIN(uint32_t pin);
void end(void);
void sendReport(KeyReport* keys);
void sendReport(MediaKeyReport* keys);
Expand Down