BLE.begin() and BLE.end() issue

Hi guys,

Does anybody know how to fix this issue?

#include <ArduinoBLE.h>

 // Bluetooth® Low Energy Battery Service
// Default batteryService -> 180F

#define BLE_UUID_TEST_SERVICE               "28a72d52-35ca-4c57-8b21-b831066983a8"
#define BLE_UUID_FILE_NAME                  "c7f50871-a316-4926-b58a-26e7b58b78e4"

BLEService myService("180F");
BLEUnsignedCharCharacteristic myCharacteristic("19b10010-e8f2-537e-4f6c-d104768a1224", BLENotify);

char test[] = "";

void setup() {
  
  Serial.begin(9600);    // initialize serial communication
  while (!Serial); 

   //if (!BLE.begin()) {
    //Serial.println("starting BLE failed!");

    //while (1);
  //}
  
  BLE.setLocalName("Image transfer");
  BLE.setAdvertisedService(myService);
  myService.addCharacteristic(myCharacteristic);
  BLE.addService(myService);
  myCharacteristic.writeValue(test[0]);

  BLE.advertise();
  //BLE.setConnectionInterval(0xffff, 0xffff);

  Serial.println("Bluetooth® device active, waiting for connections...");

}

void loop(){
  
  BLE.begin();

  BLEDevice central = BLE.central();

  if (central) {
    Serial.print("Connected to central: ");
    // print the central's BT address:
    Serial.println(central.address());
  }

  if (central.connected()) {

     send_image();
  }
  else{
    Serial.print("Central is not connected!\n");
    }
  BLE.end();
  delay(3000);
  
}

void send_image(){
  
  int test_value = 1;
  sprintf(test, "%d", test_value);
  myCharacteristic.writeValue(test[0]);
  Serial.println(test_value);
}

I want to connect to the central node and send a simple message but it is not possible when I put BLE.begin() and BLE.end() in the loop function. When it's only BLE.begin() in setup it works, but for my project, I need to turn on BLE when I need it and turn it off when data is sent. Does anyone know how to enable this?

That is not currently what you are doing. In your code BLE.begin() is called every time through loop(), albeit with a 3 second delay() between calls.

Are you seeing the "Connected" message every 3 seconds or the "not connected" message ?

I am able to see only the "not connected" message. Do you maybe know where I made the mistake? I just want to send every 3 sec this simple message but in the meantime put BLE off in order to save some energy. I thought that creating a loop like this where in the end BLE will be put off and in the next step wake up and turn on is the way to achieve that. Which part should be fixed? Thanks in advance!

My knowledge of Bluetooth BLE is very limited but I thought that one of its benefits over traditional BT was that it was already a Low Energy solution

Yes, it is. But I want to save a bit more by putting my device into a sleep state between two transmissions. I have already found the example where this part is tested but in that case, the author just tested the number of cycles that can be done from begin to end state without including another device for which messages are assigned.

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