Bluetooth disconnecting


Just started using Arduino IDE for programming ESP32 boards.

I have a project that requires a small wireless two button remote or macro pad if you will. One button for 'ESC' and another for 'F5' and possible a third for 'ENTER'.

Now I have been trying the code below, which works great after the initial Bluetooth connection is made then after approximately 10 minutes the connection is lost and the only way to reconnect it to remove the device in the Windows setting and re-add.

Is there a piece of code that I'm missing that can keep the connection 'alive' if a button push is not detected?

Any another suggestions welcome.

Windows 10
Lolin D32 ESP32

#include <Key.h>
#include <Keypad.h>

#include <BleConnectionStatus.h>
#include <BleKeyboard.h>
#include <KeyboardOutputCallbacks.h>

#include <BluetoothSerial.h>

const byte ROWS = 1; //one rows
const byte COLS = 2; //two columns
int ledpin = 5;

// the library will return the character inside this array
// when the appropriate button is pressed.
char keys[ROWS][COLS] = {
  {'1', '2'},

// Looking at the keypad from the front, the row pins are on the left.

byte rowPins[ROWS] = {21}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {22, 23}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it

BluetoothSerial SerialBT;
BleKeyboard bleKeyboard("LIGHTING(1)", "Steven_Moseley_2021", 100);

void setup() {
  pinMode(ledpin, OUTPUT);

void loop() {

  char key = keypad.getKey();

  // Only do something with the keypress if we
  // are connected to something via bluetooth

  if (bleKeyboard.isConnected() && key) {

    switch (key) {
      case '1':;
      case '2':;
        digitalWrite(ledpin, LOW);

    bleKeyboard.releaseAll(); // this releases the buttons

Hi @steven_moseley87
If this sketch compiles without these libraries, why use them.

//#include <Key.h>
//#include <BleConnectionStatus.h>
//#include <KeyboardOutputCallbacks.h>

Try without them and see if it corrects the fault.

RV mineirin