Hi,
I'm new to ESP32. I'm now trying out the BLE code to broadcast sine values to my Android 11 phone (I'm doing it natively and not using nrfConnect). However, it doesn't get connected whenever I try to reconnect after disconnecting. I have to forget the device, scan for the device, and then connect to it. Are there any workarounds to this? Any advice for it to work correctly?
/*
Install "Arduino ESP32 Boards" by Arduino and "esp32" by Espressif Systems
*/
#include <BLE2902.h>
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
BLEServer *pServer = NULL;
BLECharacteristic *pCharacteristic = NULL;
bool deviceConnected = false;
bool oldDeviceConnected = false;
float value = 0;
const float AMP = 1.0; // Amplitude of the sine wave
const float F = 1.0; // Frequency of the sine wave
const int POINTS = 100; // Number of points in the sine wave
// See the following for generating UUIDs:
// https://www.uuidgenerator.net/
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
class MyServerCallbacks : public BLEServerCallbacks {
void onConnect(BLEServer *pServer) {
deviceConnected = true;
};
void onDisconnect(BLEServer *pServer) {
deviceConnected = false;
BLEDevice::stopAdvertising();
pServer->getAdvertising()->start();
}
};
void setup() {
Serial.begin(115200);
// Create the BLE Device
BLEDevice::init("ESP32");
// Create the BLE Server
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
// Create the BLE Service
BLEService *pService = pServer->createService(SERVICE_UUID);
// Create a BLE Characteristic
pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_NOTIFY | BLECharacteristic::PROPERTY_INDICATE
);
// https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml
// Create a BLE Descriptor
pCharacteristic->addDescriptor(new BLE2902());
// Start the service
pService->start();
// Start advertising
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->setScanResponse(false);
pAdvertising->setMinPreferred(0x0); // set value to 0x00 to not advertise this parameter
BLEDevice::startAdvertising();
Serial.println("Waiting a client connection to notify...");
}
void loop() {
// notify changed value
if (deviceConnected) {
// Continuously generate and send sine wave points
static int i = 0; // Static variable to keep track of the current point
// Calculate the angle for the current point
float angle = (2.0 * PI * i) / POINTS;
pCharacteristic->setValue((uint8_t *)&value, 4);
pCharacteristic->notify();
value = AMP * sin(F * angle);
// Print the sine value to Serial (for debugging)
Serial.println(value);
// Increment the point index, and wrap around if necessary
i = (i + 1)%POINTS;
delay(500); // bluetooth stack will go into congestion, if too many packets are sent, in 6 hours test i was able to go as low as 3ms
}
// disconnecting
if (!deviceConnected && oldDeviceConnected) {
delay(500); // give the bluetooth stack the chance to get things ready
pServer->startAdvertising(); // restart advertising
Serial.println("start advertising");
oldDeviceConnected = deviceConnected;
}
// connecting
if (deviceConnected && !oldDeviceConnected) {
oldDeviceConnected = deviceConnected;
}
}