5

I am trying to make a "keyboard" with my Arduino Mini Pro and a RN-42 HID Bluetooth module. I can connect to it with my MacBook and it shows up as a Bluetooth keyboard. Also, I can press buttons and it sends commands.

Perfect right?

Unfortunately not... I don't know how to code the bytes I need to send to the module to tell it key presses and key releases. I have been reading through every Bluetooth RN-42 manual I can get my hands on, but I don't completely understand what I should be sending to the Bluetooth module. I have been searching using Google Search the past few weeks too, and I can not find any C code that would help me. As far as I know USB keypresses are sent as arrays of hex. Is this right? If so, how would I code that?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user2238127
  • 63
  • 1
  • 5

4 Answers4

6

I have written a arduino library silverball that supports the RN42 and the HID protocols for keyboards and mice. I have a sample application that shows how to send HID raw reports through the BT module. Mine was designed to use to play game from a custom controller.

As was stated before you need to set your BT module into HID mode (send the command S~,6 from a serial connection or set GPIO11 high on starting the module). The module should be set to a keyboard HID from the factory by default but to set it to a keyboard send the command SH,0200 to the BT module and this will set it to keyboard mode.

After that you will need to simple send RAW reports to the BT and they will be formatted like this:

RN42 HID raw report format:

|start(1 byte)|length(1 byte)|descriptor(1 byte)|data(length - 1 [for the descriptor]) 

Keyboard Example:

|0xFD|9|1|modifier|0x00|code 1|code 2|code 3|code 4|code 5|code 6 

Keyboard modifier bits (sent as one byte)

bit 7  |bit 6  |bit 5    |bit 4  |bit 3  |bit 2  |bit 1    |bit 0 
rt GUI |rt alt |rt shift |rt ctrl|lt GUI |lt alt |lt shift |lt ctrl

My code can be found on github - use it for whatever you like!

jou
  • 5,824
  • 3
  • 22
  • 24
Chris
  • 96
  • 2
1

So if you truly have a RN-42 module with HID-611 firmware:

All you have to do is send ASCII strings to the serial port @115200 baud on the RN-42, and it will do the rest. So if you want to Serial1.println("Hello"); then Hello(enter) will be sent to your Mac. If this does not work then make sure that the RN-42 is not in SPP mode by pulling up GPIO11 or send commands to RN-42 and turn on HID mode.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • I can send messages, thats not the problem. I want to use it for games. And right now I can send Serial.write('a'); and it will print an 'a' every time I press that button. But since it is in a loop when I hold down a button it will tell the computer to press 'a' every few microseconds instead of actually holding down the key. If i do a while loop it will press the key once then release immediately. I need to find out the bytes to send to tell the computer that im currently pressing and holding down that key. – user2238127 Apr 28 '13 at 15:28
  • Thank you for your answer. Youre right I can send strings, and this works fine. But how do I simulate the keys being held down? Because I plan on making a game controller. The computer doesnt know I'm holding down the key, it just sees a bunch of quick key presses instead of one long key press (AKA the key being held down). – user2238127 Apr 29 '13 at 18:50
0

So if you truly have a RN-42 module with HID-611 firmware:

All you have to do is send ASCII strings to the serial port @115200 baud on the RN-42, and it will do the rest.

Peter is right, it's very easy with a RN-42 with HID preloaded, it can be bought from sparkfun standalone or even soldered in a board for easy connection.

-3

If you want the Arduino to send keyboard press messages to the computer, use Keyboard.press() for keypresses and Keyboard.release() to release. I don't have that type of Arduino or that Bluetooth module, but since it recognizes the Arduino as a keyboard, I think this should work...

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
red_eyes
  • 308
  • 1
  • 3
  • 6
  • 1
    Thanks for the input, but unfortunately that doesn't work with all Arduinos. The "Keyboard" functions only work with the Leonardo. – user2238127 Apr 28 '13 at 15:24
  • The Keyboard library for Leonardo (or Micro or Due) is for use with HID over USB. As far as I know, it does not work with a RN-42. – efunneko Aug 04 '14 at 00:24