Skip to content

disconnect() does not trigger callback #66

Open
@rmlearney-digicatapult

Description

@rmlearney-digicatapult

Example:

  1. Initialise BLE
  2. Create callbacks for central connection and disconnection to turn LED on and off
  3. Connect to central
  4. disconnect() after 10 seconds

Expected behaviour - disconnection from central should trigger callback and turn off LED
Actual behaviour - disconnection from central with disconnect() method does not trigger callback

#include <ArduinoBLE.h>

BLEService TestService("DEAD");
BLECharacteristic TestData("BEEF", BLEIndicate | BLENotify, 2, true);
BLEDescriptor TestDescriptor("BEEF", "Test");

uint32_t genericTimer = 0;

void setup(){

  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);

  initBLE();
}

void loop(){
  BLEDevice central = BLE.central(); // begin listening for centrals to connect

  if(central){
    genericTimer = millis();
    while(central.connected()){
      if(millis() - genericTimer >= 10000){ // Wait 10 seconds
        BLE.disconnect(); // Disconnect BLE - LED should go out
      }
    }
  }
}

void initBLE(void){ 
  BLE.begin();
  BLE.setEventHandler(BLEConnected, blePeripheralConnectHandler);
  BLE.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler);
  BLE.setLocalName("TestName");
  BLE.setDeviceName("Test Device Name");
  TestService.addCharacteristic(TestData);
  TestData.addDescriptor(TestDescriptor);
  BLE.addService(TestService);
  BLE.setAdvertisedService(TestService);
  BLE.advertise();
}

void blePeripheralConnectHandler(BLEDevice central){
    // turn on the LED to indicate the connection:
    digitalWrite(LED_BUILTIN, HIGH);
}

void blePeripheralDisconnectHandler(BLEDevice central){
    // when the central disconnects, turn off the LED:
    digitalWrite(LED_BUILTIN, LOW);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions