T-vK / ESP32-BLE-Keyboard

Bluetooth LE Keyboard library for the ESP32 (Arduino IDE compatible)
2.28k stars 379 forks source link

There was no response after my Bluetooth connection #232

Open SuperPrintf opened 1 year ago

SuperPrintf commented 1 year ago

Hello author, I am testing using MacBook after trying to burn the program for ESP32S3. After connecting to the Bluetooth keyboard using my Mac, there was no response from my Mac, but I can still observe through UART that ESP32S3 did indeed send a message. I want to know whether it's a problem with my code or with the MCU

This is the code I used for testing

#include <Arduino.h>
#include <BleKeyboard.h>
BleKeyboard bleKeyboard("ESP_Keyboard","Espressif",100);

// Dispelling trembling
#define TIMER_TAG 500 // TIMER_TAG * 0.1mS
int32_t tag = 0;
// IO configuration
const uint8_t KEY_1 = 0;
const uint8_t LED_1 = 10;
const uint8_t LED_2 = 11;
// Timer pointer
hw_timer_t *timer_0 = NULL;
// Interrupt service routine
void IRAM_ATTR TimerEvent(void);

void setup() {
  pinMode(KEY_1, INPUT);
  pinMode(LED_1, OUTPUT);
  pinMode(LED_2, OUTPUT);

  Serial.begin(115200);
  bleKeyboard.begin();
  Serial.println("Starting BLE work!");

  timer_0 = timerBegin(0, 80, true);// Timer initialization
  timerAttachInterrupt(timer_0, &TimerEvent, true);// Interrupt handling function for binding timers
  timerAlarmWrite(timer_0, 100, true);// Timer interrupt counter
  timerAlarmEnable(timer_0); // Timer open

  digitalWrite(LED_2, HIGH);
}

void loop() {
  if(bleKeyboard.isConnected()) {
    digitalWrite(LED_2, LOW);

    Serial.println("Sending 'Hello world'...");
    bleKeyboard.print("Hello world");

    while(true) {
      if(digitalRead(KEY_1) == LOW and tag == 0){
        bleKeyboard.write('C');
        tag = TIMER_TAG;
        digitalWrite(LED_1, HIGH);
        Serial.println("C");
      }
    }
  }
  else{
    Serial.println("The Bluetooth not turned on!");
    delayMicroseconds(100000);
  }
}

// Interrupt service routine
void IRAM_ATTR TimerEvent(void){
  if(tag > 0){
    tag--;
  }
  else{
    digitalWrite(LED_1, LOW);
  }
}