Home > Arduino, Bluetooth, TKJ Electronics, USB > Bluetooth HID devices now supported by the USB Host library

Bluetooth HID devices now supported by the USB Host library

I am glad to announce that Bluetooth HID devices are now supported by the USB Host library. The library already supports PS3 and Wiimote controllers, but now it also supports more general devices, like Bluetooth keyboard and mice.

An example is available at the following link: https://github.com/felis/USB_Host_Shield_2.0/blob/master/examples/Bluetooth/BTHID/BTHID.ino.

First time you run the code, you will have to pair with your device, this is done by creating the instance like so:

BTHID bthid(&Btd, PAIR, "0000");

You can of course set the pin to anything you like.

Now you should enable discovery of your device and it should automatically detect any mouse or keyboard present and then connect to them. On Bluetooth mice there is no need to enter any pin, but on a keyboard you should enter the pin on the keyboard and then afterwards press enter.

So in this example you should press 0 four times on the keyboard and then press enter afterwards.

After you have paired with the device, you can simply create the instance like so:

BTHID bthid(&Btd);

When you press any button on your device it will automatically connect to the last paired device i.e. your Arduino.

By default it uses the “Boot Protocol Mode”, but since only basic functionality is available in “Boot Protocol Mode”, I have made it easy to change it.
Simply call this in your setup:

bthid.setProtocolMode(HID_RPT_PROTOCOL);

Since it do not read the HID descriptor of your device, you might find that some of the buttons etc. might not work on your device. Unfortunately you will have to modify the parser, but it should not be that difficult.

Simply uncomment:

#define PRINTREPORT

In BTHID.cpp to see the incoming data and then modify the parser for your needs – the standard parser can be found in hidboot.cpp.

I have currently tested the code with a PS3 keyboard (see image below) and it works perfectly fine, but I had to change it to use “Report Protocol Mode” as described above. A nice thing about the PS3 keyboard is that it features both a keyboard and a mouse, as the keyboard is touch sensitive and can therefore be used as a trackpad.

Furthermore I have tested it with my Apple Wireless Keyboard, Apple Wireless Trackpad and an old Bluetooth mouse from Microsoft that I have and all of them worked fine as well.

ps3_controller_keyboard_01

This is all for now. Fell free to leave a comment below if you have any questions or problems with a specific device.

