Skip to content

OnConnect() callback is not triggring #975

Closed
@dinesh-slx

Description

@dinesh-slx

I am using nimble version 2.2.3, with extended advertisiig i am trying to get the multiple connection for the very first time connect is going to onConnect and advertising for the next device to connect but when i do second connect from different device it is not trigerring onConnect because of that i am unabe to do multiple connection and i have provice the serial logs also

#include <NimBLEDevice.h>
#include <NimBLEExtAdvertising.h>
#include "ble.h"
#include "NimBLECharacteristic.h"
#include "NimBLEService.h"
#include "NimBLEUUID.h"
#include "bond.h"
#include "device_info.h"
#include "device_status.h"
#include "error.h"
#include "esp32-hal-log.h"
#include "esp32-hal.h"
#include "gatt.h"
#include "neopixel.h"
#include "ringtone.h"
#include <Adafruit_NeoPixel.h>
#include <string>
#include <strings.h>
// #include "device_info.h"

uint8_t primaryPhy = BLE_HCI_LE_PHY_1M;
uint8_t secondaryPhy = BLE_HCI_LE_PHY_1M;

NimBLEExtAdvertisement extAdv(primaryPhy, secondaryPhy);

/*

NimBLE-Arduino API doc:
https://h2zero.github.io/NimBLE-Arduino/

BLE Connection Parameters:
https://software-dl.ti.com/lprf/simplelink_cc2640r2_latest/docs/blestack/ble_user_guide/html/ble-stack-3.x/gap.html
https://www.btframework.com/connparams.htm
https://punchthrough.com/manage-ble-connection/
https://interrupt.memfault.com/blog/ble-throughput-primer

TODO: api for ble power control by central setting different connections parameters
- increase throughput for OTA firmware transfer

*/

static const char *TAG = "ble";
static uint16_t connHandle;

class ServerCallbacks : public NimBLEServerCallbacks {
  void onConnect(NimBLEServer *pServer, NimBLEConnInfo& connInfo) {
    NimBLEAddress centralAddr = connInfo.getAddress();
    ESP_LOGI(TAG, "central connected: %s", centralAddr.toString().c_str());
    connHandle = connInfo.getConnHandle();
    pServer->updateConnParams(connHandle,24,40,0,500);
    bool phyUpdated = pServer->updatePhy(connHandle, BLE_HCI_LE_PHY_1M_PREF_MASK, BLE_HCI_LE_PHY_1M_PREF_MASK, 0);
    ESP_LOGI(TAG, "PHY update %s", phyUpdated ? "success" : "failed");
    
    NimBLEDevice::startAdvertising(0);
  };

  void onDisconnect(NimBLEServer *pServer, NimBLEConnInfo& connInfo, int reason) {
    NimBLEAddress centralAddr = connInfo.getAddress();
    ESP_LOGI(TAG, "central disconnected: %s", centralAddr.toString().c_str());
    NimBLEDevice::startAdvertising(0);
  };
};

class CharacteristicCallbacks : public NimBLECharacteristicCallbacks {
  void onRead(NimBLECharacteristic *pCharacteristic, NimBLEConnInfo& connInfo) {
    
  };

  void onWrite(NimBLECharacteristic *pCharacteristic, NimBLEConnInfo& connInfo) {
  };
};

static CharacteristicCallbacks chrCallbacks;

void addServices(NimBLEServer *pServer);

