Skip to content

restart peripheral service causes crash #530

Closed
@sdetweil

Description

@sdetweil

in my business solution

  1. don't advertise til app is detected (scan)
  2. the UUID text means something (advertise)
  3. given an additional sensor triggers, change the advertised UUID

on ArduinoBLE and ESP32 BLE I have to basically destroy all the setup
undo the service , and characteristics, and server
and rebuild them.

issues

  1. BLEservice->start(), doesn't have a stop().
  2. BLEserver doesn't have a delete
changed
14:17:09.729 -> new service string=32179901-27b9-42f0-82aa-2e951747bbf9
14:17:09.729 -> after UUID create
14:17:09.729 -> add service
14:17:09.729 -> ble server
14:17:09.729 -> after server create
14:17:09.729 -> config as string={"config":{"gender":3,"type":2,"subtype":"99","sequence":23,"td":227,"UWBSerial":"12345678"}}
14:17:09.729 -> config length=93
14:17:09.729 -> after characteristic setCallbacks 
14:17:09.729 -> after service start
14:17:09.766 -> after advertising setup
14:17:09.766 -> restart advertising after resetup
14:17:09.766 -> Guru Meditation Error: Core  1 panic'ed (LoadProhibited). Exception was unhandled.
14:17:09.766 -> 
14:17:09.766 -> Core  1 register dump:
14:17:09.766 -> PC      : 0x400dc86c  PS      : 0x00060230  A0      : 0x800dcb52  A1      : 0x3ffc8ce0  
14:17:09.766 -> A2      : 0x00000020  A3      : 0x00000000  A4      : 0x3ffcda58  A5      : 0x3ffcd780  
14:17:09.797 -> A6      : 0x00000001  A7      : 0x00000000  A8      : 0x00000020  A9      : 0x3ffcd770  
14:17:09.797 -> A10     : 0x3ffcda68  A11     : 0x00000008  A12     : 0x00000000  A13     : 0xffffffff  
14:17:09.797 -> A14     : 0x400dc630  A15     : 0x00000000  SAR     : 0x0000001a  EXCCAUSE: 0x0000001c  
14:17:09.797 -> EXCVADDR: 0x00000020  LBEG    : 0x40090c2d  LEND    : 0x40090c3d  LCOUNT  : 0xfffffff7  
14:17:09.797 -> 
14:17:09.797 -> 
14:17:09.797 -> Backtrace: 0x400dc869:0x3ffc8ce0 0x400dcb4f:0x3ffc8d60 0x400d562b:0x3ffc8d90 0x400d3d56:0x3ffc8dd0 0x400d4abc:0x3ffc8e00 0x400d2fcf:0x3ffc8e20 0x400e86f1:0x3ffc8ec0

the code at time to change UUID

#ifdef ESP32
          BLEDevice::stopAdvertising();
          pAdvertising->removeServiceUUID(ourService->getUUID()); // will be added back by setupService()
          pBLEScanner->stop();
          #if USE_NIMBLE == 0
              ourService->stop();  // not available in Nimble
          #endif   
          delete ourService;   
          #if USE_NIMBLE == 0 
            delete pServer;        // no delete on server in Nimble
          #endif
          pServer=NULL;   // force new create (now 2?)
          setupService();
          Serial.println("restart advertising after resetup");
          BLEDevice::startAdvertising();    // <-  crash here 
          Serial.println("after restart advertising after resetup");
          advertising=true;

from the setupservice function

Serial.println("add service");
  #ifdef ESP32
    if(pServer == NULL){
      pServer = BLEDevice::createServer();
    }
    Serial.println("after server create");   
    ourService = pServer->createService(buffer);
  #else
    ourService= new BLEService(buffer); // BLE LED Service
  #endif
   
#ifdef ESP32
  LEDCharacteristic= ourService->createCharacteristic(
                      "9A61",
#if USE_NIMBLE
                     NIMBLE_PROPERTY::WRITE
#else                      
                      BLECharacteristic::PROPERTY_WRITE  
#endif                      
                    );
  LEDCharacteristic->setCallbacks(new MyCallbacks());                    
#else   
  LEDCharacteristic=new BLEByteCharacteristic("9A61", BLEWrite);
  ourService->addCharacteristic(*LEDCharacteristic);  
#endif
 
  String jsonString = JSON.stringify(configInfo);
  Serial.print("config as string=");  
  Serial.println(jsonString);
 
 Serial.print("config length=");
 Serial.println(jsonString.length());
#ifdef ESP32
 configCharacteristic= ourService->createCharacteristic(
                      "9AFF",
#if USE_NIMBLE                      
                      NIMBLE_PROPERTY::READ | 
                      NIMBLE_PROPERTY::WRITE
#else
                      BLECharacteristic::PROPERTY_WRITE  |
                      BLECharacteristic::PROPERTY_READ
#endif                      
                    );
  configCharacteristic->setCallbacks(new MyCallbacks());       
   Serial.println("after characteristic setCallbacks ");               
  ourService->start();            . 
  Serial.println("after service start");         
#else
  configCharacteristic =  new BLEStringCharacteristic("9AFF", BLERead|BLEWrite, jsonString.length());  
  configCharacteristic->setEventHandler(BLERead, configCharacteristicRead);
  configCharacteristic->setValue(jsonString);
  ourService->addCharacteristic(*configCharacteristic);  
#endif

#ifdef ESP32
  if(pAdvertising == null){  // 1st time
     pAdvertising = BLEDevice::getAdvertising();
     pAdvertising->setScanResponse(true);
  } 
  pAdvertising->addServiceUUID(ourService->getUUID());  // was removed before second call
#else  
  BLE.setAdvertisedService(*ourService);
  BLE.addService(*ourService);
#endif

include setup

#ifdef ESP32
  #define USE_NIMBLE 1
  #if USE_NIMBLE 
    #include <NimBLEDevice.h>
  #else
    #include <BLEDevice.h>
    #include <BLEServer.h>
    #include <BLEUtils.h>
    #include <BLEScan.h>
    #include <BLEAdvertisedDevice.h>
  #endif
#else
  #include <ArduinoBLE.h>
#endif

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions