Open
Description
Example:
- Initialise BLE
- Create callbacks for central connection and disconnection to turn LED on and off
- Connect to central
- 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);
}