NimBLEServer *InitBLE() {
  NimBLEDevice::init("VST-CL");

  // Get the device mac address
  NimBLEAddress address = NimBLEDevice::getAddress();

  // const uint8_t *native = address.getNative();
  const uint8_t *native = address.getBase()->val;

  // Compose unique device name by using the Lower Address Part (3 bytes) of bluetooth device
  // address, which uniquely identifies the device as long as we stick to ESP controllers.
  char buffer[14];
  sprintf(buffer, "VST-CL-%02X%02X%02X", native[2], native[1], native[0]);
  NimBLEDevice::setDeviceName(buffer);
  // NimBLEDevice::getAdvertising()->setName(buffer);
  ESP_LOGI(TAG, "Device name: %s", buffer);
  ESP_LOGI(
      TAG,
      "DeviceInfo: {Manufacturer:%s Serial: %s Model: %s HardwareVersion: %s FirmwareVersion: %s}",
      GetManufacturer(), GetSerial(), GetModel(), GetHardwareVersion(), GetFirmwareVersion());

  

  // Set BLE TX power
  // https://docs.espressif.com/projects/esp-idf/en/v4.4.6/esp32s3/api-reference/bluetooth/controller_vhci.html#_CPPv420esp_ble_tx_power_set20esp_ble_power_type_t17esp_power_level_t
  NimBLEDevice::setPower(ESP_PWR_LVL_P9); /** +9db*/
  NimBLEDevice::setSecurityAuth(true, false, true);
  NimBLEDevice::setSecurityIOCap(BLE_HS_IO_NO_INPUT_OUTPUT);

  NimBLEServer *pServer = NimBLEDevice::createServer();
  addServices(pServer);
  // NimBLEExtAdvertisement extAdv;

  extAdv.setName(buffer);
  extAdv.setConnectable(true);
  extAdv.addTxPower();
  extAdv.setFlags(0x06);
  // extAdv.addServiceUUID(NimBLEUUID(LIGHT_SERVICE));
  // extAdv.addServiceUUID(NimBLEUUID(AUDIO_SERVICE));
  // extAdv.addServiceUUID(NimBLEUUID(DEVICE_INFO_SERVICE));
  // extAdv.addServiceUUID(NimBLEUUID(DEVICE_STATUS_SERVICE));
  // extAdv.addServiceUUID(NimBLEUUID(BOND_SERVICE));

  extAdv.setMinInterval(0x20);
  extAdv.setMaxInterval(0x40);
  extAdv.setAppearance(0x0340); 
  return pServer;
}

void addServices(NimBLEServer *pServer) {
  pServer->setCallbacks(new ServerCallbacks());

  
  extAdv.setMinInterval(0x20);
  extAdv.setMaxInterval(0x40);
  extAdv.setAppearance(0x0340); 
}

bool StartAdvertising() {
  // NimBLEAdvertising *pAdvertising = NimBLEDevice::getAdvertising();
  // pAdvertising->addTxPower();
  // return pAdvertising->start();
  NimBLEExtAdvertising *pAdvertising = NimBLEDevice::getAdvertising();
  // Pass the configured advertisement to instance 0.
  pAdvertising->setInstanceData(0, extAdv);
  // Start advertising using instance ID 0.
  pAdvertising->start(0);
  return true;
}

uint16_t GetConnHandle() { return connHandle; }

void setup() {
  
  
  NimBLEServer *pServer = InitBLE();

  bool ok = StartAdvertising();

}

void loop() {delay(100);}

Serial logs

[12:04:08:432] [ 10547][I][ble.cpp:48] onConnect(): [ble] central connected: e8:48:b8:c8:20:00␍␊
[12:04:08:441] [ 10558][I][ble.cpp:52] onConnect(): [ble] PHY update success␍␊
[12:04:08:450] D NimBLEServer: << handleGapEvent␍␊
[12:04:08:450] D NimBLEServer: >> handleGapEvent: ␍␊
[12:04:08:450] D NimBLEExtAdvertisingCallbacks: onStopped: Default␍␊
[12:04:08:454] D NimBLEServer: >> handleGapEvent: ␍␊
[12:04:08:459] D NimBLEServerCallbacks: onPhyUpdate: default, txPhy: 1, rxPhy: 1␍␊
[12:04:08:547] D NimBLEServer: >> handleGapEvent: ␍␊
[12:04:08:547] D NimBLEServer: << handleGapEvent␍␊
[12:04:08:553] D NimBLEServer: >> handleGapEvent: ␍␊
[12:04:08:553] I NimBLEServer: mtu update event; conn_handle=1 mtu=517␍␊
[12:04:08:563] D NimBLEServerCallbacks: onMTUChange(): Default␍␊
[12:04:08:563] D NimBLEServer: << handleGapEvent␍␊
[12:04:09:762] D NimBLEServer: >> handleGapEvent: ␍␊
[12:04:09:762] I NimBLEServer: subscribe event; attr_handle=8, subscribed: true␍␊
[12:04:09:771] D NimBLEServer: << handleGapEvent␍␊
[12:04:17:032] D NimBLEServer: >> handleGapEvent: ␍␊
[12:04:17:032] D NimBLEExtAdvertisingCallbacks: onStopped: Default␍␊

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions