Using BeaconNano library with Nano 33 sense fails at stopping and restarting

Hi,

I am new to this forum and to Arduino programming. I am working on a project that detects human presence and sends the result by broadcasting a 1 second burst in iBeacon format. I broadcast two different UUIDs depending on presence status (one for present and one for absent)

First, I need to say I understand this is not a normal way of using an iBeacon, but I have no other choice. The receiver I am communicating with only accepts the iBeacon format.

The problem that I am facing is that I can get the Arduino to broadcast only one time. When I stop the beacon, change the UUID and restart it, the program crashes and I need to double-click the reset button to recover. I tried multiple ways and some allowed me to turn the iBeacon on and off a couple of times (less than 5 times at best). The attached test case allows me to send a 1 second burst on the first attempt and then crashes on the next one. Here is the execution summary:

  1. Boot, get the "Enter a Key - 'a' for Absent or 'p' for Present to send presence broadcasting:" prompt on the serial monitor
  2. Hit the ‘a’ Key then the system Prints “Absent” on the monitor and then broadcasts for 1 second, using the “absent” UUID
  3. hit the ‘p’ key : then the system Prints “Present” on the monitor, but nothing else happens (no broadcast) and the board hangs (disconnects from the serial monitor).

If I comment out the calls to the “changeBeaconUUID” code, of course the program works well forever, printing “Absent” every time I hit ‘a’ and printing “Present” every time I hit ‘p’

Has anyone worked with the beaconNano library and successfully changed the UUID on the fly multiple times? I could not find any documented bug or bug fix about this library. There was another thread similar to mine, but it was closed a while ago:

https://forum.arduino.cc/t/arduino-nano-33-ble-as-ibeacon/623266

Thanks in advance for any help

Here's my test case:

#include <ArduinoBLE.h>
#include <BeaconNano.h>
#define oneSecDelay 1000
#define presentUUID "5e3490c7946841a199dd6897cd538d2a"
#define absentUUID "468a81cc1b944cdeb5dd0e3408ed87b8"

    
 void changeBeaconUUID(char* uuidVal)
  { 
    BeaconNano bn;
    bn.setUuid(uuidVal);
    bn.setMajor(1);
    bn.setMinor(2);
    bn.setTx(197);
    bn.startBeacon();
    delay(oneSecDelay);
    bn.stopBeacon();   
    delay(oneSecDelay);
  }
  
void setup() 
  {
    delay(oneSecDelay);
    Serial.begin(115200);
    delay(oneSecDelay);
    Serial.println("Enter a Key - 'a' for Absent or 'p' for Present to send presence broadcasting:");       
    delay(oneSecDelay);
  }

void loop() 
  {
    int keyRead;
    do
      {
        keyRead = Serial.read();  
      }
    while(keyRead != 'p' && keyRead != 'a');
    if (keyRead == 'p')
      {
        Serial.println("Présent");
        delay(oneSecDelay);       
        changeBeaconUUID(presentUUID); 
      }
    if (keyRead == 'a')
      {
        Serial.println("Absent");
        delay(oneSecDelay);       
        changeBeaconUUID(absentUUID);
      } 
  }

Quick update: I also tried a different flavor of this library found somewhere else. Even though both use the same version number, they are not the same and they get called differently. I tried a much simpler test case that simply turns the beacon on and off every 30 seconds. It starts broadcasting and stops working on the first attempt to restart:

#include <ArduinoBLE.h>
#include <BeaconNano.h>
#define dly 30000

void setup()
{  
  Beacon.begin("5e3490c7946841a199dd6897cd538d2a",1,1,197);
  Beacon.startBeacon();
  delay (dly);
}

void loop()
{
  Beacon.stopBeacon();
  delay (dly);
  Beacon.startBeacon();
  delay (dly);
}

There are two issues reported on GitHub for the ArduinoBLE.h library. You may be running into a library issue.

stopAdvertise/startAdvertise doesn't restart #196

ArduinoBLE crashes when BLE.begin BLE.end is called multiple times (possible solution included)
#192

Thanks @cattledog . Being new to this, I was not sure where to look for the problem and kept searching only for known bug in the BeaconNano library. Now, at least, I will be looking in the right direction. Thanks again.

@cattledog I tried the solution proposed by @Klaus_K in the issue #192 reported in Github that you mentioned above:

ArduinoBLE crashes when BLE.begin BLE.end is called multiple times (possible solution included)
#192

And although this seems like Klaus is really correcting a real bug, my problem seems to come from somewhere else, since I still have the same problem. It seems like this other issue that you mentioned:

stopAdvertise/startAdvertise doesn't restart #196

Could be my problem. Unfortunately, I searched everywhere and there doesn't seem to be any bug fix available for this yet. I am on a time crunch and don't know where to look for some possible fix. Anyone has any idea when a new library is due to be released and if a bug fix is or will be available for issue #196 above?

Thanks in advance for your help.

I really don't know what to say, in that you are probably the world expert on using a Nano33 BLE as an ibeacon using the Arduino ide. :nerd_face:

Perhaps there is something in the meded layer for the nrf52840, or with proprietary protocols using Nordic tools device, but that's all beyond me.