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?