Skip to content

Commit 8040eec

Browse files
committed
Work around issue with ESPHome 2023.7.0. Fixed in 2023.7.1.
1 parent 0afb63e commit 8040eec

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

components/ble_keyboard/ble_keyboard.cpp

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,37 @@ namespace ble_keyboard {
1515
static const char *const TAG = "ble_keyboard";
1616

1717
void Esp32BleKeyboard::setup() {
18-
ESP_LOGI(TAG, "Setting up...");
18+
if (this->setup_) {
19+
ESP_LOGW(TAG, "BLE keyboard already setup.");
20+
return;
21+
}
22+
23+
ESP_LOGCONFIG(TAG, "Setting up BLE keyboard...");
1924

2025
bleKeyboard.begin();
2126

2227
pServer = BLEDevice::getServer();
2328

24-
ESP_LOGD(TAG, "advertiseOnDisconnect(%s)", this->reconnect_ ? "true" : "false");
29+
ESP_LOGCONFIG(TAG, "advertiseOnDisconnect(%s)", this->reconnect_ ? "true" : "false");
2530
pServer->advertiseOnDisconnect(this->reconnect_);
2631

2732
if (!this->advertise_on_start_) {
28-
ESP_LOGD(TAG, "stopAdvertising()");
33+
ESP_LOGCONFIG(TAG, "stopAdvertising() because advertise_on_start is false");
2934
pServer->stopAdvertising();
3035
}
3136

3237
bleKeyboard.releaseAll();
38+
this->setup_ = true;
39+
ESP_LOGCONFIG(TAG, "Finished setting up up BLE keyboard.");
3340
}
3441

3542
void Esp32BleKeyboard::stop() {
36-
ESP_LOGD("ble", "stop()");
43+
if (!this->setup_) {
44+
ESP_LOGW(TAG, "Attempting to use without setup. Not doing anything.");
45+
return;
46+
}
47+
48+
ESP_LOGD(TAG, "stop()");
3749
if (this->reconnect_) {
3850
ESP_LOGD(TAG, "advertiseOnDisconnect(false)");
3951
pServer->advertiseOnDisconnect(false);
@@ -51,6 +63,10 @@ void Esp32BleKeyboard::stop() {
5163
}
5264

5365
void Esp32BleKeyboard::start() {
66+
if (!this->setup_) {
67+
ESP_LOGW(TAG, "Attempting to use without setup. Not doing anything.");
68+
return;
69+
}
5470
ESP_LOGD(TAG, "start()");
5571
if (this->reconnect_) {
5672
ESP_LOGD(TAG, "advertiseOnDisconnect(true)");
@@ -78,6 +94,10 @@ void Esp32BleKeyboard::update_timer() {
7894
}
7995

8096
void Esp32BleKeyboard::press(std::string message) {
97+
if (!this->setup_) {
98+
ESP_LOGW(TAG, "Attempting to use without setup. Not doing anything.");
99+
return;
100+
}
81101
if (this->is_connected()) {
82102
if (message.length() >= 5) {
83103
for (unsigned i = 0; i < message.length(); i += 5) {
@@ -94,6 +114,10 @@ void Esp32BleKeyboard::press(std::string message) {
94114
}
95115

96116
void Esp32BleKeyboard::press(uint8_t key, bool with_timer) {
117+
if (!this->setup_) {
118+
ESP_LOGW(TAG, "Attempting to use without setup. Not doing anything.");
119+
return;
120+
}
97121
if (this->is_connected()) {
98122
if (with_timer) {
99123
this->update_timer();
@@ -104,6 +128,10 @@ void Esp32BleKeyboard::press(uint8_t key, bool with_timer) {
104128
}
105129

106130
void Esp32BleKeyboard::press(MediaKeyReport key, bool with_timer) {
131+
if (!this->setup_) {
132+
ESP_LOGW(TAG, "Attempting to use without setup. Not doing anything.");
133+
return;
134+
}
107135
if (this->is_connected()) {
108136
if (with_timer) {
109137
this->update_timer();
@@ -113,6 +141,10 @@ void Esp32BleKeyboard::press(MediaKeyReport key, bool with_timer) {
113141
}
114142

115143
void Esp32BleKeyboard::release() {
144+
if (!this->setup_) {
145+
ESP_LOGW(TAG, "Attempting to use without setup. Not doing anything.");
146+
return;
147+
}
116148
if (this->is_connected()) {
117149
this->cancel_timeout((const std::string) TAG);
118150
bleKeyboard.releaseAll();

components/ble_keyboard/ble_keyboard.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ namespace esphome {
1212
namespace ble_keyboard {
1313
class Esp32BleKeyboard : public PollingComponent {
1414
public:
15-
Esp32BleKeyboard(std::string name, std::string manufacturer_id, uint8_t battery_level, bool reconnect, bool advertise_on_start)
15+
Esp32BleKeyboard(std::string name, std::string manufacturer_id, uint8_t battery_level, bool reconnect,
16+
bool advertise_on_start)
1617
: PollingComponent(1000), bleKeyboard(name, manufacturer_id, battery_level) {
1718
reconnect_ = reconnect;
1819
advertise_on_start_ = advertise_on_start;
@@ -47,6 +48,7 @@ class Esp32BleKeyboard : public PollingComponent {
4748
BLEServer *pServer;
4849
BleKeyboard bleKeyboard;
4950

51+
bool setup_{false};
5052
bool reconnect_{true};
5153
bool advertise_on_start_{true};
5254
uint32_t default_delay_{100};

0 commit comments

Comments
 (0)