ESP32 BLE Keyboard always connects and reconnects

Hi there
I have a Waveshare ESP32-S3-Zero on which i run the following program below. Its not my final program but i just wantet to test the Library. It is named mod because i made a modification to i because of an error which occured due to conversions.

I can connect to the ESP with my I-Pad for example but if i watch the serial monitor it always connects and disconnects. Has someone have a similar Problem?

Thanks for helping!

/* TO DO
- Led auf board nutzen um zu zeigen ob connected ist
- Batteriestand auslesen und anpassen


*/


#include <BleKeyboard_mod.h>

// Pins für Rechts-/Linkspedale
#define LEFT_PEDAL_PIN  12
#define RIGHT_PEDAL_PIN 14  

BleKeyboard bleKeyboard("ESP32", "Me", 100);

void setup() {
  Serial.begin(115200);
  Serial.println("BLE gestartet!");
  bleKeyboard.begin();

  // Pedal Pins mit Pull-Up Resistor initialisieren
  pinMode(LEFT_PEDAL_PIN, INPUT_PULLUP);
  pinMode(RIGHT_PEDAL_PIN, INPUT_PULLUP);
}

void loop() {
  if(bleKeyboard.isConnected()) {
    Serial.println("BLE Connected");

    //prüfen ob Pedale gedrückt werden
    if(!digitalRead(LEFT_PEDAL_PIN)){
      Serial.println("Linkes Pedal gedrückt");
      bleKeyboard.write(KEY_LEFT_ARROW);
    }
    if(!digitalRead(RIGHT_PEDAL_PIN)){
      Serial.println("Rechtes Pedal gedrückt");
      bleKeyboard.write(KEY_RIGHT_ARROW);
    }


    delay(50);
  }

  Serial.println("Waiting for connection ...");
  delay(1000);
}

Not sure if it is completely related but I was having the same issue with ESP32C3 and Windows 11. (BLEKeyboard version 0.3.2)

I fixed it by changing this line in the BLEkeyboard library. solution link

From:

pSecurity->setAuthenticationMode(ESP_LE_AUTH_REQ_SC_MITM_BOND);

To:

pSecurity->setAuthenticationMode(ESP_LE_AUTH_BOND);