Categories: Arduino, Bluetooth, TKJ Electronics, USB Tags:
  1. Kapil Saraswat
    December 27th, 2013 at 19:58 | #1

    Glade to hear. I want to know how I connect my android mobile phone with arduino mega adk via bluetooth? Any example?
    Regards

  2. December 27th, 2013 at 20:33 | #2

    @Kapil Saraswat
    First of all you should activate the mega ADK at explained here: https://github.com/felis/USB_Host_Shield_2.0#boards.

    Then upload the following code to the Arduino: https://github.com/felis/USB_Host_Shield_2.0/blob/master/examples/Bluetooth/SPP/SPP.ino. After that install the following application on your Android phone: https://play.google.com/store/apps/details?id=es.pymasde.blueterm.

    If you want to control a robot or something like that, then take a look at the Android app for the Balanduino: https://github.com/TKJElectronics/BalanduinoAndroidApp.

  3. ken
    January 10th, 2014 at 09:33 | #3

    Hi,
    PS3BT is too large for my board Atmega328 Duemilanove. Is this normal?
    If I need to buy a bigger size board, can I get Arduino MEGA 2560 R3?
    or it has to be first revision of Arduino MEGA 2560?

    thanks!

  4. January 10th, 2014 at 20:00 | #4

    @ken
    You can overcome this by simply removing some of the code from the example.
    You can start out by removing this: https://github.com/felis/USB_Host_Shield_2.0/blob/c11ca6c434a4482f60feac1df33a37368116dbd1/examples/Bluetooth/PS3BT/PS3BT.ino#L114 and this: https://github.com/felis/USB_Host_Shield_2.0/blob/c11ca6c434a4482f60feac1df33a37368116dbd1/examples/Bluetooth/PS3BT/PS3BT.ino#L128-L179. Then you should be able to compile.

    You can use any Arduino AVR board you like – they are all supported. You can find more information here: https://github.com/felis/USB_Host_Shield_2.0#boards.

  5. Andy
    January 17th, 2014 at 17:15 | #5

    Firstly, thank you for your work.

    Second, are your libraries compatible and do you offer support for the iTead USB Host Shield? I’ve purchased this board and they direct me to here as they no longer offer support for their board.

    Basically I have downloaded the library and I am having issues connecting the PS3 controller. I’m troubleshooting now but just wanted to first make sure your library is compatible, I’m using the shield with the Arduino UNO R3 board with DIP ATmega328P. Or do I need to mod the iTead USB shield some way. I can provide more information if needed.

    Thank you

  6. January 17th, 2014 at 20:12 | #6

    @Andy
    I don’t support the iTead shield – you should get help from iTead instead.
    You can take a look at the official schematic: http://www.circuitsathome.com/wp/wp-content/uploads/2011/02/UHS_20.pdf and compare it to the iTead shield: ftp://imall.iteadstudio.com/IM120417019_Arduino_USB_Host_Shield/SCH_IM120417019_ArduinoUSBHostShield.pdf.
    I just had a quick look and it looks like it will work without modifications.

  7. Michael Jacobsen
    March 28th, 2014 at 08:15 | #7

    Hi, Great Work!

    On a PC with CentOS and the ‘bluez’ libraries installed, I have a program that can ping Bluetooth addresses:

    static void ping(char *svr)
    {
    struct sigaction sa;
    struct sockaddr_l2 addr;
    socklen_t optlen;
    unsigned char *buf;
    char str[18];
    int i, sk, lost;
    uint8_t id;

    memset(&sa, 0, sizeof(sa));
    sa.sa_handler = stat;
    sigaction(SIGINT, &sa, NULL);

    buf = malloc(L2CAP_CMD_HDR_SIZE + size);
    if (!buf) {
    perror(“Can’t allocate buffer”);
    exit(1);
    }

    /* Create socket */
    sk = socket(PF_BLUETOOTH, SOCK_RAW, BTPROTO_L2CAP);
    if (sk < 0) {
    perror("Can't create socket");
    free(buf);
    exit(1);
    }

    /* Bind to local address */
    memset(&addr, 0, sizeof(addr));
    addr.l2_family = AF_BLUETOOTH;
    bacpy(&addr.l2_bdaddr, &bdaddr);

    if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
    perror("Can't bind socket");
    close(sk);
    free(buf);
    exit(1);
    }

    /* Connect to remote device */
    memset(&addr, 0, sizeof(addr));
    addr.l2_family = AF_BLUETOOTH;
    str2ba(svr, &addr.l2_bdaddr);

    if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
    perror("Can't connect");
    close(sk);
    free(buf);
    exit(1);
    }

    /* Get local address */
    memset(&addr, 0, sizeof(addr));
    optlen = sizeof(addr);

    if (getsockname(sk, (struct sockaddr *) &addr, &optlen) < 0) {
    perror("Can't get local address");
    close(sk);
    free(buf);
    exit(1);
    }

    ba2str(&addr.l2_bdaddr, str);
    printf("Ping: %s from %s (data size %d) …n", svr, str, size);

    /* Initialize buffer */
    for (i = 0; i 0) {
    struct timeval tv_send, tv_recv, tv_diff;
    l2cap_cmd_hdr *cmd = (l2cap_cmd_hdr *) buf;

    /* Build command header */
    cmd->ident = id;
    cmd->len = htobs(size);

    if (reverse)
    cmd->code = L2CAP_ECHO_RSP;
    else
    cmd->code = L2CAP_ECHO_REQ;

    gettimeofday(&tv_send, NULL);

    /* Send Echo Command */
    if (send(sk, buf, L2CAP_CMD_HDR_SIZE + size, 0) <= 0) {
    perror("Send failed");
    exit(1);
    }

    /* Wait for Echo Response */
    lost = 0;
    while (1) {
    struct pollfd pf[1];
    int err;

    pf[0].fd = sk;
    pf[0].events = POLLIN;

    if ((err = poll(pf, 1, timeout * 1000)) < 0) {
    perror("Poll failed");
    exit(1);
    }

    if (!err) {
    lost = 1;
    break;
    }

    if ((err = recv(sk, buf, L2CAP_CMD_HDR_SIZE + size, 0)) len = btohs(cmd->len);

    /* Check for our id */
    if (cmd->ident != id)
    continue;

    /* Check type */
    if (!reverse && cmd->code == L2CAP_ECHO_RSP)
    break;

    if (cmd->code == L2CAP_COMMAND_REJ) {
    printf(“Peer doesn’t support Echo packetsn”);
    exit(1);
    }

    }
    sent_pkt++;

    if (!lost) {
    recv_pkt++;

    gettimeofday(&tv_recv, NULL);
    timersub(&tv_recv, &tv_send, &tv_diff);

    printf(“%d bytes from %s id %d time %.2fmsn”, cmd->len, svr, id – ident, tv2fl(tv_diff));

    if (delay)
    sleep(delay);
    } else {
    printf(“no response from %s: id %dn”, svr, id – ident);
    }

    if (++id > 254)
    id = ident;
    }
    stat(0);
    }

    Do you think L2CAP_ECHO_REQ, L2CAP_ECHO_RSP, L2CAP_COMMAND_REJ can be implemented in the BTD library?

    /michael

  8. March 29th, 2014 at 12:28 | #8

    @Michael Jacobsen
    Why exactly do you want this capability? I might add it in the future, but to be honest it has very low priority.

  9. Bocar Ndiaye
    March 29th, 2014 at 12:40 | #9

    Hi.
    my aruino leonardo is already HID, how can I use leonardo keyboard and mouse sample code with a bleutooth dongle and usb hostshield and the bleutooth connection part of your code.
    thanks for any pointer.
    Bocar

  10. March 29th, 2014 at 13:09 | #10

    @Bocar Ndiaye
    You need the USB Host shield in order to use this code: http://shop.tkjelectronics.dk/product_info.php?products_id=43.

    Sorry but I think you misunderstood this blog post. This blog post is about how you can connect a Bluetooth HID device to your Arduino.

  11. Bocar Ndiaye
    March 29th, 2014 at 13:38 | #11

    Got it now. Thanks for the clarification.
    Bocar

  12. Michael Jacobsen
    March 31st, 2014 at 08:11 | #12

    A simple presence detector – I already use it (on the PC) to detect if any of our phones are present. If none are present, I turn off all light…

    For the Arduino, I would like to move this functionality out to the drive-way so I can turn on lights out there if it’s dark and one of our phones are detected (by constantly ping’ing them).

    /michael

    @Lauszus

  13. March 31st, 2014 at 09:20 | #13

    @Michael Jacobsen
    Okay. You need to use the following command: https://github.com/felis/USB_Host_Shield_2.0/blob/b3b476af331e17f94ba6203eefd00869720a7478/BTD.cpp#L1020-L1032 and then check if it sees a device here: https://github.com/felis/USB_Host_Shield_2.0/blob/b3b476af331e17f94ba6203eefd00869720a7478/BTD.cpp#L430.

    You can just check if the number of responses it above zero, as you properly don’t care what type of device it is?

  14. Michael Jacobsen
    April 2nd, 2014 at 07:43 | #14

    @Lauszus
    Great! Thanks, I’ll give it a go. And yes, I don’t care about device type … πŸ™‚

    /michael

  15. NicoHood
    April 27th, 2014 at 13:38 | #15

    Hi!
    I think i finally found what i was searching for. You are my man. Ive got a few questions, it would be nice if you could answer me πŸ™‚

    My goal was to connect my Wiiremote or Wiiu Pro Controller to my Arduino. I searched through alsmost every page that still exists (a lot of information got deleted). So well, we all know that youve got a solution with the shield, i will come to that later.

    I bought a HC-05 Bluetooth master. Not sure if you know this one. It can communicate as slave and as master with devices. I got it connected to my pc as a com port and also to my old k610 phone. But i wanted to connect it to my wiiremote (the old one). I got a respond from the remote with the address and the name. But pairing or linking didnt work. I am nor sure if i am doing something wrong or if the device just doesnt support HID usage. As far as i read i need to connect my remote to my arduino as hid. A question to that: is there a special master or slave role for hids? I found some other devices: the WT12 and the RN-42. Can you tell me if i can use them? I wanted to built something without usb if possible. I just have no idea how to connect them together. the actual communication with getting the buttons will be less complicated (i hope. at least the connection is there). If you have some information about that it would be nice to share it with me. Maybe i am just searching for the wrong keywords and you might know what page i could look up for good bluetooth information. If you can tell me how to link with the remote that would be nice.

    Well you might have no idea about the devices i want to use and cant help me with that. But you probably can help me with the usb host shield. If its the only solution (at least the only working solution i found yet) i will go this way. Ive got a arduino uno, mega, pro mini and pro micros. As far as i read the shield uses spi and two other pins of choice, is that right?

    I am wondering: Is there any difference between this shield (linked here) and the cheaper once from aliexpress? Will they also work? I would like to support the developers of that library if everything works fine too, but i am not sure if you have any profit from buying the original shield. Not sure if its worth spending that much money on the original shield, also because of shipping. I would rather buy it somewhere else and donate money here. So what is the different between the other shields?

    I also read that the mega adk is supported. I am not sure if its worth it to buy a mega adk just for this. It just isnt that fat with the shield, but its very expensive. Can I connect my Pro Controller there the same way, right? How slow/fast is the usb shield? Do i have time for other complex operations? Because a mega with a lot of ram but slow processor is no solution, then I would just connect more arduinos via uart or i2c.

    And one last question: Do you know if its possible to switch roles? Like emulating a pro controller and send information to the wii/wiiu and controll my games like that?
    Thanks for any help ?
    Nico

  16. April 27th, 2014 at 16:02 | #16

    @NicoHood
    No you can not use the HC-05 Bluetooth module, as it doesn’t support Bluetooth HID. You will have to use the USB Host library.

    As far as i read the shield uses spi and two other pins of choice, is that right?

    Yes that is correct. It uses SCK, MISO, MOSI, SS and a pins for INT.

    I am wondering: Is there any difference between this shield (linked here) and the cheaper once from aliexpress? Will they also work? I would like to support the developers of that library if everything works fine too, but i am not sure if you have any profit from buying the original shield. Not sure if its worth spending that much money on the original shield, also because of shipping. I would rather buy it somewhere else and donate money here. So what is the different between the other shields?

    If you want to support me, then please buy it our shop: http://shop.tkjelectronics.dk/product_info.php?products_id=43, the cheap shield are just a copy of the official shield and often there a several flaws with them.

    I also read that the mega adk is supported. I am not sure if its worth it to buy a mega adk just for this. It just isnt that fat with the shield, but its very expensive. Can I connect my Pro Controller there the same way, right? How slow/fast is the usb shield? Do i have time for other complex operations? Because a mega with a lot of ram but slow processor is no solution, then I would just connect more arduinos via uart or i2c.

    If you write your code properly, then you can still do a lot of complex tasks.
    For instance the Balanduino uses the USB Host library, but still keeps standing upright.

    And one last question: Do you know if its possible to switch roles? Like emulating a pro controller and send information to the wii/wiiu and controll my games like that?

    Yes it’s possible, but no something I have implemented, so you will have to write the code yourself.

  17. NicoHood
    April 28th, 2014 at 12:46 | #17

    Thanks for your reply πŸ™‚

    I just downloaded the Library and compiled the WiiuPro sketch. around 30kb is a lot, for only the basic usage. Is there a way to short this down? The library seems very komplex to me, i have no idea how hard it would be to implement my own solution. Because 30kb means for me i only can use the mega for usb projects, because i want to built other stuff around in my sketch. The goal is to get it working as a usb joystick on my pc, but then i have to get it under 28kb on a leonardo, and then that is everything i can do with that leonardo, no other functions. I made the expirience with the ir library that writing my own takes less flash, ram and is more relyable because i only support one device. In my case a wiiu pro controler. So is there stuff in the library that i could possibly short/reprogram and how hard is that to write the usb control?

    Lets say i want to read the controller every so often to use it as a pc controller. Then i have to send the information via Serial to another arduino that supports HID keyboards (leonardo) because 30kb is too much for a leonardo. But how fast is the access for a single bluetooth commant to get the new button states? Can i built a lot of stuff around or does the usb controll take a lot of time. Any benchmark for this?

    Is the mini version arduino pro micro compatibel? Because if its pro micro compatible and i can get the sketch under 28kb i would use the mini version. if the usb control is that processor expensive then i would also use a mini with a nano because i cant do anything else and a 328 is enough. But if there is no way to get it working under 28kb and the prcessor load is not that big i will use it with a mega. or probably buy an adk but i think the shield would be the better choose.

    You said its not possible to emulate a wiiremote, but is it possible to emulate normal keyboards and receive the pro controller at the same time? then i could built a bridge between my pc that converts the signals to a normal bluetooth controller.

    Thanks πŸ™‚

  18. April 28th, 2014 at 14:53 | #18

    @NicoHood
    I know that the Arduino team is working on updating the AVR-GCC toolchain. It should be updated from Arduino 1.5.6.

    For now you can download it from one of the following links depending on your OS:
    http://downloads.arduino.cc/arduino-avr-toolchain-nightly-gcc-4.8.1-windows.zip
    http://downloads.arduino.cc/arduino-avr-toolchain-nightly-gcc-4.8.1-macosx.zip
    http://downloads.arduino.cc/arduino-avr-toolchain-nightly-gcc-4.8.1-linux32.tgz
    http://downloads.arduino.cc/arduino-avr-toolchain-nightly-gcc-4.8.1-linux64.tgz

    I just tried to compile the “WiiUProController” example for the Uno and the flash was reduced from 29,308 bytes to 26,270 bytes.

    You could reduce the code size removing all the content from: https://github.com/felis/USB_Host_Shield_2.0/blob/master/Wii.cpp that is related to the Wiimote and Nunchuck.

    Yes you could also use the Pro Mini 3.3V 8MHz version together with the mini version of the shield – you could update the bootloader on the Pro Mini, so it only takes up 512 bytes instead of 2048 bytes.
    Then you should be able to get everything to fit on the Pro Mini if you use the updated toolchain.
    Yes the mini version of the shield is not pro micro compatible, but you will need to add your own logic level shifters.

    Have you thought about using a Sanguino: http://sanguino.cc/? I have written a hardware plugin for it as well: https://github.com/Lauszus/Sanguino since the Balanduino uses the same microcontroller.

    The update rate for the controller is approximately 1ms (1kHz) if you write your code efficient.

    You said its not possible to emulate a wiiremote, but is it possible to emulate normal keyboards and receive the pro controller at the same time? then i could built a bridge between my pc that converts the signals to a normal bluetooth controller.

    I never said that it wasn’t possible (everything is possible πŸ™‚ ), but it’s not something I will work on.
    Yes you should be able to both read the Wii U Pro controller and send keyboard events.

  19. NicoHood
    April 28th, 2014 at 17:12 | #19

    I need to get this working with my arduino pro micro/leonardo, this would be so awesome. I tried to compile it with the linked version and if i remove the Serial output i can get 28.620/28.672 kb (on a leonardo). Not much space to work with. I am wondering how you can blow up such a big library. I am not sure if removing stuff from the cpp would help that much because the compiler only compiles thes needed files. and the new version is even more clever it seems. I havent programmed the whole thing and havent looked through the code in detail. But maybe there are possibles to improve the code is this this usb thing just that complex?

    Oh and by the way: sparkfun sells pro micros with 3.3v and it seems that this would work then. its just expensive as hell and the sketch limit is reached very fast. If i have to use a mega and send the information to the pro micro which acts as usb keyboard then i could rather just try it with my raspberry.

    Where exactly did you get the latest arduino ide? The one i downloaded from the website (nightly) totally failed and compiled the sketch with 32kb.

  20. April 28th, 2014 at 21:10 | #20

    @NicoHood
    The only place where you can easily remove some code is in Wii.cpp! Simply remove all the code related to the Wiimote and Nunchuck as I told you in my previous comment.
    Due to the way the library is written the library doesn’t know which one is going to connect. This allows you to use the same library for all the Wii controllers.
    It might be better to split them up at some point, but I haven’t got time for that at the moment.

    Yes the USB and Bluetooth protocols are quite complex, so it’s not that easy to shrink it down.

    Have you considered using the one of the Teensy boards? They are all supported by the library and they can be used as a Keyboard as well – or even a Joystick, Mouse, MIDI, External disk and flight simulator!

    It sounds very odd that it was that large!
    I just tried to compile the entire “WiiUProController” example for the Leonardo and that ended up at 28,898 bytes.
    Are you familiar with Git and have you ever compiled the Arduino IDE before?

  21. NicoHood
    April 28th, 2014 at 22:29 | #21

    as i said: i removed all serial functions and i think i added just a led functions instead. without any modification i get the same size. but thats too much for the leonardo.

    Splitting them seems a nice idea, but if you dont have time, i will have to do it on my own, no problem πŸ˜‰

    using the teensy is a very nice idea. have to think about that. If i want to use it with a 5v arduino, do i have to use special logic level shifters or can i use simple resistors for all pins?

    Have you tried bluetooth with 3.3v, because it sais there can be problems for some devices?

    i used git to upload my own libraries but only for windows straight forward. and no i never compiled the ide before, because i never needed to do that. just asking where you got that link from so i can check out new updates too.

    Thanks so much for all your help and sorry for that many questions :S

  22. May 3rd, 2014 at 13:25 | #22

    @NicoHood
    Normally you would use mosfets for logic level conversion or even better you a dedicated IC, as there is on the 5V version of the shield. Take a look at the schematic: http://www.circuitsathome.com/wp/wp-content/uploads/2011/02/UHS_20.pdf.

    Yes I have. Some of the dongles I have works fine when VBUS is only 3.3V others doesn’t.

    If you are interrested in the development of the Arduino IDE then you should join the mailing list: https://groups.google.com/a/arduino.cc/forum/?fromgroups#!forum/developers, but please don’t ask any questions about Arduino, as it’s intended for development of the IDE!

    It would also be a good idea to “Watch” the Github repository: https://github.com/arduino/Arduino.

  23. September 5th, 2014 at 07:49 | #23

    Hey! I know this is kinda off topic but I was wondering
    which blog platform are you using for this site? I’m getting fed up of WordPress because I’ve had problems with hackers and I’m looking at options for another platform.
    I would be great if you could point me in the direction of a good platform.

  24. September 8th, 2014 at 06:20 | #24

    @cabinets
    We use WordPress as well with the plugin “Akismet” which catches most spammers!

  25. Mike
    May 25th, 2015 at 22:01 | #25

    Hi
    Thanks for the usb library it is very helpful, however I have the following question:
    I have a code that is written for a USB TV tuner using C, and I am wondering whether I can replicate the code on arduino (via using the core usb functions from your code).
    The original C code utilizes the libusb library to move data to and from the device, however the libusb functions have this form:

    libusb_get_device_descriptor
    libusb_free_device_list
    libusb_init
    libusb_control_transfer
    and so on (basically they are doing the core usb functions).

    Basically I am thinking of using the raw functions of your code (which set the device descriptors, do the interrupts, control transfer and so on) to replicate the code which I have in C for the original device driver. Am I thinking correctly about the problem and do you see any problems with this approach?

    Best Regards.
    Mike

  26. May 28th, 2015 at 10:13 | #26

    @Mike
    I have answered you email πŸ™‚

  27. Helge
    July 5th, 2015 at 20:43 | #27

    Hi, Thanks for this awsome library.

    I am trying to connect PS3 Controller and PS3 Keypad at the same time, but am unable to do so.
    I have successfully connected each device seperatly, but when I try to merge the two sketches I get the PS3 controller to connect, but when I turn on the keypad the debug show it is trying to connect but it is periodicly cucling through withous success.

    Is this even possible what I am trying to achieve??

    Debug serial printout :

    Incoming Connection Request
    Remote Name: Wireless Keypad
    Connected to Device: 04:76:6E:72:07:AD
    Wait For Incoming Connection Request
    LeftHatX: 0 LeftHatY: 0 RightHatX: 0 RightHatY: 0
    LeftHatX: 0 LeftHatY: 0 RightHatX: 0 RightHatY: 0
    LeftHatX: 0 LeftHatY: 0 RightHatX: 0 RightHatY: 0
    HCI Disconnected from Device
    Wait For Incoming Connection Request
    Keyboard is connecting
    Incoming Connection Request
    Remote Name: Wireless Keypad
    Connected to Device: 04:76:6E:72:07:AD
    Wait For Incoming Connection Request
    LeftHatX: 0 LeftHatY: 0 RightHatX: 0 RightHatY: 0
    LeftHatX: 0 LeftHatY: 0 RightHatX: 0 RightHatY: 0
    LeftHatX: 0 LeftHatY: 0 RightHatX: 0 RightHatY: 0
    HCI Disconnected from Device
    Wait For Incoming Connection Request
    Keyboard is connecting
    Incoming Connection Request
    Remote Name: Wireless Keypad
    Connected to Device: 04:76:6E:72:07:AD
    Wait For Incoming Connection Request

  28. July 5th, 2015 at 20:49 | #28

    @Helge
    First of all it looks like you need another dongle in order to use the joysticks on the PS3 controller: https://github.com/felis/USB_Host_Shield_2.0#faq. You can find a list of dongles here: https://github.com/felis/USB_Host_Shield_2.0/wiki/Bluetooth-dongles.

    Also have you created two instances? One for the PS3 controller and one for the PS3 keypad?

  29. Helge
    July 5th, 2015 at 23:14 | #29

    @Kristian Lauszus
    Hi Kristian. Thanks for the fast reply.
    My dongle works corectly, but I was having trouble with the two instaces, I am not very experianced in OOP so I had to twist my brain quite hard to get it working corectly.

    Perhaps you can add this to the library example sketches (after you rewrite it to your good standard.) that is.

    I am adding the working sketch to this post. Note: Press the PS3 power button first, then the keypad. and at least on my computer the PS3 and Keypad are working simultaneously.

    #include <PS3BT.h>
    #include <usbhub.h>
    #include <BTHID.h>
    #include "KeyboardParser.h"
    #include "MouseParser.h"
    #ifdef dobogusinclude
    #include <spi4teensy3.h>
    #include <SPI.h>
    #endif

    USB Usb;
    BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
    PS3BT PS3(&Btd); // This will just create the instance
    BTHID bthid(&Btd, PAIR, "0000");
    BTHID hid(&Btd);
    KbdRptParser keyboardPrs;
    MouseRptParser mousePrs;

    void setup() {
      Serial.begin(115200);
    #if !defined(__MIPSEL__)
      while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
    #endif
      if (Usb.Init() == -1) {
        Serial.print(F("rnOSC did not start"));
        while (1); //halt
      }
      bthid.SetReportParser(KEYBOARD_PARSER_ID, (HIDReportParser*)&keyboardPrs);
      bthid.SetReportParser(MOUSE_PARSER_ID, (HIDReportParser*)&mousePrs);
      bthid.setProtocolMode(HID_RPT_PROTOCOL); // Report Protocol Mode
      Serial.print(F("rnPS3 Bluetooth Library Started"));
    }
    void loop() {
      Usb.Task();

    if (PS3.getAnalogButton(L2) || PS3.getAnalogButton(R2)) {
      Serial.print(F("rnL2: "));
      Serial.print(PS3.getAnalogButton(L2));
      if (PS3.PS3Connected) {
         Serial.print(F("tR2: "));
         Serial.print(PS3.getAnalogButton(R2));
         }
      }
    if (PS3.getButtonClick(PS)) {
      Serial.print(F("rnPS"));
      PS3.disconnect();
      }
    }
  30. July 5th, 2015 at 23:34 | #30

    @Helge
    You can just remove the instance “BTHID hid(&Btd);” also you should check if a PS3 controller is connected before accessing any of the functions:

    if (PS3.PS3Connected) {
      // Your code in here
    }

    Btw I have edited you comment so it shows up correctly. Please embed your code like so:
    < code lang="cpp" >
    // Your code
    < /code >

    (Just remove the spaces)

  31. MartinJ. Brockhus
    July 26th, 2015 at 15:57 | #31

    Hallo Kristian;

    An impressive collection of hardware support section and thank you for sharing all this.

    I have a small BT gamepad controller working, with #define PRINTREPORT it generates output to serial from all keys. Without #define PRINTREPORT only one key generates something like LefthGUIChanged etc, all the others remain dead. You wrote”Unfortunately you will have to modify the parser, but it should not be that difficult.”That seems not so simple, I have tried to follow your code via usb.task etc but lost track everytime.

    I just need to collect or extract the third byte from the output of PRINTREPORT to control my autohelm project. Can you hint me how to get these pressed key values on the interrupt as simple as possible into a variable in stead of usb.task()??

    Thanks on beforehand.

    Martin J.

    Martin

  32. July 27th, 2015 at 13:24 | #32

    @MartinJ. Brockhus
    You should check out the following example: https://github.com/felis/USB_Host_Shield_2.0/tree/master/examples/HID/USBHIDJoystick. You will basically just need to edit the parser.

  33. MartinJ. Brockhus
    July 27th, 2015 at 15:21 | #33

    Hmmm?
    I have to use BTHID.ino to pair, I can see 1 key being recognised by the parser.
    When I enable PRINTREPORT I can see HEX codes for each button and POV switch.
    When paired I load USBHIDJoystick and Serial monitor yields:
    Start
    HU Init
    Addr:01
    NC:01
    and nothing more than that.

    How is the output of the parser supposed to become avialable for the normal program code?

    It seems that I have to run BTHID each time just to pair and connect again. I need the bluetooth controller however to be connected back automatically when arduino is repowered, is feasable or am I looking for the impossible?

    Thanks in advance.

    Martin

  34. MartinJ. Brockhus
    July 27th, 2015 at 16:19 | #34

    I may be mistaken but it appears that BTHID.ino is waiting for the serial monitor to connect. When debug and printreport are not enabled nothing happens and arduino is not connecting to the controller. When both are enable it does. When the Arduino is not connected to IDE nothing happens. When the controller is disconnected en repowered again it it not automatically connected again so the sketch must be restarted on the Arduino.

    What is the idead behind usb.task()? I mean where is the input from the usb dongle in the shield being directed to?

    I am messing around with small remotes like this http://www.google.nl/imgres?imgurl=http://g01.s.alicdn.com/kf/HTB1XDQiGFXXXXc8XXXXq6xXFXXXc/220768105/HTB1XDQiGFXXXXc8XXXXq6xXFXXXc.jpg&imgrefurl=http://www.alibaba.com/product-detail/Android-IOS-PC-3-in-1_60097175733.html&h=333&w=652&tbnid=tK1EnKEZyUGZaM:&docid=QkWLJSDT6jcChM&itg=1&ei=5ku2VaPHCOzD7gaNnY6IBQ&tbm=isch&ved=0CCYQMygGMAZqFQoTCKPSgf3N-8YCFeyh2wodjY4DUQ

  35. July 27th, 2015 at 16:53 | #35

    @MartinJ. Brockhus
    Sorry I answered you question to quickly. I thought you were using a usb joystick. You should take a look at the following example instead: https://github.com/felis/USB_Host_Shield_2.0/tree/master/examples/Bluetooth/BTHID. You can just adjust the keyboard parser for your needs.

    If you want to write a proper driver you should take a look at the PS4 parser which inherits the BTHID class: https://github.com/felis/USB_Host_Shield_2.0/blob/master/PS4Parser.h and https://github.com/felis/USB_Host_Shield_2.0/blob/master/PS4Parser.cpp.

    If you don’t want to wait for the serial monitor to connect you can simply remove the line:

    while (!Serial);
  36. MartinJ. Brockhus
    July 28th, 2015 at 20:11 | #36

    Dear Kristian;

    Thank you for your prompt reaction.
    I can have the cotroller being detected and paired with BTHID.
    Following you suggestion I will try to use PS4Parser code.
    I am still wondering how to pick up the output from BT->USB into the sketch code.
    I will keep you posted.

    Kind regards

    Martin

  37. July 28th, 2015 at 22:44 | #37

    @MartinJ. Brockhus
    One easy approach might be to just make the struct you are reading the data into public, so you can access it in your sketch. In the PS4Parser class it would be the “ps4Data” variable.

  38. MartinJ. Brockhus
    July 29th, 2015 at 14:42 | #38

    Dear Kristian;
    Sorry,

    Change BTHID to work with ps4parser instead of keyboardparser,
    then ps4Data is the variable to find the BT control keys in?
    Is that what you mean?

    kind regards
    Martin

  39. July 29th, 2015 at 14:48 | #39

    @MartinJ. Brockhus
    No just modify the PS4Parser so the variable “ps4Data” is public. I believe this will be easier for you than modifying the keyboardparser.

    It might be even easier to uncomment “PRINTREPORT” in: https://github.com/felis/USB_Host_Shield_2.0/blob/master/PS4Parser.cpp and then just save the value of a specific location in the buffer in a public variable.

    Btw you should properly change this line:

    if (len > 1 && buf)  {

    To:

    if (len > 0 && buf)  {
  40. MartinJ. Brockhus
    July 29th, 2015 at 16:32 | #40

    Kristan;
    Just as a matter of interest how is the extensive library intended to be used?
    What I mean is when a keyboard , xbox or playstation controller in connected, where is the output coming to the surface?

    Martin

  41. August 3rd, 2015 at 00:15 | #41

    @MartinJ. Brockhus
    What do you mean by “coming to the surface”?

    I guess you are talking about the provided API? As you can see all my USB libraries use more or less the same API, so it should be easy for others to get familiar with a them no matter if they are using for instance the Playstation or Xbox libraries.

  42. MartinJ. Brockhus
    August 31st, 2015 at 15:17 | #42

    Finally back on-line after struggling with some sort of recaptcha immunity.

    Both my project’s hardware and sketch are in beta. I “steal” the bluetooth controller byte from BTHID.ccp through an externally declared variable, so far so good.

    But… when keyboard library is enabled and used for a 4×4 foil keyboard the BT connection freezes and will not be restored when rebooting.

    I have digged through the various codes but couldn’t find double use of timers or interrupts.

    Bluetooth library and keyboard library just seem not compatible to each other but couldn’t find anything further on this around.

    Any suggestion or link is much appreciated to solve this.

    Kind regards Martin JK,

  43. August 31st, 2015 at 16:14 | #43

    @MartinJ. Brockhus
    Can you share your project somewhere, then I will have a look πŸ˜‰

  44. MartinJ. Brockhus
    September 1st, 2015 at 19:16 | #44

    Hallo Kristian;

    Please do, I have put up assembly.ino and BTHID.ccp for you.
    see BTHID.ccp 197-221 additions to use the remote controller.
    Bluetooth is working from start but does not reconnect when lost.
    Bluetooth dies directly when keyboard function call is enabled.

    It appears I need your email address to allow you to see the files at my Google Drive

  45. September 1st, 2015 at 21:04 | #45

    @MartinJ. Brockhus
    You can just use my personal email: lauszus@gmail.com

  46. MartinJ. Brockhus
    September 2nd, 2015 at 13:51 | #46

    Have you received the link for access?

  47. September 3rd, 2015 at 23:06 | #47

    @MartinJ. Brockhus
    Sorry for the late response.

    First of all you don’t need to edit BTHID.cpp. You can just register a parser instead. Please see the following example: https://github.com/felis/USB_Host_Shield_2.0/tree/master/examples/Bluetooth/BTHID. Take a look at these lines: https://github.com/felis/USB_Host_Shield_2.0/blob/22d82ecd0a328b6a45c59133dd48aaf3c57e22b6/examples/Bluetooth/BTHID/BTHID.ino#L43-L44.

    The reason why it stops working might be because of a power issue. Please also see the following issue: https://github.com/felis/USB_Host_Shield_2.0/issues/77.

  48. kulverstukas
    September 10th, 2015 at 19:30 | #48

    I wish you good health. Read several times, downloaded the library, what should be the Bluetooth dongle? It’s definitely USB dongle or it can be UART HC-07?

  49. September 11th, 2015 at 08:26 | #49

    @kulverstukas
    You will have to use a Bluetooth dongle, as the HC-07 module only support SPP and there is no way to get access to the lower layers of the Bluetooth protocol.

  50. MartinJ. Brockhus
    September 12th, 2015 at 18:01 | #50

    Kristian,

    it’s definitely not a power issue since i use a 5v3a ps all the time.
    one for one bluetooth and keyboard functions are working well when enabled.
    when i use both the keyboard is working but the bluetooth side is completely dead.

    i have studied on your code and the parsers but couldn’t dfigure it out.
    from whati see the parsres just send it result to the serial console.
    therefor i have changed BTHID.ccp and use externally declarted variable.

    Basically everything is working OK but not bluetooth and keyboard at the same time.
    I couldn’t find them yet but do you use any timers or basic functions that keyboard may also use?

  51. September 30th, 2015 at 14:01 | #51

    @MartinJ. Brockhus
    Hmm it is properly a conflict somewhere in your code. Can you share it somewhere? Then I will take a look.

  52. April 5th, 2016 at 04:03 | #52

    i need help setting up the wiiupro. not sure why but i cannot seem to get this thing working

  53. April 14th, 2016 at 23:10 | #53

    @austin
    Please read the following section of the readme: https://github.com/felis/USB_Host_Shield_2.0#wii-library.

  54. Uddhav
    May 3rd, 2016 at 17:05 | #54

    Can you please help on specific pairing of controllers….like I have 2 PS4 controllers and two host shield with bluetooth dongles.
    How to connect specific PS4 to specific arduino . not to other one…is there anything related to setting address of bluetooth..

  55. May 6th, 2016 at 15:57 | #55

    @Uddhav
    Just pair one of them at a time and then when you press the PS button it will pair with the last device.

  56. joeyjojo
    May 12th, 2016 at 03:16 | #56

    Thanks for this library. I can get my bt joystick to connect and when I enable PRINTREPORT I can see the data in the serial monitor. My question is how do I plug this data back into my script? This is the output when I press up on the joystick L2CAP Interrupt: A1 03 B3 00 00 00 . How can I use this to turn on an LED. Sorry I am new to all this.

  57. May 14th, 2016 at 18:23 | #57

    @joeyjojo
    You will need to modify the parsers to your need: https://github.com/felis/USB_Host_Shield_2.0/tree/master/examples/Bluetooth/BTHID.

    However it looks like it is sending the data using report 0x03, so you will need to add it manually here: https://github.com/felis/USB_Host_Shield_2.0/blob/master/BTHID.cpp#L188-L209.

  58. joeyjojo
    May 17th, 2016 at 14:58 | #58

    @Kristian Sloth Lauszus
    Ok. I have modified the report to 0x03 and now the serial monitor data for dpad up is. The output for any presses is about the same.

    L2CAP Interrupt: A1 03 B3 00 00 00 LeftCtrl changed
    LeftShift changed
    RightCtrl changed
    RightShift changed
    RightGUI changed

  59. joe
    May 23rd, 2016 at 03:07 | #59

    @joeyjojo
    i got it working now, thanks!

  60. Andreas
    June 20th, 2016 at 18:40 | #60

    I try to conncet to Adafruit Bluetooth EZ Key device (HID keybord simmulator). Pairing works and I can see received keys.

    But at normal connect (after BTHID bthid(&Btd)) I get only errors when I swith the HID device on after “Wait For Incomming Connection Request. The error is “Connection Failed: 06” repeated many times.

    Any idea on how to solve this?

  61. Andreas
    June 22nd, 2016 at 07:11 | #61

    For a remote control project I want to use the Adafruit EZ Key BTHID Keybord Simmulator (https://learn.adafruit.com/introducing-bluefruit-ez-key-diy-bluetooth-hid-keyboard). It pairs successfully and I can see the keys. But after normal reconnect (with BTHID bthid(&Btd)
    I get only repeating errors. Either “Connection Failed: 06” or “Connection Failed: 10”.

    Any idea how to connect sucessfully?

  62. June 24th, 2016 at 09:06 | #62

    @joe
    Good to hear you got it working πŸ™‚

    @Andreas
    Sorry, but I haven’t experienced that before.

  63. Oleg_Todosko
    April 14th, 2017 at 10:17 | #63

    Tell me please.
    There is here such joystick:
    (https://www.aliexpress.com/item/2017-Rechargeable-Mini-Ring-Wireless-Universal-VR-Game-Pad-Bluetooth-Remote-Controller-Gamepad-Joystick-for-Android/32786864839.html?spm=2114.13010608.0.0.XvUWO0)
    But this joystick BLE!
    Can I connect it using USB Bluetooth dongle + USB Host Shield?
    Under Windows, this joystick is recognized as a keyboard.
    And even configured in the program “InputMapper”. A stick is just 4 buttons.
    Thank you in advance

  64. Jared
    February 23rd, 2018 at 14:36 | #64

    @Oleg_Todosko
    I also am trying to integrate one of these to an arduino project using USB host shield and this library. I Cannot seem to get the device to pair to the dongle on the shield. the device remains in discovery mode and the serial monitor only reads “HID Bluetooth Library Started”.These little controllers would be perfect for so many remote control projects.

  65. GΓ©rard ROBERT
    July 13th, 2018 at 18:07 | #65

    Hello,
    Did you manage to connect a mini joystick bluetooth on a Mega ADK 2650 ?

    Thank you if you have any information about this.

  66. January 30th, 2020 at 01:27 | #66

    Hi,

    Im trying to use an Arduino Zero to read inputs from a Bluetooth mouse (Apple Magic Mouse). It works fine with an old bluetooth 2.0 dongle I have but I cannot seem to get it to work with newer bluetooth dongles i bought on ebay or aliexpress. Is there a way to get more debug data when connecting a bluetooth mouse?

    It tried uncommenting:
    #define EXTRADEBUG
    #define PRINTREPORT

    but that did not display anything.

  67. Frank Yester
    April 22nd, 2020 at 19:03 | #67

    Kristian,

    Thank you for all your work on the USB Host Library and Examples.

    I’m wondering if I’m missing something very basic. I’m using the SparkFunUSB Host Shield ( new version 09947) with Arduino Uno trying to connect to a bluetooth keyboard using BTHID. Device appears to pair and run successfully and prints out “HID Bluetooth Library Started” but no matter what keys I press there are no further outputs. I’ve tried Boot Protocol and Report Protocol with the same results. I also ran board_qc and it passed all tests. I also have an external supply. I also tried a regular USB keyboard with the USBHID_desc example and that seemed to work ok. Any thoughts on what I might try?

    Thanks,
    Frank Yester

  68. Craig
    January 4th, 2022 at 03:37 | #68

    Have you (or anyone) used this with the ESP32?

  1. August 21st, 2017 at 22:22 | #1