Invoking BLE Library Functions Inside User Functions

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.

To all those wishing to help, please disregard - I think this is a sequencing problem and not a C-related variable protection issue. It looks like I was trying to advertise but I was also connected to a peripheral. When I interleave the functionality so that I connect to a peripheral then disconnect, I can start advertising for a while then stop and repeat the process. My bad.

  • Fred

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.