Skip to content

Commit 3742221

Browse files
committed
Fix BLE byte counter regression introduced in 9.0.0
In 8.0.0, addOnReceiveListener() called addOnReceiveCallback() which registered callbacks in both _onReceiveListeners and the BLE-specific _onCharateristicValueChangedListeners array. In 9.0.0 this was removed to "avoid duplicate push", but Serial's addOnReceiveCallback() pushes to _onReceiveListeners while BLE's pushes to its own separate array. This meant the byte counter listener (added via addOnReceiveListener) only went into _onReceiveListeners, but BLE's notification handler only called _onCharateristicValueChangedListeners - so the counter never incremented and MSP responses were never seen by the parser. Fix: align BLE with Serial by using _onReceiveListeners throughout, removing the now-unnecessary _onCharateristicValueChangedListeners array. Includes debug logging to aid diagnosis if further issues arise.
1 parent d7a1e84 commit 3742221

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

js/connection/connectionBle.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ class ConnectionBle extends Connection {
4848
this._writeCharacteristic = false;
4949
this._device = false;
5050
this._deviceDescription = false;
51-
this._onCharateristicValueChangedListeners = [];
5251
this._onDisconnectListeners = [];
5352
this._reconnects = 0;
5453
this._handleOnCharateristicValueChanged = false;
@@ -166,12 +165,19 @@ class ConnectionBle extends Connection {
166165
buffer[i] = event.target.value.getUint8(i);
167166
}
168167

169-
this._onCharateristicValueChangedListeners.forEach(listener => {
170-
listener({
171-
connectionId: 0xFF,
172-
data: buffer
173-
});
168+
const info = {
169+
connectionId: 0xFF,
170+
data: buffer
171+
};
172+
173+
// TODO: Remove debug logging after BLE fix is verified in testing
174+
console.log(`[BLE FIX] ← Received ${buffer.byteLength} bytes, ${this._onReceiveListeners.length} listener(s), counter at ${this._bytesReceived}`);
175+
176+
this._onReceiveListeners.forEach(listener => {
177+
listener(info);
174178
});
179+
180+
console.log(`[BLE FIX] Byte counter after dispatch: ${this._bytesReceived}`);
175181
};
176182

177183
this._readCharacteristic.addEventListener('characteristicvaluechanged', this._handleOnCharateristicValueChanged)
@@ -242,11 +248,15 @@ class ConnectionBle extends Connection {
242248
}
243249

244250
addOnReceiveCallback(callback){
245-
this._onCharateristicValueChangedListeners.push(callback);
251+
// TODO: Remove debug logging after BLE fix is verified in testing
252+
console.log(`[BLE FIX] addOnReceiveCallback: listener count ${this._onReceiveListeners.length}${this._onReceiveListeners.length + 1}`);
253+
this._onReceiveListeners.push(callback);
246254
}
247255

248256
removeOnReceiveCallback(callback){
249-
this._onCharateristicValueChangedListeners = this._onCharateristicValueChangedListeners.filter(listener => listener !== callback);
257+
this._onReceiveListeners = this._onReceiveListeners.filter(listener => listener !== callback);
258+
// TODO: Remove debug logging after BLE fix is verified in testing
259+
console.log(`[BLE FIX] removeOnReceiveCallback: listener count now ${this._onReceiveListeners.length}`);
250260
}
251261

252262
addOnReceiveErrorCallback(callback) {

0 commit comments

Comments
 (0)