Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stops working after reconnect (windows only) #328

Open
hilman2 opened this issue Oct 27, 2024 · 0 comments
Open

stops working after reconnect (windows only) #328

hilman2 opened this issue Oct 27, 2024 · 0 comments

Comments

@hilman2
Copy link

hilman2 commented Oct 27, 2024

At first I thought it was due to my code, so I tested it with an absolute minimal configuration: same result.
Setup: Seed Studio XIAO ESP32C3 with button connected to D0<->GND
Arduino IDE with esp32 2.0.17
On Windows 10/11, the device is detected and can be paired.
After pairing, everything works perfectly. When you press the button, "i" characters are typed into Notepad.
When you restart the ESP, the Windows laptop, or even just the Bluetooth on the laptop, the paired device reconnects, but no letters are typed into Notepad anymore. Something is happening though, as the cursor briefly stops blinking.

This issue doesn't occur on iOS, Android, ChromeOS, or Linux.
I even tested it with a live system on the same hardware where it wasn't working under Windows:
With Ubuntu, there's no problem at all - after resetting the ESP, it reconnects immediately and you can type letters again.
If I remove the device under Windows and pair it again, it works again - until the next reconnection.
Any ideas? Or has anyone experienced the same problem?

The Code I use for testing.

#include <BleKeyboard.h>

// Initialize the BLE Keyboard with a name and manufacturer
BleKeyboard bleKeyboard("Button Keyboard", "MyCompany", 100);

// Define the button pin
const int BUTTON_PIN = 2; // D0 = GPIO2

// Variables for button debouncing
bool lastButtonState = HIGH; // Initial state (Pull-Up)
unsigned long lastDebounceTime = 0;
const unsigned long debounceDelay = 50; // Debounce time in milliseconds

void setup() {
  // Start serial communication for debugging (optional)
  Serial.begin(115200);
  Serial.println("Starting BLE Keyboard...");

  // Configure the button pin as input with internal pull-up resistor
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  Serial.println("Button pin configured as INPUT_PULLUP.");

  // Start the BLE Keyboard
  bleKeyboard.begin();
  Serial.println("BLE Keyboard initialized.");
}

void loop() {
  // Check if the BLE Keyboard is connected
  if (bleKeyboard.isConnected()) {
    // Read the current state of the button
    int reading = digitalRead(BUTTON_PIN);

    // Check if the button state has changed (debouncing)
    if (reading != lastButtonState) {
      lastDebounceTime = millis();
    }

    // If the state has been stable for longer than the debounce delay
    if ((millis() - lastDebounceTime) > debounceDelay) {
      // If the button is pressed (LOW because of Pull-Up)
      if (reading == LOW) {
        Serial.println("Button pressed. Sending 'i'.");
        bleKeyboard.print("i");
        // Short delay to prevent multiple sends
        delay(200);
      }
    }

    // Update the last button state
    lastButtonState = reading;
  }

  // Short delay to reduce CPU usage
  delay(10);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant