This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm whether you accept or reject these cookies being set.

A cookie will be stored in your browser regardless of choice to prevent you being asked this question again. You will be able to change your cookie settings at any time using the link in the footer.

How to disconnect BLE when experiment finished?
#6
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:
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
Reply


Messages In This Thread
RE: How to disconnect BLE when experiment finished? - by Dominik Dorsel - 07-06-2022, 09:46 AM

Forum Jump: