07-06-2022, 09:46 AM
Hey,
just testet your code. The problem is that the ble server stops advertising once a connection is established. After the connection is canceld, you have to start advertising once again to allow new connections. The code below solves this issue:
hm ok, the basic idea behind the arduino library is that the user does not have to worry about the bluetooth low energy connection anymore. We think this would help beginners to focus on the code of the measurement setup itself.
Cheers Dominik
just testet your code. The problem is that the ble server stops advertising once a connection is established. After the connection is canceld, you have to start advertising once again to allow new connections. The code below solves this issue:
Code:
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
BLEServer* pServer = NULL;
BLECharacteristic* pCharacteristic = NULL;
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
class MyServerCallbacks: public BLEServerCallbacks {
void onDisconnect(BLEServer* pServer) {
BLEDevice::startAdvertising();
}
};
void setup() {
Serial.begin(115200);
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_NOTIFY
);
// Start the service
pService->start();
// Start advertising
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
BLEDevice::startAdvertising();
}
void loop() {
// notify changed value
int ran = random(0,11); //
Serial.println(ran);
pCharacteristic->setValue(ran); //
pCharacteristic->notify();
delay(100);
}
Quote:Phyphox Arduino library is good enough. But I am a very beginner of coding, it seems a little complicated for me. Using a general BLE library and Phyphox web editor seems much suitable for me.
hm ok, the basic idea behind the arduino library is that the user does not have to worry about the bluetooth low energy connection anymore. We think this would help beginners to focus on the code of the measurement setup itself.
Cheers Dominik