espressif / arduino-esp32

Arduino core for the ESP32
GNU Lesser General Public License v2.1
13.69k stars 7.42k forks source link

BLE server connection callbacks being made when BLE client connects or disconnects #7115

Closed AlexMompoint closed 1 year ago

AlexMompoint commented 2 years ago

Board

Tiny Pico

Device Description

Just Tiny pico using usb power

Hardware Configuration

I have two other esp32 boards, one acting as a server and one acting as a client just to connect and disconnect from the one I am experiencing the issue I have.

Version

v2.0.3

IDE Name

Arduino IDE

Operating System

Windows 10

Flash frequency

80 MHz

PSRAM enabled

yes

Upload speed

921600

Description

I was given a task use the esp32 as both a server and a client to allow some device to connect to it, and as well to reach out to a server and connect to it. I attempted performing this using the BLE libraries in the arduino esp32.

If the server portion receives a connect, or disconnect, it functions as expected, only the server callbacks are made, but when the client portion connects, or disconnects, it performs the call back for both server and client meaning I can't really use the server callback as just a server callback.

After some more testing the Server doesn't even have to be started the callbacks just need to be set.

Sketch

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

#define SERVICE_UUID1           "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID

bool doScan = true;
bool deviceConnected = false;

BLEClient *pClient;
BLEServer *pServer;
BLEScan *pBLEScan;
std::string deviceName = "serverDevice";

class MyServerCallbacks: public BLEServerCallbacks {
  void onConnect(BLEServer* pServer) {
    deviceConnected = true;
    Serial.println("***** Connect");
  }
  void onDisconnect(BLEServer* pServer) {
    Serial.println("***** Disconnect");
    deviceConnected = false;
    BLEDevice::startAdvertising();
  }
};

class MyClientCallbacks: public BLEClientCallbacks {
  void onConnect(BLEClient *pClient) {
    Serial.println("Client Connected *****");
  }

  void onDisconnect(BLEClient *pClient) {
    Serial.println("Client Disconnected *****");
    doScan = true;
  }
};

static void statusNotifyCallback(
  BLERemoteCharacteristic* pBLERemoteCharacteristic,
  uint8_t* pData,
  size_t length,
  bool isNotify) {
  Serial.println("status");
  return;
}

void setup() {

  Serial.begin(115200);

  // Initialize Device
  BLEDevice::init("DualModeDevice");
  pBLEScan = BLEDevice::getScan();
  pBLEScan->setInterval(100); //ms
  pBLEScan->setWindow(99);
  pBLEScan->setActiveScan(true);

  // Server creation
  pServer = BLEDevice::createServer();
  pServer->setCallbacks(new MyServerCallbacks());
  BLEService *pService = pServer->createService(SERVICE_UUID1);

  // Starting server
  // pService->start();
  // BLEDevice::startAdvertising();

}

void loop() {
  // put your main code here, to run repeatedly:
  if(doScan) {
      Serial.println("Scanning");
      bool doConnect = false;
      BLEScanResults results = pBLEScan->start(5); 
      BLEAdvertisedDevice *myDevice;
      for(int i = 0; i < results.getCount();i++) {
        if((results.getDevice(i).toString().find(deviceName.c_str()) == std::string::npos)) {
          continue;
        }
        myDevice = new BLEAdvertisedDevice(results.getDevice(i));
        doConnect = true;
        break;
      }
    if(doConnect) {
      if(pClient != NULL) {
        delete pClient;
        pClient = NULL;
      }
      if(pClient == NULL) {
        // Client creation
        pClient = BLEDevice::createClient();
        pClient->setClientCallbacks(new MyClientCallbacks());
      }
      pClient->connect(myDevice);
      doScan = false;
    }
  } else {
        Serial.println("Not Scanning");
  }
  delay(2000);
}

Debug Message

