T-vK / ESP32-BLE-Keyboard

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

print() method do not send more than 16 chars per call. #171

Open aopenna opened 2 years ago

aopenna commented 2 years ago

Hello,

I'm working on a prototype that read from a HID USB device and send data as a bluetooth keyboard. I use the USB host library to read data from the USB device, and i have a custom HID parser to read the data. This is working flawlessly.

than I added the BLe keyboard, and to send data as the BLE keyboard I write the data processed by custom parser on an char array, and set a flag that will be read on loop (as below)

The problem is if the array has more than 16 chars the paried device only received the first 16, some time 20, but nothing more. As an workaround I send in 16 chars chunks until the end of array. Even using this workaround I needed to put a 100 ms delay in order to not lost any chars.

Anyone have the similar problem?

void loop()
{
  if (sendCharstoBLE) {
    sendCharstoBLE = false;
    Notify(PSTR("\r\nSent to BLE\r\n"), 0x80);
    if (bleKeyboard.isConnected()) { //send to BLE
      Notify(PSTR(" BT connected, sending..."), 0x80);
      int i;
      String charSubstring;
      for ( i = 0; i <= charstoBLE.length(); i += 16) {
        Notify(Serial.print(charSubstring),0x80);
        Notify(PSTR("\r\n"),0x80);
        Notify(PSTR(charSubstring.length()),0x80);
        charSubstring = charstoBLE.substring(i, 16);
        bleKeyboard.print(charSubstring);
        delay(100);
      }
      charSubstring = charstoBLE.substring(i);
      if (charSubstring.length() > 0) {
        Notify(Serial.print(charSubstring),0x80);
        Notify(PSTR("\r\n"),0x80);
        Notify(PSTR(charSubstring.length()),0x80);
        bleKeyboard.print(charSubstring);
        delay(300);
      }
      //bleKeyboard.print(charstoBLE // send all chars is not working, disabling for while
      delay(300);
      bleKeyboard.write(KEY_RETURN);
      delay(100);
    } else {
      Notify(PSTR(" BT disconnected"), 0x80);
    };
    delay(300);
  }
  Usb.Task();
}
ekoslav commented 1 year ago

I had that issue, where the chars would drop randomly or lock up repeating character indefinitely on long block (like an e-mail address) I resolved the issue by sending chars one by one. Now I can send unlimited length, and I can control the speed of typing.

alexz006 commented 1 year ago
void Keyboard_print(String text){
  delay(200);
  if(text.length()>15){
    bleKeyboard.print(text.substring(0,15));
    text.remove(0, 15);
    return Keyboard_print(text);
  }
  bleKeyboard.print(text);
}

Keyboard_print("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.")
SirBramble commented 1 year ago

very useful! thanks

dejavu1987 commented 1 year ago

For me it is only 8 characters. Just wondering what is causing this limit. I have a similar workaround too but would be great to understand the actual cause and a fix for this.