During prototyping, I put all the function calls within either the setup() or the loop() portions of my Arduino code. As things started getting more complex, I began to separate code into discrete function blocks, even if it meant moving a simple function like BLE.poll() to startPollingForPeripheral().
Once in a while, however, I ran into strange behaviour where functions stop working or just return garbage/nulls if I do that. The following is an example. This code does not work. setupPeripheralAdvertising() just calls setLocalName() and setConnectable() then returns a hard-coded true:
if (!initDone) {
if (setupPeripheralAdvertising()) {
// BLE.setLocalName("My Little Pony");
// BLE.setConnectable(true);
startAdvertising();
initDone = true;
}
}
If I stop calling my function and place its contents in the top-level if statement, it works:
if (!initDone) {
// if (setupPeripheralAdvertising()) {
if (true) {
BLE.setLocalName("My Little Pony");
BLE.setConnectable(true);
startAdvertising();
initDone = true;
}
}
Interestingly, my function "startAdvertising()" is just BLE.advertise() and it works fine. So, I suspect this has something to do with compartmentalisation but I do not know what to make global in order to make all functionality transparent to all called functions. I've already declared BLEDevice central and BLEDevice peripheral globally. I would appreciate any comments.