Skip to content

Commit 1aad4c2

Browse files
committed
fix ESP32 BLE bug
1 parent bea9300 commit 1aad4c2

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

src/Blinker/BlinkerProtocol.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class BlinkerProtocol
2424
, state(CONNECTING)
2525
, isFresh(false)
2626
, isAvail(false)
27+
, availState(false)
2728
, canParse(false)
2829
{}
2930

@@ -49,7 +50,11 @@ class BlinkerProtocol
4950

5051
void run();
5152

52-
bool available() { return isFresh; }
53+
bool available() {
54+
bool _avail = availState;
55+
availState = false;
56+
return _avail;
57+
}
5358

5459
String readString()
5560
{
@@ -103,6 +108,7 @@ class BlinkerProtocol
103108

104109
void flush() {
105110
isFresh = false;
111+
availState = false;
106112
}
107113

108114
private :
@@ -112,6 +118,7 @@ class BlinkerProtocol
112118
if (isAvail) {
113119
isFresh = true;
114120
canParse = true;
121+
availState = true;
115122
}
116123
return isAvail;
117124
}
@@ -126,7 +133,7 @@ class BlinkerProtocol
126133
}
127134
}
128135

129-
void isParsed() { isFresh = false; canParse = false; }// BLINKER_LOG1("isParsed");
136+
void isParsed() { isFresh = false; canParse = false; availState = false; }// BLINKER_LOG1("isParsed");
130137

131138
bool parseState() { return canParse; }
132139

@@ -135,6 +142,7 @@ class BlinkerProtocol
135142
BlinkerState state;
136143
bool isFresh;
137144
bool isAvail;
145+
bool availState;
138146
bool canParse;
139147

140148
void begin()

src/BlinkerSimpleESP32_BLE.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ class BlinkerTransportESP32_BLE
110110
char BLEBuf[BLINKER_BUFFER_SIZE];
111111
bool isAvail;
112112
bool isFresh;
113+
uint32_t _bufLen;
113114
uint32_t freshTime;
114115
BLEServer *pServer;
115116
BLEService *pService;
@@ -133,27 +134,29 @@ class BlinkerTransportESP32_BLE
133134

134135
void onWrite(BLECharacteristic *pCharacteristic) {
135136
std::string value = pCharacteristic->getValue();
137+
int vlen = value.length();
136138

137-
if (value.length() > 0) {
139+
if (vlen > 0) {
138140
freshTime = millis();
139141

140-
if (!isFresh) {
141-
strcpy(BLEBuf, value.c_str());
142-
isFresh = true;
143-
}
144-
else {
145-
strcat(BLEBuf, value.c_str());
142+
for (uint8_t _num = 0; _num < vlen; _num++) {
143+
BLEBuf[_bufLen] = value[_num];
144+
_bufLen++;
146145
}
147146

148-
if (String(value.c_str()).endsWith("\n")) {
147+
isFresh = true;
148+
149+
if (value[vlen-1] == '\n') {
149150
isAvail = true;
151+
_bufLen = 0;
150152
}
151153
}
152154
}
153155

154156
void checkTimeOut() {
155157
if (isFresh && !isAvail && (millis() - freshTime) > BLINKER_STREAM_TIMEOUT) {
156158
isAvail = true;
159+
_bufLen = 0;
157160
}
158161
}
159162
};

0 commit comments

Comments
 (0)