diff --git a/components/ble_keyboard/ble_keyboard.cpp b/components/ble_keyboard/ble_keyboard.cpp index d2de64e..b93e3f3 100644 --- a/components/ble_keyboard/ble_keyboard.cpp +++ b/components/ble_keyboard/ble_keyboard.cpp @@ -15,25 +15,37 @@ namespace ble_keyboard { static const char *const TAG = "ble_keyboard"; void Esp32BleKeyboard::setup() { - ESP_LOGI(TAG, "Setting up..."); + if (this->setup_) { + ESP_LOGW(TAG, "BLE keyboard already setup."); + return; + } + + ESP_LOGCONFIG(TAG, "Setting up BLE keyboard..."); bleKeyboard.begin(); pServer = BLEDevice::getServer(); - ESP_LOGD(TAG, "advertiseOnDisconnect(%s)", this->reconnect_ ? "true" : "false"); + ESP_LOGCONFIG(TAG, "advertiseOnDisconnect(%s)", this->reconnect_ ? "true" : "false"); pServer->advertiseOnDisconnect(this->reconnect_); if (!this->advertise_on_start_) { - ESP_LOGD(TAG, "stopAdvertising()"); + ESP_LOGCONFIG(TAG, "stopAdvertising() because advertise_on_start is false"); pServer->stopAdvertising(); } bleKeyboard.releaseAll(); + this->setup_ = true; + ESP_LOGCONFIG(TAG, "Finished setting up up BLE keyboard."); } void Esp32BleKeyboard::stop() { - ESP_LOGD("ble", "stop()"); + if (!this->setup_) { + ESP_LOGW(TAG, "Attempting to use without setup. Not doing anything."); + return; + } + + ESP_LOGD(TAG, "stop()"); if (this->reconnect_) { ESP_LOGD(TAG, "advertiseOnDisconnect(false)"); pServer->advertiseOnDisconnect(false); @@ -51,6 +63,10 @@ void Esp32BleKeyboard::stop() { } void Esp32BleKeyboard::start() { + if (!this->setup_) { + ESP_LOGW(TAG, "Attempting to use without setup. Not doing anything."); + return; + } ESP_LOGD(TAG, "start()"); if (this->reconnect_) { ESP_LOGD(TAG, "advertiseOnDisconnect(true)"); @@ -78,6 +94,10 @@ void Esp32BleKeyboard::update_timer() { } void Esp32BleKeyboard::press(std::string message) { + if (!this->setup_) { + ESP_LOGW(TAG, "Attempting to use without setup. Not doing anything."); + return; + } if (this->is_connected()) { if (message.length() >= 5) { for (unsigned i = 0; i < message.length(); i += 5) { @@ -94,6 +114,10 @@ void Esp32BleKeyboard::press(std::string message) { } void Esp32BleKeyboard::press(uint8_t key, bool with_timer) { + if (!this->setup_) { + ESP_LOGW(TAG, "Attempting to use without setup. Not doing anything."); + return; + } if (this->is_connected()) { if (with_timer) { this->update_timer(); @@ -104,6 +128,10 @@ void Esp32BleKeyboard::press(uint8_t key, bool with_timer) { } void Esp32BleKeyboard::press(MediaKeyReport key, bool with_timer) { + if (!this->setup_) { + ESP_LOGW(TAG, "Attempting to use without setup. Not doing anything."); + return; + } if (this->is_connected()) { if (with_timer) { this->update_timer(); @@ -113,6 +141,10 @@ void Esp32BleKeyboard::press(MediaKeyReport key, bool with_timer) { } void Esp32BleKeyboard::release() { + if (!this->setup_) { + ESP_LOGW(TAG, "Attempting to use without setup. Not doing anything."); + return; + } if (this->is_connected()) { this->cancel_timeout((const std::string) TAG); bleKeyboard.releaseAll(); diff --git a/components/ble_keyboard/ble_keyboard.h b/components/ble_keyboard/ble_keyboard.h index 08bee4b..d3580ce 100644 --- a/components/ble_keyboard/ble_keyboard.h +++ b/components/ble_keyboard/ble_keyboard.h @@ -12,7 +12,8 @@ namespace esphome { namespace ble_keyboard { class Esp32BleKeyboard : public PollingComponent { public: - Esp32BleKeyboard(std::string name, std::string manufacturer_id, uint8_t battery_level, bool reconnect, bool advertise_on_start) + Esp32BleKeyboard(std::string name, std::string manufacturer_id, uint8_t battery_level, bool reconnect, + bool advertise_on_start) : PollingComponent(1000), bleKeyboard(name, manufacturer_id, battery_level) { reconnect_ = reconnect; advertise_on_start_ = advertise_on_start; @@ -47,6 +48,7 @@ class Esp32BleKeyboard : public PollingComponent { BLEServer *pServer; BleKeyboard bleKeyboard; + bool setup_{false}; bool reconnect_{true}; bool advertise_on_start_{true}; uint32_t default_delay_{100};