ESP32-BLE-Keyboard icon indicating copy to clipboard operation
ESP32-BLE-Keyboard copied to clipboard

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

Open aopenna opened this issue 2 years ago • 4 comments

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();
}

aopenna avatar Jun 24 '22 21:06 aopenna

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.

ekoslav avatar Jul 13 '22 07:07 ekoslav

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.")

alexz006 avatar Jan 31 '23 10:01 alexz006

very useful! thanks

SirBramble avatar Feb 21 '23 02:02 SirBramble

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.

dejavu1987 avatar Apr 18 '23 15:04 dejavu1987