Skip to content

Commit ad782c4

Browse files
authored
Remove duplicate BLE service data sent over MQTT(#1044)
This will remove the sending of BLE service data when multiple service data attributes are advertised by a broadcasting device. Instead the data for known device types will be consolidated into a single message to reduce overhead.
1 parent 2dedabc commit ad782c4

File tree

1 file changed

+1
-30
lines changed

1 file changed

+1
-30
lines changed

main/ZgatewayBT.ino

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -685,36 +685,7 @@ class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks {
685685
process_bledata(BLEdata); // this will force to resolve all the service data
686686
}
687687

688-
if (serviceDataCount > 1) {
689-
BLEdata.remove("servicedata");
690-
BLEdata.remove("servicedatauuid");
691-
692-
int msglen = BLEdata.measureLength() + 1;
693-
char jsonmsg[msglen];
694-
char jsonmsgb[msglen];
695-
BLEdata.printTo(jsonmsgb, sizeof(jsonmsgb));
696-
for (int j = 0; j < serviceDataCount; j++) {
697-
strcpy(jsonmsg, jsonmsgb); // the parse _destroys_ the message buffer
698-
JsonObject& BLEdataLocal = getBTJsonObject(jsonmsg, j == 0); // note, that first time we will get here the BLEdata itself; haPresence for the first msg
699-
if (!BLEdataLocal.containsKey("id")) { // would crash without id
700-
Log.trace("Json parsing error for %s" CR, jsonmsgb);
701-
break;
702-
}
703-
std::string service_data = convertServiceData(advertisedDevice->getServiceData(j));
704-
std::string serviceDatauuid = advertisedDevice->getServiceDataUUID(j).toString();
705-
706-
int last = atomic_load_explicit(&jsonBTBufferQueueLast, ::memory_order_seq_cst) % BTQueueSize;
707-
int size1 = jsonBTBufferQueue[last].buffer.size();
708-
BLEdataLocal.set("servicedata", (char*)service_data.c_str());
709-
int size2 = jsonBTBufferQueue[last].buffer.size();
710-
BLEdataLocal.set("servicedatauuid", (char*)serviceDatauuid.c_str());
711-
int size3 = jsonBTBufferQueue[last].buffer.size();
712-
Log.trace("Buffersize for %d : %d -> %d -> %d" CR, j, size1, size2, size3);
713-
PublishDeviceData(BLEdataLocal);
714-
}
715-
} else {
716-
PublishDeviceData(BLEdata, false); // easy case
717-
}
688+
PublishDeviceData(BLEdata, false);
718689
} else {
719690
PublishDeviceData(BLEdata); // PublishDeviceData has its own logic whether it needs to publish the json or not
720691
}

0 commit comments

Comments
 (0)