12:05:44.829 -> [ 12361][D][BLEAdvertisedDevice.cpp:472] setRSSI(): - setRSSI(): rssi: -78
12:05:44.876 -> [ 12367][D][BLEAdvertisedDevice.cpp:292] parseAdvertisement(): Type: 0x01 (), length: 1, data: 04
12:05:44.876 -> [ 12376][D][BLEAdvertisedDevice.cpp:292] parseAdvertisement(): Type: 0x02 (), length: 2, data: 5afd
12:05:44.876 -> [ 12384][D][BLEAdvertisedDevice.cpp:500] setServiceUUID(): - addServiceUUID(): serviceUUID: 0000fd5a-0000-1000-8000-00805f9b34fb
12:05:44.876 -> [ 12396][D][BLEAdvertisedDevice.cpp:292] parseAdvertisement(): Type: 0x16 (), length: 22, data: 5afd1300210199390134332c00a9a3000000692a0b7d
12:05:44.876 -> [ 12408][D][BLEAdvertisedDevice.cpp:519] setServiceDataUUID(): - addServiceDataUUID(): serviceDataUUID: 0000fd5a-0000-1000-8000-00805f9b34fb
12:05:44.923 -> [ 12420][D][BLEAdvertisedDevice.cpp:292] parseAdvertisement(): Type: 0x09 (), length: 9, data: 536d61727420546167
12:05:44.923 -> [ 12430][D][BLEAdvertisedDevice.cpp:461] setName(): - setName(): name: Smart Tag
12:05:44.923 -> [ 12438][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 40:b0:03:53:cf:b3, already seen it.
12:05:44.923 -> [ 12446][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 01:e6:ba:22:4c:7a, already seen it.
12:05:44.970 -> [ 12455][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 54:01:39:89:e5:2b, already seen it.
12:05:44.970 -> [ 12463][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 7e:74:68:00:36:0e, already seen it.
12:05:44.970 -> [ 12471][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 30:c6:f7:41:52:86, already seen it.
12:05:44.970 -> [ 12479][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 43:b4:03:4f:44:18, already seen it.
12:05:44.970 -> [ 12487][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 69:af:5c:dc:78:0d, already seen it.
12:05:44.970 -> [ 12495][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 30:c6:f7:41:52:86, already seen it.
12:05:45.018 -> [ 12503][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 40:4e:36:dc:8a:94, already seen it.
12:05:45.018 -> [ 12511][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 7e:74:68:00:36:0e, already seen it.
12:05:45.018 -> [ 12519][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 01:e6:ba:22:4c:7a, already seen it.
12:05:45.018 -> [ 12527][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 67:d7:3e:e6:8b:58, already seen it.
12:05:45.018 -> [ 12535][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 30:c6:f7:41:52:86, already seen it.
12:05:45.018 -> [ 12546][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 4f:01:7f:b6:71:47, already seen it.
12:05:45.018 -> [ 12551][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 43:b4:03:4f:44:18, already seen it.
12:05:45.066 -> [ 12588][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 7e:74:68:00:36:0e, already seen it.
12:05:45.066 -> [ 12590][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 01:e6:ba:22:4c:7a, already seen it.
12:05:45.066 -> [ 12596][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 43:b4:03:4f:44:18, already seen it.
12:05:45.066 -> [ 12604][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 55:30:69:fb:66:db, already seen it.
12:05:45.066 -> [ 12611][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 30:c6:f7:41:52:86, already seen it.
12:05:45.066 -> [ 12620][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 22:58:b2:d3:68:ce, already seen it.
12:05:45.114 -> [ 12627][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 6c:7c:00:88:59:de, already seen it.
12:05:45.114 -> [ 12635][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 4f:01:7f:b6:71:47, already seen it.
12:05:45.114 -> [ 12644][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 43:b4:03:4f:44:18, already seen it.
12:05:45.114 -> [ 12651][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 30:c6:f7:41:52:86, already seen it.
12:05:45.114 -> [ 12659][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 69:af:5c:dc:78:0d, already seen it.
12:05:45.161 -> [ 12678][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 4c:d3:9b:97:31:d8, already seen it.
12:05:45.161 -> [ 12679][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 40:4e:36:dc:8a:94, already seen it.
12:05:45.161 -> [ 12684][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 40:b0:03:53:cf:b3, already seen it.
12:05:45.161 -> [ 12692][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 30:c6:f7:41:52:86, already seen it.
12:05:45.161 -> [ 12725][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 01:e6:ba:22:4c:7a, already seen it.
12:05:45.207 -> [ 12735][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 43:b4:03:4f:44:18, already seen it.
12:05:45.207 -> [ 12758][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 30:c6:f7:41:52:86, already seen it.
12:05:45.254 -> [ 12781][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 30:c6:f7:41:52:86, already seen it.
12:05:45.254 -> [ 12786][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 4c:d3:9b:97:31:d8, already seen it.
12:05:45.254 -> [ 12787][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 67:d7:3e:e6:8b:58, already seen it.
12:05:45.254 -> [ 12795][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 40:b0:03:53:cf:b3, already seen it.
12:05:45.254 -> [ 12803][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 7e:74:68:00:36:0e, already seen it.
12:05:45.254 -> [ 12815][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 01:e6:ba:22:4c:7a, already seen it.
12:05:45.301 -> [ 12824][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 43:b4:03:4f:44:18, already seen it.
12:05:45.301 -> [ 12829][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 30:c6:f7:41:52:86, already seen it.
12:05:45.301 -> [ 12835][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 4f:01:7f:b6:71:47, already seen it.
12:05:45.301 -> [ 12860][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 40:4e:36:dc:8e:23, already seen it.
12:05:45.347 -> [ 12871][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 69:af:5c:dc:78:0d, already seen it.
12:05:45.347 -> [ 12877][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 30:c6:f7:41:52:86, already seen it.
12:05:45.347 -> [ 12882][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 6c:7c:00:88:59:de, already seen it.
12:05:45.394 -> [ 12897][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 7e:74:68:00:36:0e, already seen it.
12:05:45.394 -> [ 12898][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 40:b0:03:53:cf:b3, already seen it.
12:05:45.394 -> [ 12912][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 01:e6:ba:22:4c:7a, already seen it.
12:05:45.394 -> [ 12917][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 43:b4:03:4f:44:18, already seen it.
12:05:45.394 -> [ 12930][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 30:c6:f7:41:52:86, already seen it.
12:05:45.394 -> [ 12931][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 4f:01:7f:b6:71:47, already seen it.
12:05:45.394 -> [ 12936][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 54:01:39:89:e5:2b, already seen it.
12:05:45.394 -> [ 12957][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 7a:8a:5e:0e:c8:de, already seen it.
12:05:45.441 -> [ 12971][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 30:c6:f7:41:52:86, already seen it.
12:05:45.441 -> [ 12972][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 69:af:5c:dc:78:0d, already seen it.
12:05:45.441 -> [ 12978][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 55:30:69:fb:66:db, already seen it.
12:05:45.441 -> [ 12985][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 4c:d3:9b:97:31:d8, already seen it.
12:05:45.441 -> [ 12993][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 40:4e:36:dc:8a:94, already seen it.
12:05:45.441 -> [ 13004][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 7e:74:68:00:36:0e, already seen it.
12:05:45.490 -> [ 13011][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 40:b0:03:53:cf:b3, already seen it.
12:05:45.490 -> [ 13018][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 01:e6:ba:22:4c:7a, already seen it.
12:05:45.490 -> [ 13033][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 30:c6:f7:41:52:86, already seen it.
12:05:45.490 -> [ 13053][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 30:c6:f7:41:52:86, already seen it.
12:05:45.538 -> [ 13077][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 69:af:5c:dc:78:0d, already seen it.
12:05:45.538 -> [ 13104][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 4c:d3:9b:97:31:d8, already seen it.
12:05:45.586 -> [ 13117][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 30:c6:f7:41:52:86, already seen it.
12:05:45.586 -> [ 13119][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 01:e6:ba:22:4c:7a, already seen it.
12:05:45.586 -> [ 13124][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 43:b4:03:4f:44:18, already seen it.
12:05:45.586 -> [ 13145][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 4f:01:7f:b6:71:47, already seen it.
12:05:45.633 -> [ 13155][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 30:c6:f7:41:52:86, already seen it.
12:05:45.633 -> [ 13156][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 22:58:b2:d3:68:ce, already seen it.
12:05:45.633 -> [ 13161][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 6c:7c:00:88:59:de, already seen it.
12:05:45.633 -> [ 13177][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 43:b4:03:4f:44:18, already seen it.
12:05:45.633 -> [ 13186][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 40:4e:36:dc:8a:94, already seen it.
12:05:45.633 -> [ 13190][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 30:c6:f7:41:52:86, already seen it.
12:05:45.728 -> [ 13212][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 7e:74:68:00:36:0e, already seen it.
12:05:45.728 -> [ 13217][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 54:01:39:89:e5:2b, already seen it.
12:05:45.728 -> [ 13219][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 01:e6:ba:22:4c:7a, already seen it.
12:05:45.728 -> [ 13229][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 40:b0:03:53:cf:b3, already seen it.
12:05:45.728 -> [ 13236][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 30:c6:f7:41:52:86, already seen it.
12:05:45.728 -> [ 13243][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 43:b4:03:4f:44:18, already seen it.
12:05:45.728 -> [ 13285][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 30:c6:f7:41:52:86, already seen it.
12:05:45.775 -> [ 13294][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 69:af:5c:dc:78:0d, already seen it.
12:05:45.775 -> [ 13296][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 4c:d3:9b:97:31:d8, already seen it.
12:05:45.775 -> [ 13305][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 43:b4:03:4f:44:18, already seen it.
12:05:45.821 -> [ 13326][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 7e:74:68:00:36:0e, already seen it.
12:05:45.821 -> [ 13328][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 01:e6:ba:22:4c:7a, already seen it.
12:05:45.821 -> [ 13333][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 40:b0:03:53:cf:b3, already seen it.
12:05:45.821 -> [ 13352][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 30:c6:f7:41:52:86, already seen it.
12:05:45.821 -> [ 13353][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 55:30:69:fb:66:db, already seen it.
12:05:45.868 -> [ 13371][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 43:b4:03:4f:44:18, already seen it.
12:05:45.868 -> [ 13373][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 2f:1b:40:a7:b0:96, already seen it.
12:05:45.868 -> [ 13377][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 30:c6:f7:41:52:86, already seen it.
12:05:45.868 -> [ 13393][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 67:d7:3e:e6:8b:58, already seen it.
12:05:45.868 -> [ 13394][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 69:af:5c:dc:78:0d, already seen it.
12:05:45.916 -> [ 13423][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 4c:d3:9b:97:31:d8, already seen it.
12:05:45.916 -> [ 13429][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 30:c6:f7:41:52:86, already seen it.
12:05:45.916 -> [ 13430][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 01:e6:ba:22:4c:7a, already seen it.
12:05:45.916 -> [ 13437][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 6c:7c:00:88:59:de, already seen it.
12:05:45.916 -> [ 13470][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 4f:01:7f:b6:71:47, already seen it.
12:05:45.916 -> [ 13477][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 30:c6:f7:41:52:86, already seen it.
12:05:45.963 -> [ 13488][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 54:01:39:89:e5:2b, already seen it.
12:05:45.963 -> [ 13494][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 69:af:5c:dc:78:0d, already seen it.
12:05:45.963 -> [ 13503][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 7a:8a:5e:0e:c8:de, already seen it.
12:05:45.963 -> [ 13515][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 4c:d3:9b:97:31:d8, already seen it.
12:05:46.010 -> [ 13526][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 30:c6:f7:41:52:86, already seen it.
12:05:46.010 -> [ 13528][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 7e:74:68:00:36:0e, already seen it.
12:05:46.010 -> [ 13535][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 01:e6:ba:22:4c:7a, already seen it.
12:05:46.010 -> [ 13548][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 43:b4:03:4f:44:18, already seen it.
12:05:46.010 -> [ 13565][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 30:c6:f7:41:52:86, already seen it.
12:05:46.057 -> [ 13596][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 40:4e:36:dc:8a:94, already seen it.
12:05:46.057 -> [ 13608][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 69:af:5c:dc:78:0d, already seen it.
12:05:46.057 -> [ 13609][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 30:c6:f7:41:52:86, already seen it.
12:05:46.057 -> [ 13614][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 4c:d3:9b:97:31:d8, already seen it.
12:05:46.104 -> [ 13633][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 7e:74:68:00:36:0e, already seen it.
12:05:46.104 -> [ 13635][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 01:e6:ba:22:4c:7a, already seen it.
12:05:46.104 -> [ 13647][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 40:b0:03:53:cf:b3, already seen it.
12:05:46.104 -> [ 13648][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 2f:1b:40:a7:b0:96, already seen it.
12:05:46.104 -> [ 13656][D][BLEScan.cpp:105] handleGAPEvent(): Ignoring 43:b4:03:4f:44:18, already seen it.
12:05:46.151 -> [ 13678][W][BLEScan.cpp:75] handleGAPEvent(): ESP_GAP_SEARCH_INQ_CMPL_EVT
12:05:46.151 -> [ 13681][I][BLEDevice.cpp:622] addPeerDevice(): add conn_id: 1, GATT role: client
12:05:46.151 -> [ 13682][D][BLEDevice.cpp:148] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 5] ... Unknown
12:05:46.151 -> [ 13690][D][BLEClient.cpp:178] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 5] ... Unknown
12:05:46.293 -> [ 13783][D][BLEDevice.cpp:102] gattServerEventHandler(): gattServerEventHandler [esp_gatt_if: 4] ... Unknown
12:05:46.293 -> ***** Connect
12:05:46.293 -> [ 13784][D][BLEServer.cpp:365] onConnect(): BLEServerCallbacks
12:05:46.293 -> [ 13789][D][BLEServer.cpp:366] onConnect(): BLEServerCallbacks
12:05:46.293 -> [ 13794][D][BLEServer.cpp:367] onConnect(): BLEServerCallbacks
12:05:46.293 -> [ 13802][D][BLEDevice.cpp:148] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 5] ... Unknown
12:05:46.293 -> [ 13810][D][BLEClient.cpp:178] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 5] ... Unknown
12:05:46.293 -> [ 13819][D][BLEDevice.cpp:148] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 5] ... Unknown
12:05:46.293 -> [ 13829][D][BLEClient.cpp:178] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 5] ... Unknown
12:05:46.293 -> Client Connected *****
12:05:46.902 -> [ 14438][D][BLEDevice.cpp:148] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 5] ... Unknown
12:05:46.902 -> [ 14440][D][BLEClient.cpp:178] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 5] ... Unknown
12:05:48.311 -> Not Scanning
12:05:50.291 -> Not Scanning
12:05:52.305 -> Not Scanning

Other Steps to Reproduce

  1. Create both BLE server and BLE Client with respective callbacks
  2. Have BLEClient connect to another device
  3. Get both callbacks
  4. Disconnect Client
  5. Get Both callbacks

I have checked existing issues, online documentation and the Troubleshooting Guide

chegewara commented 2 years ago

After some more testing the Server doesn't even have to be started the callbacks just need to be set.

How did you test it? Because from your code i see the server is started:

pServer = BLEDevice::createServer();

Also from your logs i dont see anything wrong, could you isolate the logs you think are important and to describe what you do and what you expect to see or not suppose to see?

AlexMompoint commented 2 years ago
12:05:46.151 -> [ 13681][I][BLEDevice.cpp:622] addPeerDevice(): add conn_id: 1, GATT role: client
12:05:46.151 -> [ 13682][D][BLEDevice.cpp:148] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 5] ... Unknown
12:05:46.151 -> [ 13690][D][BLEClient.cpp:178] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 5] ... Unknown
12:05:46.293 -> [ 13783][D][BLEDevice.cpp:102] gattServerEventHandler(): gattServerEventHandler [esp_gatt_if: 4] ... Unknown
12:05:46.293 -> ***** Connect
12:05:46.293 -> [ 13784][D][BLEServer.cpp:365] onConnect(): BLEServerCallbacks
12:05:46.293 -> [ 13789][D][BLEServer.cpp:366] onConnect(): BLEServerCallbacks
12:05:46.293 -> [ 13794][D][BLEServer.cpp:367] onConnect(): BLEServerCallbacks
12:05:46.293 -> [ 13802][D][BLEDevice.cpp:148] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 5] ... Unknown
12:05:46.293 -> [ 13810][D][BLEClient.cpp:178] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 5] ... Unknown
12:05:46.293 -> [ 13819][D][BLEDevice.cpp:148] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 5] ... Unknown
12:05:46.293 -> [ 13829][D][BLEClient.cpp:178] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 5] ... Unknown
12:05:46.293 -> Client Connected *****
12:05:46.902 -> [ 14438][D][BLEDevice.cpp:148] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 5] ... Unknown
12:05:46.902 -> [ 14440][D][BLEClient.cpp:178] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 5] ... Unknown
12:05:48.311 -> Not Scanning

I apologize, when I seem to have misunderstood what it means to start the server, but regardless, In this section here I believe it to be what I am trying to discuss.

My expectation is that when I use the BLEClient to connect to a server while I also have a BLEServer running on the same device, I receive only a client callback which would print "Client Connected ", but what I am getting is the server callback as well as denoted by the the server call back which would print " Connect".

I also tested it with this exact sketch running on the device im viewing, and starting a BLE server with the defined name I am searching for so I have something to connect to.

AlexMompoint commented 2 years ago

bump, I seem to have found somethin from chegewara, but the solution appears to be quite outdated, so it seems to have been an issue in the past. https://www.esp32.com/viewtopic.php?t=12191

chegewara commented 2 years ago

It is not outdated at all. I would use it by myself to debug the problems with BLE.

patrickleboutillier commented 1 year ago

I'm seeing the same behavior in a sketch that has both a BLEClient and a BLEServer defined. Server callbacks are being called on Client connect/disconnect events.

AlexMompoint commented 1 year ago

@patrickleboutillier Yes I was experiencing this issue continuously using the arduino esp32 BLE library. Although not an optimal solution, I used a different library nimBLE for arduino, which fixed the issue with minimal required changes to the source code I was working with. I know this is not much of a help, just a band-aid fix since it does not deal with the issue, but it did fix the issue.

Berg0162 commented 1 year ago

I can confirm as well that I am confronted with the same behavior, very consistently and very reproducibly! I have one BLE client and one server that both/separately have defined their respective onConnect and onDisconnect callback methods. I describe the behavior that I notice. When the client connects for the first time it calls: client_Connection_Callbacks::onConnect(BLEClient pClient). When the client disconnects for the first time it calls: client_Connection_Callbacks::onDisconnect(BLEClient pClient), like is to be expected, and no strange behavior! However, the next time the client connects again, it calls first of all: server_Connection_Callbacks::onConnect(BLEServer pServer, esp_ble_gatts_cb_param_t *param) and only thereafter: client_Connection_Callbacks::onConnect(BLEClient pClient). The very same (mirrored) behavior can be observed when it disconnects again (after its first disconnect) : first of all Server::onDisconnect and only thereafter Client::onDisconnect is called... To end up with workable code I had to catch and identify these unexpected pClient calls to avoid a complete mix-up! I figured out that esp_ble_gatts_cb_param_t is filled with data of pClient and that is very helpful to identify the nature/owner of the calls, is it pClient calling? or is it pServer calling?......

see a code snippet:

void server_Connection_Callbacks::onConnect(BLEServer* pServer, esp_ble_gatts_cb_param_t *param) {
    // Get some connection parameters of the peer device.
    char fullMacAddress[18] = {}; //
    uint32_t count = pServer->getConnectedCount();
    uint16_t ConnectionHandle = param->connect.conn_id;
    uint8_t RemoteAddress[6];
    memcpy(&RemoteAddress, param->connect.remote_bda, 6);
    ConvertMacAddress(fullMacAddress, RemoteAddress, true); // true -> Native format!
    DEBUG_PRINTF("Server onConnect Callback --> Conn cnt: [%d] Conn handle: [%d] Address: [%s]\n", count, ConnectionHandle, fullMacAddress);
    // For some reason Server_Connection_Callbacks::onConnect is not only called for pServer but 
    // also called when a pClient is Connecting...???? Bug or Feature --> handle it to avoid inconsistencies....!!
    if (memcmp(RemoteAddress, Trainer.PeerAddress, 6) == 0 ) { // Check for Trainer MAC address Notice: pClient_FTMS->getConnId() == 255
      DEBUG_PRINTLN(">>> Client will Connect soon with Server device!");
      return; // Let client Connect happen....
    }   
    BLEDevice::stopAdvertising();
    // Who has been exactly connected?
    DEBUG_PRINTF("ESP32 Server connected to Client device with MAC Address: [%s] Handle: [%d]\n", fullMacAddress, param->connect.conn_id); 
etcetera

void server_Connection_Callbacks::onDisconnect(BLEServer* pServer, esp_ble_gatts_cb_param_t *param) {
    uint32_t count = pServer->getConnectedCount();
    // Get some Disconnection parameters of the peer device.
    uint16_t ConnectionHandle = param->connect.conn_id;
    uint8_t Reason = param->disconnect.reason;
    uint8_t RemoteAddress[6] = {};
    memcpy(&RemoteAddress, param->connect.remote_bda, 6);
    char fullMacAddress[18] = {}; //
    ConvertMacAddress(fullMacAddress, RemoteAddress, true); // true -> Native format!
    DEBUG_PRINTF("Server onDisconnect Callback --> Conn cnt: [%d] Conn handle: [%d] Address: [%s] Reason: [%d]\n", count, ConnectionHandle, fullMacAddress, Reason);
    // For some reason Server_Connection_Callbacks::onDisconnect is not only called for pServer but 
    // also called when a pClient is disconnecting...???? Bug or Feature --> handle it to avoid inconsistencies....!!
    if (memcmp(RemoteAddress, Trainer.PeerAddress, 6) == 0 ) { // Check for Trainer MAC address
      DEBUG_PRINTLN(">>> Client will disconnect soon from Server device!");
      return; // Let client disconnect happen....
    }
    if (Laptop.conn_handle == ConnectionHandle ) { // Laptop/Desktop is disconnected
 etcetera

see the debug_print output and compare with above code (notice that the pServer connection is meanwhile doing its work, and also reporting!)

. . 15:43:56.684 -> Set Indoor Bike Simulation Parameters! 15:43:56.684 -> Wind speed (1000): 0.00 | Grade (100): 0.00 | Crr (10000): 0.00 | Cw (100): 0.51 15:43:56.718 -> -> Client Rec'd Raw FTM Control Point Response Data: [0] [3] [ 80 11 01 ] 15:44:34.303 -> Server onDisconnect Callback --> Conn cnt: [2] Conn handle: [0] Address: [FD:6C:BD:F3:50:95] Reason: [8] 15:44:34.336 -> >>> Client will disconnect soon from Server device! 15:44:34.336 -> Client Disconnected from Server device with Name: [Sim nRF52] Mac Address: [FD:6C:BD:F3:50:95]! 15:44:34.336 -> Client Starts Scanning again for Server Device with CPS, CSC and FTMS! . . 15:44:43.438 -> Set Indoor Bike Simulation Parameters! 15:44:43.438 -> Wind speed (1000): 0.00 | Grade (100): 0.19 | Crr (10000): 0.00 | Cw (100): 0.51 15:44:43.890 -> Central Updated CCCD --> HR_Measurement Notify Enabled! 15:44:45.168 -> Found advertising Peripheral with FTMS enabled! See data: 15:44:45.211 -> Name: Sim nRF52, Address: fd:6c:bd:f3:50:95, serviceUUID: 00001818-0000-1000-8000-00805f9b34fb, serviceUUID: 00001816-0000-1000-8000-00805f9b34fb, serviceUUID: 00001826-0000-1000-8000-00805f9b34fb, txPower: 4, rssi: -39 15:44:45.288 -> --> Raw FTM Control Point Data [len: 7] [OpCode: 11] [Values: 00 00 13 00 28 33 00 ] 15:44:45.288 -> Set Indoor Bike Simulation Parameters! 15:44:45.288 -> Wind speed (1000): 0.00 | Grade (100): 0.19 | Crr (10000): 0.00 | Cw (100): 0.51 15:44:45.451 -> Server onConnect Callback --> Conn cnt: [1] Conn handle: [1] Address: [FD:6C:BD:F3:50:95] 15:44:45.451 -> >>> Client will Connect soon with Server device! 15:44:45.451 -> ESP32 Client connected to Server device with Name: [Sim nRF52] MAC Address: [FD:6C:BD:F3:50:95] Handle: [1] MTU: [20] 15:44:45.451 -> Now checking all Client Services and Characteristics! 15:44:45.451 -> If Mandatory Services Fail --> the Client will disconnect! 15:44:45.490 -> Client Generic Access: Found! 15:44:46.847 -> -> Client Reads Device Name: [Sim nRF52] . .

Berg0162 commented 1 year ago

bump, I seem to have found somethin from chegewara, but the solution appears to be quite outdated, so it seems to have been an issue in the past. https://www.esp32.com/viewtopic.php?t=12191

I have tested this suggestion but I could NOT find any change in behavior during connect and/or disconnect!