This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

BLE HID Keyboard: sending multiple char's does not work

I'm referring to the HID keyboard example (BLE peripherals)

The example itself is more or less working and since my goal is to send a whole character string via BLE and HID keyboard to a smartphone or PC, I started with this example and modified it.

The code itself is IMHO a bit confusing because keys_send() seems to accept a string of up to 6 characters - but its not clear that this means the BLE device produces first 6 x key press input reports and then 6 x key release input reports, which is not what we want. So I started to write my own routine, like this one:

void send_HID_keys(uint8_t nLen, uint8_t* pKeys)
{
   uint8_t  i = 0;
   uint8_t  nKey;
   ret_code_t err_code;
   
   uint8_t  keyRep[INPUT_REPORT_KEYS_MAX_LEN];
   memset(keyRep, 0, INPUT_REPORT_KEYS_MAX_LEN);

   for (i=0; i<nLen; i++)
   {
      nKey = pKeys[i];

      keyRep[0] = 0;       // no modifiers Ctrl,Shift,Alt,GUI
      keyRep[1] = 0;       // reserved
      keyRep[2] = nKey;    // scancode of key (press)
         
      err_code = ble_hids_inp_rep_send(&m_hids,
                                  INPUT_REPORT_KEYS_INDEX,
                                  INPUT_REPORT_KEYS_MAX_LEN,
                                  keyRep,
                                  m_conn_handle);
      app_sched_execute();
      nrf_delay_ms(10);

      keyRep[2] = 0;       // release key
         
      err_code = ble_hids_inp_rep_send(&m_hids,
                                  INPUT_REPORT_KEYS_INDEX,
                                  INPUT_REPORT_KEYS_MAX_LEN,
                                  keyRep,
                                  m_conn_handle);
      app_sched_execute();
      nrf_delay_ms(10);

   }
}

The device based on nRF52832 is successfully paired and bonded to my Android Smartphone, I'm using the Jota text editor to check the HID input.

Each and every string I'm trying to send with this routine is not properly transmitted: something will always come through, but most common some characters are lost.

For example, I'm trying to send the string "103.23" (just numbers and a dot) and see "1.23" or "1109.23" etc...

I played aroung with the nrf_delay_ms() and the app_sched_execute() call is also seomthing I tried.
Without the app_sched_execute() the behaviour is much worse, usually leadint to an NO MEMORY exception after 2-3 tries.

The Nordic SDK documentation does not clearly explain how to use the ble_hids_inp_rep_send - at least I cant find a deeper explantion, such as is there any backgroiund thing needed, event handler after each input report or whatever.

It should be easy to reproduce this behaviour and I'm looking forward to any helpful input

thanks, Matthias

  • Hi Matthias,

    If your goal is to send a string of characters I think you should instead be looking at the ble_app_uart example instead of the ble_app_hids_keyboard example. The function ble_hids_inp_rep_send() is mean to be use to send an update on which keyboard button that is being press right now, not for string of characters. A keyboard will for example never send the same button press simultaneously from a keyboard, while in your example you try to send the same character in a string multiple times.

    keys_send() seems to accept a string of up to 6 characters

    When using boot descriptor for a keyboard you are only allowed to send 6 characters at a time.

    Best regards,

    Marjeris

  • Hi Marjeris

    thanks for the advice, my experience sofar with HID keyboard is pretty bad: even the example code does not work reliably with Android devices. With Win7, there is no support for a generic BLE keyboard, and even with Win10 it does not work properly - and I was not able to even do a pairing/bonding with IOS devices.

    In my project, I have an industrial measuring device and we thought the easiest proof of concept would be to simply act like a keyboard and send the data via BLE right into Excel or whatever, but I guess this will not work.

    What I don't understand: you (and some other people) say "a keyboard will never send the same button press..." - which is wrong, as far as I can say. When I attempt to type the Word "google" on a keyboard, I'm typing twice the letter "o" (of course like: press - release - press - release) and I expect to see on the screen "google" and not "gogle" because some stupid software thinks this is a typo - and my code behaves like this. I create an input key-press report for each button, followed by a key-release report, so its exactly like typing on a real keyboard.

    When going for uart-over-BLE, can you please help me to find a virtual com port driver ? For Android devices, I'm currently using the "nrf UART" which is ok so far - but for Windows (I need to support Win7 and Win10) I need a vurtual com port driver so my software can receive the incoming data via BLE.

    thank, regards, Matthias

  • MDF said:
    When going for uart-over-BLE, can you please help me to find a virtual com port driver ? For Android devices, I'm currently using the "nrf UART" which is ok so far - but for Windows (I need to support Win7 and Win10) I need a vurtual com port driver so my software can receive the incoming data via BLE.

     Win7 does not support native BLE. I suggest using a nRF52 dongle or DK, that can act as the BLE central.

    If you use something like a nRF52840 dongle or DK, that have built-in/on-chip USB support, then you want something that combines the CDC ACM example with the ble_app_uart_c example, see this post: https://devzone.nordicsemi.com/f/nordic-q-a/53649/need-a-simple-terminal-program-to-communicate-with-ble-uart-service/217898#217898

    If you use e.g. a nRF52832 DK(that don't have on-chip USB), you would need to use the on-board Segger J-link chip for the Uart-to-USB functionality, and you can then use the ble_app_uart_c example in the nRF5 SDK.

Related