Pages: [1]   Go Down
Author Topic: arduino as bluetooth keyboard - no scancodes sent?  (Read 1118 times)
0 Members and 1 Guest are viewing this topic.
Ohio, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am trying to use an arduino with a bluetooth HID module (bluesmirf) to control the volume on my Galaxy tab.  I have the arduino setup so that i push a small button that is wired in, and it sends ascii text to the tablet just like a keyboard would, and this works fine.  I wanted to create a custom keyboard layout file for this "bluetooth keyboard" so that when the tablet receives a keypress from the arduino over bluetooth, it would control the appropriate item, like the volume, mute, etc.  However, when using keytest to capture the incoming button press from the bluetooth module, the scancode is always 0.  Keytest is reading the key right and shows it as keycode_a (for example) when an A is sent, but the scancode for that key is always 0 instead of a unique identifier.  Oddly enough, the carriage return that the arduino automatically puts at the end of the serial.println command (command that I am using to send the button presses over bluetooth) shows up correctly as ENTER and DOES have a scancode.

I dont get it.  I must be doing something wrong but I am still new/learning so I must be missing it. I cant setup a custom keyboard layout to do what I want without having a scancode coming in for the tablet to see.

I can post my arduino code if necessary.  Any and all help is greatly appreciated.  I am so close to accomplishing what I need and its driving me insane.
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 112
Posts: 5286
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Show us your code, maybe I understand your problem better then.
Logged

Ohio, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
// test code for sending keystrokes from arduino
// to computer via HID bluetooth module
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
// initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);  
// begin serial communication at 115200 baud rate  
  Serial.begin(115200);
}
void loop() {
// read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH,
  //the LED turns on, and the line is printed via bluetooth.
  if (buttonState == HIGH) {    
    digitalWrite(ledPin, HIGH); // turn LED on:
    Serial.println("A"); // write the line via bluetooth
    delay(1000); // delay one second  
  }
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }  
    
}

So in the code above, Serial.println ("A"); sends to the tablet and I can see an A printed in a text editor.  The tablet sees that an A was pressed in the keytest app, but it shows the scancode as a 0.  Every character sent shows as 0 for some reason, except the automatic carriage return that the sketch throws in after the Serial.println.  I even tried using Serial.print instead since that doesnt throw in a carriage return, and I get the same scancode 0 with that too.  

Frustrating to say the least.  Thanks for taking the time to look at my code/project.
« Last Edit: March 06, 2013, 06:06:58 pm by traviswt » Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 112
Posts: 5286
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Edit your post and insert code tags!
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is the only part of your code that seems to relate to Bluetooth, and the relationship is only in the comment; all the code does is write the ascii character sequence 'A', '\r', '\n' to the hardware UART. It doesn't do any keyboard emulation or anything to do with scan codes.

Code:
Serial.println("A"); // write the line via bluetooth

Could you explain how this relates to scancodes and keyboard devices?
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Ohio, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Apologies, code tags are now inserted in the original post.

I assumed that this bluetooth module, which is in HID mode, would behave and be seen as a keyboard? At least thats how it was showing up on my tab originally but now that I think about it, I wonder if it changed modes somehow as I havent seen it show up as a new keyboard in a while...

I guess my basic thought was that sending an ascii A was the same as pressing the A button on a keyboard, since my bluetooth module is acting as a Human Interface Device (the module is in keyboard mode by default in its firmware).  Am I wrong in thinking that? I then assumed that sending an A as a simulate keypress should have a scancode associated with it, which can be read by the tab and interpreted based on what command I tell it to use in the keyboard layout file.  I pulled this idea from several examples that other people had completed, the only difference being that they were using actual bluetooth keyboards.
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 112
Posts: 5286
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I assumed that this bluetooth module, which is in HID mode, would behave and be seen as a keyboard?

How do you know it's in HID mode? Most of these devices default to the Serial Profile. Just because you once switched it to HID mode doesn't mean it stays there (I don't know that particular module so I might be wrong here).

Quote
I guess my basic thought was that sending an ascii A was the same as pressing the A button on a keyboard, since my bluetooth module is acting as a Human Interface Device (the module is in keyboard mode by default in its firmware).  Am I wrong in thinking that?

Yes. The scancode for an 'a' (without holding shift) on a US keyboard is 0x04, far away from it's ASCII code 0x41 which you sent. It's possible that your BT module makes some conversion but all these conversions are specific to a selected keyboard layout then. Look that up in your module's documentation or at least provide a link to it.
Logged

Ohio, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I know that at least once my module was in HID mode, because it would show on my tablet as a new keyboard input device.  However I haven't been getting that recently and I was thinking the same thing as you as I typed my reply, maybe it defaulted back to SPP.  I will have to check that this evening. I think my particular module comes default in HID mode since its sold specifically as an HID module. 

Heres the link to my device:
https://www.sparkfun.com/products/10938
Heres the link to the bluetooth manual:
http://dlnmh9ip6v2uc.cloudfront.net/datasheets/Wireless/Bluetooth/RN-HID-User%20Guide-1.1r.pdf

I read through the manual several times but there are things in there that I dont quite understand, however I didnt see anything pop out at me as the obvious way to correct the issue that I am seeing, assuming that its not an SPP configuration issue on my end. Maybe you will understand it a bit more than I did.

Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 112
Posts: 5286
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I think my particular module comes default in HID mode since its sold specifically as an HID module.

I don't think so, here is a quote from the manual:

Quote
In firmware version 6.10 and higher, the SPP profile is enabled by default

This means in your setup() routine you have to get the module into command mode (don't forget the pauses before and after the command string) and change the profile to HID.

Then I guess you have to send HID packets and not just raw scan codes. HID is used for mice, keyboards and other devices used for human interaction. The best input for that is probably found in the HID.cpp file for the USB HID feature of the Leonardo. Looks like the you have to send the packet type (2) and then the structure named KeyReport (found in USBAPI.h).
Logged

Ohio, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hmmm I saw that in the manual but I thought that I saw somewhere that it wasnt true.  Perhaps I should stop assuming haha.  Ill see if I can change the mode to HID tonight and then check my results.

I am using an arduino uno, will your info on the HID.cpp file still apply? Is there a good place online to learn about how that file works to send HID info? I am a bit new to this so Im not really understanding the process that you outlined, but Id be happy to read up on it to figure it out.
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 112
Posts: 5286
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I am using an arduino uno, will your info on the HID.cpp file still apply?

The code is for the Leonardo but you cannot use that code anyway because it's for USB and not BT. But you can get an idea about how HID works.

Quote
Is there a good place online to learn about how that file works to send HID info?

I used a completely new tool named "Google" to find the following page: http://en.wikipedia.org/wiki/Human_interface_device. There you also find a link to the HID specification. It's not beginner-friendly but you should find all the information you need.
Logged

Ohio, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

google...never wouldve thought of that.

I googled before asking that question but wasnt finding things that were aimed for someone without heavy coding knowledge.  I was more looking for a primer of sorts or any beginners knowledge but I will look into it some more.

Thanks for the replies.
Logged

Sweden
Offline Offline
Sr. Member
****
Karma: 13
Posts: 257
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You can try out my library if you got a leonardo board. I made this library to a custom board I designed.. So it's optimised for that board.. But a leonardo will work fine.. If you want to use it with a UNO you'll have to change all the "Serial1" with Serial, and hopefully it will work then (not guaranteed), but hopefully you'll learn something from it..

Here is the link @ github: https://github.com/baselsw/BPLib
Logged

Pages: [1]   Go Up
Jump to: