@@ -15,25 +15,37 @@ namespace ble_keyboard {
15
15
static const char *const TAG = " ble_keyboard" ;
16
16
17
17
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..." );
19
24
20
25
bleKeyboard.begin ();
21
26
22
27
pServer = BLEDevice::getServer ();
23
28
24
- ESP_LOGD (TAG, " advertiseOnDisconnect(%s)" , this ->reconnect_ ? " true" : " false" );
29
+ ESP_LOGCONFIG (TAG, " advertiseOnDisconnect(%s)" , this ->reconnect_ ? " true" : " false" );
25
30
pServer->advertiseOnDisconnect (this ->reconnect_ );
26
31
27
32
if (!this ->advertise_on_start_ ) {
28
- ESP_LOGD (TAG, " stopAdvertising()" );
33
+ ESP_LOGCONFIG (TAG, " stopAdvertising() because advertise_on_start is false " );
29
34
pServer->stopAdvertising ();
30
35
}
31
36
32
37
bleKeyboard.releaseAll ();
38
+ this ->setup_ = true ;
39
+ ESP_LOGCONFIG (TAG, " Finished setting up up BLE keyboard." );
33
40
}
34
41
35
42
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()" );
37
49
if (this ->reconnect_ ) {
38
50
ESP_LOGD (TAG, " advertiseOnDisconnect(false)" );
39
51
pServer->advertiseOnDisconnect (false );
@@ -51,6 +63,10 @@ void Esp32BleKeyboard::stop() {
51
63
}
52
64
53
65
void Esp32BleKeyboard::start () {
66
+ if (!this ->setup_ ) {
67
+ ESP_LOGW (TAG, " Attempting to use without setup. Not doing anything." );
68
+ return ;
69
+ }
54
70
ESP_LOGD (TAG, " start()" );
55
71
if (this ->reconnect_ ) {
56
72
ESP_LOGD (TAG, " advertiseOnDisconnect(true)" );
@@ -78,6 +94,10 @@ void Esp32BleKeyboard::update_timer() {
78
94
}
79
95
80
96
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
+ }
81
101
if (this ->is_connected ()) {
82
102
if (message.length () >= 5 ) {
83
103
for (unsigned i = 0 ; i < message.length (); i += 5 ) {
@@ -94,6 +114,10 @@ void Esp32BleKeyboard::press(std::string message) {
94
114
}
95
115
96
116
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
+ }
97
121
if (this ->is_connected ()) {
98
122
if (with_timer) {
99
123
this ->update_timer ();
@@ -104,6 +128,10 @@ void Esp32BleKeyboard::press(uint8_t key, bool with_timer) {
104
128
}
105
129
106
130
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
+ }
107
135
if (this ->is_connected ()) {
108
136
if (with_timer) {
109
137
this ->update_timer ();
@@ -113,6 +141,10 @@ void Esp32BleKeyboard::press(MediaKeyReport key, bool with_timer) {
113
141
}
114
142
115
143
void Esp32BleKeyboard::release () {
144
+ if (!this ->setup_ ) {
145
+ ESP_LOGW (TAG, " Attempting to use without setup. Not doing anything." );
146
+ return ;
147
+ }
116
148
if (this ->is_connected ()) {
117
149
this ->cancel_timeout ((const std::string) TAG);
118
150
bleKeyboard.releaseAll ();
0 commit comments