Skip to content

BLE completely breaks after two cycles BLE.begin->BLE.end #254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ASabovic opened this issue Aug 24, 2022 · 1 comment
Closed

BLE completely breaks after two cycles BLE.begin->BLE.end #254

ASabovic opened this issue Aug 24, 2022 · 1 comment
Assignees
Labels
conclusion: duplicate Has already been submitted topic: code Related to content of the project itself type: imperfection Perceived defect in any part of project

Comments

@ASabovic
Copy link

Hi guys,

I have already asked this question before but never really received any answer that could help me. I will ask again, is there anyone here that knows how to enable BLE begin -> BLE end cycles? I want to begin BLE when I have data to send, and once it is sent, disconnect the device and end BLE. With the current code, I am able to perform 2 cycles after which my code stops and breaks. Does anyone have the solution for this? I really need this feature as for my experiments the current consumption when BLE is constantly ON is quite high. Thanks in advance!

This is my code:

#include <ArduinoBLE.h>
unsigned long prevNow = 0;

const char* uuidOfBatteryLevelChar = "2d2F88c4-f244-5a80-21f1-ee0224e80658";
const char* uuidOfService = "180F";
const char* nameofPeripheral = "BatteryMonitor";

bool wasConnected = false;
uint32_t genericTimer = 0;

BLEService batteryService(uuidOfService);

BLEUnsignedCharCharacteristic batteryLevelChar(uuidOfBatteryLevelChar, BLEIndicate); 

char prediction_status[] = "";

void setup() {
  Serial.begin(9600);
  while (!Serial);
  initBLE();
}

void blePeripheralConnectHandler(BLEDevice central) {
  Serial.println("Connected event, central: ");
  Serial.println(central.address());
}

void blePeripheralDisconnectHandler(BLEDevice central) {
  Serial.println("Disconnected event, central: ");
  Serial.println(central.address());
}

void loop() {

  BLE.poll();
  long now = millis();
  if (now - prevNow >= 5000) {
    prevNow = now;
    if (BLE.connected()) {
      Serial.println("BLE device is connected!");
      int rssiValue = BLE.rssi();
      Serial.println(rssiValue);
      Serial.println("Connected to central");
      int prediction = 1;
      sprintf(prediction_status, "%d", prediction);
      batteryLevelChar.writeValue(prediction_status[0]);
      BLE.disconnect();
      BLE.end();
      wasConnected=true;
    }
  
    else {
      Serial.println("Not connected to central");
    }
  }

  
  if(wasConnected){
    Serial.println("Central is not connected and must be connected again!");
    genericTimer = millis();
    while(millis() - genericTimer <= 10000){} 
    wasConnected = false;
    Serial.println("I have to initialize BLE again!");
    initBLE();
  }
}


void initBLE(void){
  if (BLE.begin()){
    Serial.println("BLE will begin now!");
  }
  BLE.setLocalName(nameofPeripheral);
  Serial.println("First step is done!");
  BLE.setAdvertisedService(batteryService);
  Serial.println("Second step is done!");
  batteryService.addCharacteristic(batteryLevelChar); // add the battery level characteristic
  Serial.println("Third step is done!");
  BLE.addService(batteryService);
  Serial.println("Fourth step is done!");
  BLE.setEventHandler(BLEConnected, blePeripheralConnectHandler);
  Serial.println("Fifth step is done!");
  BLE.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler);
  Serial.println("Sixth step is done!");
  Serial.println("Before seventh step and immediately after sixth step!");
  Serial.println("Seventh step is done!");
  BLE.advertise();
}

And after two successful cycles, it stops after the Arduino is again connected to my computer, calling the callback void blePeripheralConnectHandler. This is the output from my serial monitor:

BLE will begin now!
First step is done!
Second step is done!
Third step is done!
Fourth step is done!
Fifth step is done!
Sixth step is done!
Before seventh step and immediately after sixth step!
Seventh step is done!
Not connected to central
Connected event, central: 
3c:6a:a7:42:cc:0e
BLE device is connected!
-37
Connected to central
Central is not connected and must be connected again!
I have to initialize BLE again!
BLE will begin now!
First step is done!
Second step is done!
Third step is done!
Fourth step is done!
Fifth step is done!
Sixth step is done!
Before seventh step and immediately after sixth step!
Seventh step is done!
Not connected to central
Connected event, central: 
3c:6a:a7:42:cc:0e
BLE device is connected!
-41
Connected to central
Central is not connected and must be connected again!
I have to initialize BLE again!
BLE will begin now!
First step is done!
Second step is done!
Third step is done!
Fourth step is done!
Fifth step is done!
Sixth step is done!
Before seventh step and immediately after sixth step!
Seventh step is done!
Not connected to central
Connected event, central: 
3c:6a:a7:42:cc:0e

@per1234
Copy link
Contributor

per1234 commented Aug 24, 2022

Hi @ASabovic

I have already asked this question before

Please only open one issue per subject matter. I'll close this as a duplicate of #243. If you have additional information to add, you are welcome to do it in that thread.

If you would like assistance with your project, please post on the Arduino Forum:

https://forum.arduino.cc/

@per1234 per1234 closed this as not planned Won't fix, can't repro, duplicate, stale Aug 24, 2022
@per1234 per1234 self-assigned this Aug 24, 2022
@per1234 per1234 added type: imperfection Perceived defect in any part of project conclusion: duplicate Has already been submitted topic: code Related to content of the project itself labels Aug 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
conclusion: duplicate Has already been submitted topic: code Related to content of the project itself type: imperfection Perceived defect in any part of project
Projects
None yet
Development

No branches or pull requests

2 participants