Hi everyone, I hope I'm not making any mistakes in publishing this post.
I searched the forum but couldn't find a solution to my problem.
I want to create a code that sends a continuously changing variable via BLE, reading this variable from a phone.
At the moment the variable read by the phone remains fixed at the value it had at the time of connection and I can't resolve it.
I hope someone can help me!
#include <ArduinoBLE.h>
int var = 0;
BLEService greetingService("0000180C-0000-1000-8000-00805F9B34FB"); // User defined service
BLEStringCharacteristic greetingCharacteristic("00002A56-0000-1000-8000-00805F9B34FB", // standard 16-bit characteristic UUID
BLERead | BLEWrite, 128); // remote clients will be able to read and write up to 20 bytes
void setup() {
Serial.begin(9600); // initialize serial communication
while (!Serial);
pinMode(LED_BUILTIN, OUTPUT); // initialize the built-in LED pin
if (!BLE.begin()) { // initialize BLE
Serial.println("starting BLE failed!");
while (1);
}
BLE.setLocalName("Nano33BLE"); // Set name for connection
BLE.setAdvertisedService(greetingService); // Advertise service
greetingService.addCharacteristic(greetingCharacteristic); // Add characteristic to service
BLE.addService(greetingService); // Add service
BLE.advertise(); // Start advertising
Serial.print("Peripheral device MAC: ");
Serial.println(BLE.address());
Serial.println("Waiting for connections...");
}
void loop() {
BLEDevice central = BLE.central();
if (central) {
Serial.print("Connected to central MAC: ");
Serial.println(central.address());
digitalWrite(LED_BUILTIN, HIGH);
while (central.connected()){
var++;
greetingCharacteristic.setValue(String(var));
delay(50);
greetingCharacteristic.broadcast();
delay(500);
Serial.println(var);
}
// when the central disconnects, turn off the LED:
digitalWrite(LED_BUILTIN, LOW);
Serial.print("Disconnected from central MAC: ");
Serial.println(central.address());
}
}