Skip to content

Adds onDisconnect() BLE param #7851

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 15, 2023

Conversation

SuGlider
Copy link
Collaborator

Description of Change

PR #7559 was incomplete when compared to issue #6008
This is pointed out in issue #7741

There is a missing call to onDisconnect(this, param);

Tests scenarios

Tested with ESP32-S3 sketch (below) and a mobile BLE APP (iOS or Android).

#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#include <BLEAddress.h>

#define SERVICE_UUID        "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"

bool deviceConnected = false;

class MyServerCallbacks: public BLEServerCallbacks {
    void onConnect(BLEServer* pServer) {
      deviceConnected = true;
    };

    void onDisconnect(BLEServer* server, esp_ble_gatts_cb_param_t* param) {
      BLEAddress remote_addr(param->disconnect.remote_bda);
      
      Serial.printf("ble::onDisconnect reason: %s | Connection ID: %d | Link Role: %s | Remote Address: %s\n",
                      BLEUtils::gattCloseReasonToString(param->disconnect.reason).c_str(),
                      param->disconnect.conn_id, 
                      param->disconnect.link_role ? "SLAVE" : "MASTER",
                      remote_addr.toString().c_str()
       ); 
    }
    
    void onDisconnect(BLEServer* pServer) {
      deviceConnected = false;
    }
};

void setup() {
  Serial.begin(115200);
  Serial.println("Starting BLE work!");

  BLEDevice::init("Long name works now");
  BLEServer *pServer = BLEDevice::createServer();
  pServer->setCallbacks(new MyServerCallbacks());
  BLEService *pService = pServer->createService(SERVICE_UUID);
  BLECharacteristic *pCharacteristic = pService->createCharacteristic(
                                         CHARACTERISTIC_UUID,
                                         BLECharacteristic::PROPERTY_READ |
                                         BLECharacteristic::PROPERTY_WRITE
                                       );

  pCharacteristic->setValue("Hello World says Neil");
  pService->start();
  // BLEAdvertising *pAdvertising = pServer->getAdvertising();  // this still is working for backward compatibility
  BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  pAdvertising->addServiceUUID(SERVICE_UUID);
  pAdvertising->setScanResponse(true);
  pAdvertising->setMinPreferred(0x06);  // functions that help with iPhone connections issue
  pAdvertising->setMinPreferred(0x12);
  BLEDevice::startAdvertising();
  Serial.println("Characteristic defined! Now you can read it in your phone!");
}

void loop() {
  // put your main code here, to run repeatedly:
  delay(2000);
  Serial.printf("Server is %s\n", deviceConnected ? "Connected" : "Disconnected");
}

Related links

Fix #7741
Close #7115

@SuGlider SuGlider added the Area: BT&Wifi BT & Wifi related issues label Feb 15, 2023
@SuGlider SuGlider added this to the 2.0.7 milestone Feb 15, 2023
@SuGlider SuGlider self-assigned this Feb 15, 2023
@me-no-dev me-no-dev merged commit 211ba18 into espressif:master Feb 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: BT&Wifi BT & Wifi related issues
Projects
2 participants