BLE connection problems

Hi

Im completely new to Arduino and wanted to start with it but I have always the same issue. When I upload my sketch to my Arduino Nano ESP32 it disconnects instantly after. I want to use it over Bluetooth and I've heard that it's normal then that it disconnects, but it disconnects also if I dont use Bluetooth and I can't see the Arduino as a Bluetooth device. I've used several different sketches, also the example ones and it didnt work. I don't know if it's an issue with the drivers or a Version of something.

What else I can say is I use the newest Arduino IDE. I use the ESP BLE32 Keyboard Library. I have the Arduino ESP32 Boards 20.0.18-arduino.5 Board Manager installed. I have as additional board manager URL this URL: "https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json". My selected Board is the "Arduino Nano ESP32 - Arduino ESP32 boards"

If you want to see my main Code: (Yes I am ashamed that I let Chatgpt generate my Code :()

#include <BleKeyboard.h>

// Bluetooth Keyboard erstellen
// Name = was du am PC siehst
BleKeyboard bleKeyboard("ESP32 Deck", "Custom", 100);

// ==========================
// 🔌 PIN KONFIGURATION
// ==========================

// Hier DEINE verkabelten Pins eintragen
const int rowPins[4] = { 3, 4, 5, 6 };      // Rows (Ausgänge)
const int colPins[5] = { 7, 8, 9, 10, 11 };  // Columns (Eingänge)

// ==========================
// 🔁 SYSTEM STATUS
// ==========================

// true = NUMPAD, false = STREAM DECK
bool isNumpadMode = true;

// aktuelle Seite (0,1,2)
int currentPage = 0;

// für LONG PRESS (Taste 20)
unsigned long pressStart = 0;
bool keyHeld = false;

// ==========================
// 🔢 KEYMAP (PHYSISCHES LAYOUT)
// ==========================

// Jede Zahl entspricht einer Taste
// Passe das an, wenn dein Layout anders ist!
int keymap[4][5] = {
  { 1, 2, 3, 4, 5 },
  { 6, 7, 8, 9, 10 },
  { 11, 12, 13, 14, 15 },
  { 16, 17, 18, 19, 20 }  // Taste 20 = MODE + PAGE
};

// ==========================
// ⚙️ SETUP
// ==========================

void setup() {
  bleKeyboard.begin();  // Bluetooth starten

  // Rows als OUTPUT setzen
  for (int i = 0; i < 4; i++) {
    pinMode(rowPins[i], OUTPUT);
    digitalWrite(rowPins[i], LOW);
  }

  // Columns als INPUT_PULLUP setzen
  // → Taste gedrückt = LOW
  for (int i = 0; i < 5; i++) {
    pinMode(colPins[i], INPUT_PULLUP);
  }
}

// ==========================
// 🔄 LOOP (MATRIX SCAN)
// ==========================

void loop() {

  // Nur wenn Bluetooth verbunden
  if (!bleKeyboard.isConnected()) return;

  // Jede Row einzeln aktivieren
  for (int r = 0; r < 4; r++) {

    digitalWrite(rowPins[r], HIGH);

    for (int c = 0; c < 5; c++) {

      // Wenn Taste gedrückt
      if (digitalRead(colPins[c]) == LOW) {
        handlePress(keymap[r][c]);
      }
    }

    digitalWrite(rowPins[r], LOW);
  }
}

// ==========================
// 🔘 TASTEN VERARBEITUNG
// ==========================

void handlePress(int keyID) {

  // ======================
  // 🎯 SPEZIALTASTE (20)
  // ======================
  if (keyID == 20) {

    // Startzeit merken
    if (!keyHeld) {
      pressStart = millis();
      keyHeld = true;
    }

    // LONG PRESS → MODE wechseln
    if (millis() - pressStart > 600) {
      isNumpadMode = !isNumpadMode;
      keyHeld = false;
      delay(500);  // Schutz vor mehrfach auslösen
    }

  } else {
    // Normale Taste
    sendKey(keyID);
    delay(200);  // Debounce
  }

  // ======================
  // SHORT PRESS → PAGE wechseln
  // ======================
  if (keyID == 20 && digitalRead(colPins[4]) == HIGH) {

    if (keyHeld) {

      // Nur im Stream Deck Mode
      if (!isNumpadMode) {
        currentPage++;

        // 3 Seiten → 0,1,2
        if (currentPage > 2) currentPage = 0;
      }

      keyHeld = false;
    }
  }
}

// ==========================
// 🔁 MODE SWITCH
// ==========================

void sendKey(int keyID) {
  if (isNumpadMode) {
    sendNumpad(keyID);
  } else {
    sendStreamDeck(keyID);
  }
}

// ==========================
// 🔢 NUMPAD (ALLE 20 TASTEN BELEGT)
// ==========================

void sendNumpad(int keyID) {

  switch (keyID) {

    case 1: bleKeyboard.print("1"); break;
    case 2: bleKeyboard.print("2"); break;
    case 3: bleKeyboard.print("3"); break;
    case 4: bleKeyboard.print("4"); break;
    case 5: bleKeyboard.print("5"); break;

    case 6: bleKeyboard.print("6"); break;
    case 7: bleKeyboard.print("7"); break;
    case 8: bleKeyboard.print("8"); break;
    case 9: bleKeyboard.print("9"); break;
    case 10: bleKeyboard.print("0"); break;

    case 11: bleKeyboard.print("+"); break;
    case 12: bleKeyboard.print("-"); break;
    case 13: bleKeyboard.print("*"); break;
    case 14: bleKeyboard.print("/"); break;
    case 15: bleKeyboard.print("="); break;

    case 16: bleKeyboard.press(KEY_RETURN); break;
    case 17: bleKeyboard.press(KEY_BACKSPACE); break;
    case 18: bleKeyboard.press(KEY_ESC); break;
    case 19:
      bleKeyboard.press(KEY_TAB);
      break;

      // Taste 20 = Spezialtaste → keine Aktion hier
  }

  bleKeyboard.releaseAll();
}

// ==========================
// 🎛️ STREAM DECK (3 PAGES)
// ==========================

void sendStreamDeck(int keyID) {

  // ======================
  // PAGE 0
  // ======================
  if (currentPage == 0) {

    switch (keyID) {

      case 1:
        bleKeyboard.press(KEY_LEFT_CTRL);
        bleKeyboard.press('c');
        break;
      case 2:
        bleKeyboard.press(KEY_LEFT_CTRL);
        bleKeyboard.press('v');
        break;
      case 3:
        bleKeyboard.press(KEY_LEFT_CTRL);
        bleKeyboard.press('x');
        break;
      case 4:
        bleKeyboard.press(KEY_LEFT_CTRL);
        bleKeyboard.press('z');
        break;
      case 5:
        bleKeyboard.press(KEY_LEFT_CTRL);
        bleKeyboard.press('y');
        break;

      case 6:
        bleKeyboard.press(KEY_LEFT_CTRL);
        bleKeyboard.press('s');
        break;
      case 7:
        bleKeyboard.press(KEY_LEFT_CTRL);
        bleKeyboard.press('p');
        break;
      case 8:
        bleKeyboard.press(KEY_LEFT_CTRL);
        bleKeyboard.press('a');
        break;
      case 9:
        bleKeyboard.press(KEY_LEFT_CTRL);
        bleKeyboard.press('f');
        break;
      case 10:
        bleKeyboard.press(KEY_LEFT_CTRL);
        bleKeyboard.press('n');
        break;

      case 11: bleKeyboard.print("Hello"); break;
      case 12: bleKeyboard.print("Test"); break;
      case 13: bleKeyboard.print("ESP32"); break;
      case 14: bleKeyboard.print("StreamDeck"); break;
      case 15: bleKeyboard.print("OK"); break;

      case 16:
        bleKeyboard.press(KEY_LEFT_GUI);
        bleKeyboard.press('d');
        break;
      case 17:
        bleKeyboard.press(KEY_LEFT_ALT);
        bleKeyboard.press(KEY_TAB);
        break;
      case 18: bleKeyboard.press(KEY_MEDIA_PLAY_PAUSE); break;
      case 19: bleKeyboard.press(KEY_MEDIA_VOLUME_UP); break;
    }
  }

  // ======================
  // PAGE 1
  // ======================
  if (currentPage == 1) {

    switch (keyID) {

      case 1: bleKeyboard.print("Page2-1"); break;
      case 2: bleKeyboard.print("Page2-2"); break;
      case 3: bleKeyboard.print("Page2-3"); break;
      case 4: bleKeyboard.print("Page2-4"); break;
      case 5: bleKeyboard.print("Page2-5"); break;

      case 6: bleKeyboard.press(KEY_F1); break;
      case 7: bleKeyboard.press(KEY_F2); break;
      case 8: bleKeyboard.press(KEY_F3); break;
      case 9: bleKeyboard.press(KEY_F4); break;
      case 10: bleKeyboard.press(KEY_F5); break;

      case 11: bleKeyboard.press(KEY_F6); break;
      case 12: bleKeyboard.press(KEY_F7); break;
      case 13: bleKeyboard.press(KEY_F8); break;
      case 14: bleKeyboard.press(KEY_F9); break;
      case 15: bleKeyboard.press(KEY_F10); break;

      case 16: bleKeyboard.press(KEY_F11); break;
      case 17: bleKeyboard.press(KEY_F12); break;
      case 18: bleKeyboard.press(KEY_HOME); break;
      case 19: bleKeyboard.press(KEY_END); break;
    }
  }

  // ======================
  // PAGE 2
  // ======================
  if (currentPage == 2) {

    switch (keyID) {

      case 1: bleKeyboard.print("OBS1"); break;
      case 2: bleKeyboard.print("OBS2"); break;
      case 3: bleKeyboard.print("OBS3"); break;
      case 4: bleKeyboard.print("OBS4"); break;
      case 5: bleKeyboard.print("OBS5"); break;

      case 6:
        bleKeyboard.press(KEY_LEFT_GUI);
        bleKeyboard.press(KEY_LEFT_SHIFT);
        bleKeyboard.press('s');
        break;
      case 7: bleKeyboard.press(KEY_TAB); break;
      case 8: bleKeyboard.press(KEY_INSERT); break;
      case 9: bleKeyboard.press(KEY_DELETE); break;
      case 10: bleKeyboard.press(KEY_PAGE_UP); break;

      case 11: bleKeyboard.press(KEY_PAGE_DOWN); break;
      case 12: bleKeyboard.press(KEY_UP_ARROW); break;
      case 13: bleKeyboard.press(KEY_DOWN_ARROW); break;
      case 14: bleKeyboard.press(KEY_LEFT_ARROW); break;
      case 15: bleKeyboard.press(KEY_RIGHT_ARROW); break;

      case 16: bleKeyboard.press(KEY_MEDIA_VOLUME_DOWN); break;
      case 17: bleKeyboard.press(KEY_MEDIA_MUTE); break;
      case 18: bleKeyboard.press(KEY_MEDIA_NEXT_TRACK); break;
      case 19: bleKeyboard.press(KEY_MEDIA_PREVIOUS_TRACK); break;
    }
  }

  delay(50);
  bleKeyboard.releaseAll();
}

Show the error you received.

I've got no error but I should be able to connect the Arduino over Bluetooth but it won't appear

Have you asked Chat why it does not work?

Yeah I tried a lot with him but he always said the same stuff or he told me things that I can't even do.

Then I would toss that code in the trash and start with something that actually works or write your own code.

Have you tried any of the example sketches?

I've tried examples from the extra library and normal examples from the ide but nothing worked

What BLE device are you trying to connect to?

My PC. I have a Bluetooth dongle connected to it.

Is it a Mac?

No it's a win11 pc

Can it do BLE?
The nano esp32 can only do BLE

What examples have you tried?

I think it should support it. Anyway I have made a lot progress. I tried more with chatgpt and then changed the module in the ide to ESP32S3 Dev Module and made some changes in the tools tab. Now somehow i can upload my sketch on com5 and stay connected with the arduino on com6. I could also connect the arduino to my pc but it disconnected and reconnected all the time. Now Chatgpt said i should use the nimBLE-Arduino library and the ESP32 BLE Keyboard libraries. Now I can't connect it anymore with Bluetooth. But i see on the Serial Monitor BLE advertising...

Sorry but I don't have windows 11. All I can say is that I've used the BLE serial app on my ardroid phone with no problems.

It may not be hurting anything, but the ESP32 family is now included in the IDE, so your additional board URL is at least redundant, and at worst, causing problems.

Second issue, there are I think 3 kinds of Bluetooth, they do not talk to each other so make sure your board and other device speak the same lingo.

It sounds like you're having trouble uploading a sketch, but I need a little more context to help you out. Could you clarify a few things?

  • What platform are you using to upload the sketch (e.g., Arduino IDE, another coding environment)?
  • Are you receiving any specific error messages?
  • Is it related to a Java or Arduino sketch, or is it something else?

Im using the arduino ide. Right at the moment i can upload a sketch and even make a connection with Bluetooth to my pc, but this connection is very unstable and it disconnects and reconnects every second. I've installed now the nimBLE-Arduino Library to stabilise the connection but if i try to use thid library, Bluetooth works no more, but the serial connection works and i can receive something over it.

@leanm ... Immediately after this statement, place a Serial.print() to verify BLE is not connecting.

@waqassb
You need to tell chatGPT to read the answers to reduce redundancy.

Post #11.

Post #6.

chatGPT sounds like it read a GP doctor's manual, "Could get better, could get worse, could be the same." Maybe this will fix it: "Could be anything."

Since you are not actually having an upload problem, I suggest you change your topic title to "BLE connecion problems". You might receive more relevant help.

Show your updated sketch along with the errors you observe.