Home > Arduino, Bluetooth, USB > RFCOMM/SPP Library for Arduino

RFCOMM/SPP Library for Arduino

This is yet again a new library for Arduino.
But this time it is a speciel one and a library I have though about making for a long time since I wrote the PS3 Bluetooth Library. It is the RFCOMM/SPP library, in short it is a virtual serial port via Bluetooth, which means you can now communicate with your Arduino via Bluetooth using a normal terminal application on your computer, smartphone etc. It has been confirmed working with Windows 7, Mac OS X, Linux (Ubuntu 12.04) and several Android devices.

Arduino RFCOMM

Arduino Mega with some compatible dongles


For it to work you will need a USB Host Shield and a Bluetooth dongle, much like the PS3 Bluetooth Library.
After that you simply upload the example sketch to your Arduino.
The example sketch can be found below as well:

#include <spp.h>
USB Usb;
BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
/* You can create the instance of the class in two ways */
SPP SerialBT(&Btd); // This will set the name to the defaults: "Arduino" and the pin to "1234"
//SPP SerialBT(&Btd, "Lauszus's Arduino","0000"); // You can also set the name and pin like so

boolean firstMessage = true;

void setup() {
  Serial.begin(115200);
  if (Usb.Init() == -1) {
    Serial.print(F("\r\nOSC did not start"));
    while(1); //halt
  }
  Serial.print(F("\r\nSPP Bluetooth Library Started"));
}
void loop() {
  Usb.Task(); // The SPP data is actually not send until this is called, one could call SerialBT.send() directly as well

  if(SerialBT.connected) {
    if(firstMessage) {
      firstMessage = false;
      SerialBT.println(F("Hello from Arduino")); // Send welcome message
    }
    if(Serial.available())
      SerialBT.write(Serial.read());
    if(SerialBT.available())
      Serial.write(SerialBT.read());
  }
  else
    firstMessage = true;
}

The example sketch is pretty easy to understand:
First you include the library and create the USB instance. You will then need to create the BTD (Bluetooth Dongle) instance, which will take care of all the HCI communication. Finally you have to create the SPP instance – you will notice that you can create it in several ways. You can either just create it and then it will use the default name “Arduino” and the default pin “1234” – it is handled by this line in the source code. But you can also easily modify these values by including them as a string. By doing so you can set the name and pin of your device to whatever you want – the pin can be a maximum of 16 characters while the name can be as large as 248 characters as specified by the Bluetooth specification.

I then simple create the bool “firstMessage” which is used to detect when the connection is established. I then start the build in serial communication at 115200 baud and I then initialize the USB Host Shield. After that I just print a simple debug string to let the user know that the Arduino has started.
I then simple runs Usb.Task() which will initialize the Bluetooth dongle and run the SPP library automatically. It then waits until a SPP connection is established and if so sends a welcome message to the device.
After that it will simply sent any message it receives via it’s hardware serial port to the connected device via the SPP protocol. If a messages is received it is written to the connected computer via the USB serial communication.

You will of course need a terminal application on your device. I recommend the following terminals for the different platforms:

The full source code can as always be found at github: https://github.com/felis/USB_Host_Shield_2.0/blob/master/SPP.cpp.

Categories: Arduino, Bluetooth, USB Tags:
  1. August 5th, 2012 at 17:09 | #1

    Thank you very much for implementing this. I have been waiting for this for years after buying the USB Host Shield.

    I just tested it with two different dongles and I was able to talk to Android phone using BlueTerm without any issues.

  2. August 5th, 2012 at 17:11 | #2

    @Sudar
    Your welcome 🙂 Nice, glad to hear it’s working!
    May I ask, what kind of phone did you use and what version of Android?

  3. August 6th, 2012 at 15:46 | #3

    @Lauszus
    I used Nexus S phone running JellyBean to test it. On the phone I used the “BlueTerm” app.

  4. August 7th, 2012 at 14:09 | #4

    @Sudar
    Okay. Thanks for the feedback 🙂

  5. August 20th, 2012 at 10:29 | #5

    I have one more question. Is it possible to make two Arduino’s interact with each other using this library, without connecting them to a phone or computer?

  6. August 20th, 2012 at 10:48 | #6

    @Sudar
    No not in the moment. But you can use this library by Oleg instead: https://github.com/felis/Arduino-Bluetooth, but I can see that you have already seen that since you made a pull request. I have planned to add it to the library some day.

  7. August 20th, 2012 at 11:00 | #7

    @Lauszus

    Ohh okay, looking forward for it 🙂

    I have been using the old library and even ported it to Arduino 1.0. If the v2.0 of the USB Host Library supports Arduino-Arduino communication, then I can completely migrate to v2.0 of the USB Host library.

    Anyways let me know if I can be of any help. Would be happy to help you develop it.

  8. August 20th, 2012 at 11:10 | #8

    @Sudar
    It wouldn’t be that hard to implement it. You basically just need the BTD class (https://github.com/felis/USB_Host_Shield_2.0/blob/master/BTD.cpp) to handle all the HCI communication and then you can use a non-existing PSM: http://www.bluetooth.org/Technical/AssignedNumbers/logical_link.htm.
    I have to implement the Wii controller first and then I will look into it 🙂

  9. ttm
    August 29th, 2012 at 07:46 | #9

    the usb-host lib 2.0 can directly support a MEGA ADK board?
    rather than MEGA+USB-HOST Shield.
    thanks.

  10. August 29th, 2012 at 14:06 | #10

    @ttm
    Yes it can.
    You should just download the library and uncomment the following line to enable the ADK support: https://github.com/felis/USB_Host_Shield_2.0/blob/BTD/avrpins.h#L25

  11. martin
    October 8th, 2012 at 21:36 | #11

    Any idea how i could flush the input buffer ?

    i’m using SerialBT.read() to read some bytes and pass it to a function that takes some time to complete.

    Now the program is lagging because it has to process the old data in the buffer…

    The “normal” Serial class has a flush() function, what should i do to achieve the same ?

  12. October 8th, 2012 at 21:52 | #12

    @martin
    I will add a flush() function tomorrow.
    For now just move “uint8_t rfcommAvailable;” (see https://github.com/felis/USB_Host_Shield_2.0/blob/master/SPP.h#L162) to the public section and then set rfcommAvailable to 0 in your code.
    Thanks for your feedback!

  13. martin
    October 8th, 2012 at 22:54 | #13

    @Lauszus

    Thanks for the fast answer, i will try that tomorrow.

    Another thing i’m working on is getting the connection with Amarino to work. I’m able to connect to Amarino on my Android and read the data coming from it using the SPP class.

    But i want to use the MeetAmarino class. Problem is the MeetAmarino.h file uses the hardware Serial class. It shouldn’t be much work to replace that with the SPP class. I am however a Java programmer, not C … Eventually i will get it to work but maybe someone can combine the 2 classes to get it working.

  14. October 8th, 2012 at 23:03 | #14

    @martin
    I just had a look at it.
    Just create the instance in the top of the file and the just replace: “Serial.print”, “Serial.available()” and “Serial.read()” with “SerialBT.print”, “SerialBT.available()” and “SerialBT.read()”.
    And then make a function that will call USB.Task().

  15. Eivind
    October 19th, 2012 at 16:36 | #16

    Hi!

    I’m currently trying to get a PS3 controller to communicate with a arduino uno via a USB host shield and an BT dongle

    BT dongle: http://www.trust.com/products/product.aspx?artnr=17772
    USB host Shield: http://www.dfrobot.com/index.php?route=product/product&path=123_124&product_id=498#.UIFyyVGZh8E

    So far I haven’t been able to make any contact with the BT dongle, I tried to run the sketch above but only got the good old :

    SPP Bluetooth Library Started
    Unknown Device Connected – VID: 0CF3 PID: 3000
    BTD Init Failed, error code: 209

    I’m new to arduino so if any of you guys have some ideas and/or example code and stuff please feel free to share 😛 😀

  16. October 19th, 2012 at 19:39 | #17

    @Eivind
    It doesn’t look like your dongle is supported. Here is a list of dongles that are confirmed to work: https://github.com/felis/USB_Host_Shield_2.0/wiki/PS3-Information#wiki-Hardware

    Also try to uncomment the following line: https://github.com/felis/USB_Host_Shield_2.0/blob/master/BTD.cpp#L20 and send me the output 🙂

  17. Eivind
    October 20th, 2012 at 09:39 | #18

    @Lauszus

    Hey!
    Thanks for fast answer 😛

    I’ll try and get a hold of a working dongle.

    Hers the output after uncommenting the line:

    SPP Bluetooth Library Started
    BTD Init
    Addr: 01
    Unknown Device Connected – VID: 0CF3 PID: 3000
    BTD Init Failed, error code: 209

    Can’t say I understand much more :S

  18. October 20th, 2012 at 11:48 | #19
  19. Eivind
    October 20th, 2012 at 16:21 | #20

    @Lauszus
    Yes sir I did 😛

    By the way, should I be able to see the BT dongle form my computer via BT?

  20. October 21st, 2012 at 12:26 | #21
  21. Eivind
  22. October 24th, 2012 at 11:33 | #23

    @Eivind
    They look alright, but I think they are a bit expensive. Try amazon on ebay instead 🙂 I would only order them from a local retailer if I had to need them right now!

  23. chana
    October 26th, 2012 at 15:21 | #24

    hola el arduino puede usar cualquier modulo bluetooth??? puedo desarmar un bluetooth de computadora adaptarlo y usar esta biblioteca…???

  24. October 26th, 2012 at 18:07 | #25

    @chana
    Yes you can use any bluetooth dongle you would normally use with your computer via the USB port.

  25. Sam
    October 28th, 2012 at 17:05 | #26

    Is it posible to have multiple devices connected. (for instance my phone and the wii remote, with the wii library)?

  26. October 29th, 2012 at 23:19 | #27

    @Sam
    Yes it’s possible simply create the instances like so:

    #include <Wii.h>
    #include <SPP.h>
    USB Usb;
    BTD Btd(&Usb);
    SPP SerialBT(&Btd);
    WII Wii(&Btd);
  27. Eivind
    October 31st, 2012 at 21:34 | #28

    Hey!

    Just some more questions about the bleutooth libs!

    Is it possible to shorten down some of this code by removing options for wii-and move connection?
    https://github.com/felis/USB_Host_Shield_2.0/blob/master/BTD.cpp#L322-740

    Also is the wiimote connection only handled in the HCI_event_task(), HCI_task() and ACL_event_task()??

    And last, if I have two arduinos and two PS3 controllers and don’t want them to inerrupt eachother, is it just to make these lines different in the codes???
    PS3BT PS3(&Btd,0x00,0x15,0x83,0x3D,0x0A,0x57);

    https://github.com/felisUSB_Host_Shield_2.0/blob/master/examples/Bluetooth/PS3BT/PS3BT.ino#L12

  28. Eivind
  29. November 1st, 2012 at 00:52 | #30

    @Eivind
    Yes it is possible, but you wouldn’t save that much.
    These are the states only used by the wiimote: https://github.com/felis/USB_Host_Shield_2.0/blob/master/BTD.h#L47-49

    But you will save a lot more space by removing some of the content in PS3BT related to the move controller, for instance:
    https://github.com/felis/USB_Host_Shield_2.0/blob/master/PS3BT.cpp#L129-169, https://github.com/felis/USB_Host_Shield_2.0/blob/master/PS3BT.cpp#L209-223, https://github.com/felis/USB_Host_Shield_2.0/blob/master/PS3BT.cpp#L627-653

    But again you wouldn’t save much.
    The easiest way to save space is to comment “DEBUG” in both BTD.cpp and PS3BT:
    https://github.com/felis/USB_Host_Shield_2.0/blob/master/BTD.cpp#L19
    https://github.com/felis/USB_Host_Shield_2.0/blob/master/PS3BT.cpp#L19

    Or just removed those strings that you don’t need anyway.

    You have to get two dongles with different Bluetooth address – normally if you order multiply dongles from the same seller, for instance at ebay, they will have the same, so order a couple from different sellers!

  30. Eivind
    November 1st, 2012 at 20:29 | #31

    @Lauszus
    Sweet, thanks good Sir! 😛

  31. Timmh
    November 13th, 2012 at 01:18 | #32

    I have a sparkfun usb host shield , and a usb dongle usb-bt211. Power on the shield is fine, and I’ve connected D10-D13 to both boards (not using headers), but I’m not getting any bluetooth signal using the SPP example.

    Am I perhaps doin something wrong?

  32. Timmh
    November 13th, 2012 at 01:21 | #33

    @Timmh
    Sorry I’d like to add the serial monitor response is as follows:

    Unknown Device Connected – VID: 0CF3 PID: 3000
    BTD Init Failed, error code: 209

    bad usb dongle?

  33. November 13th, 2012 at 01:45 | #34

    @Timmh
    Yes your dongle is not supported.
    Take a look at this list of dongles that are confirmed to work: https://github.com/felis/USB_Host_Shield_2.0/wiki/PS3-Information#wiki-Hardware

  34. Eivind
    November 13th, 2012 at 12:28 | #35

    And I’m back!

    We are experiencing some trouble when we try to drive some servos via arduino and PS3 controller. Our problem is that we only manage to drive the servos with the PS3 controller when the arduino is connected to the computer. When we pull out the USB from the computer, the controller loses its connection.

    Any suggestions?

  35. November 13th, 2012 at 16:37 | #36

    @Eivind
    It might be a power issue. Try to connect an external power supply! 🙂

  36. Soumya
    November 15th, 2012 at 04:15 | #37

    Lauszus,

    I am using: http://www.newegg.com/Product/Product.aspx?Item=N82E16833139027&Tpk=GBU521

    which is same as: http://www.microcenter.com/product/387794/Bluetooth_40_USB_Micro_Adapter

    UPC of the Newegg’s item also matches with Microcenter’s item.

    But when I use the above Example Sketch, I get the following error:

    SPP Bluetooth Library Started
    Unknown Device Connected – VID: 0A5C PID: 21E8
    BTD Init Failed, error code: 209

  37. Timmh
    November 15th, 2012 at 04:27 | #38

    I’ve changed usb adapters now to a Kensington. Now I’m getting OSC did not start, does that mean my pin connection to my USB HOST shield from sparkfun is incorrect?

  38. Timmh
    November 15th, 2012 at 04:48 | #39

    nevermind if I wiggle it around it seems to work…

  39. Timmh
    November 15th, 2012 at 05:05 | #40

    Ok no its back to not working. It was blinking oddly there for a bit after it gave me the address and everything normally, and then when I hit reset it seems not to do anything. If I wait, the light on the bluetooth will start blinking, but not steadily. Is there perhaps a connection problem?

  40. Timmh
    November 15th, 2012 at 05:19 | #41

    Ok I can sort of get it working if I unplug usb every time and reset arduino.

    Now I see the address on my android phone, but it can’t be paired with. Is there a way to easily get the welcome message of the sample app to work so I can test that it indeed connects properly?

  41. Timmh
    November 15th, 2012 at 05:28 | #42

    I’m using Android’s Bluetooth SPP by Jerry Li, and I see the bluetooth device as “Arduino” but it says its not binding and not connecting.

  42. Timmh
    November 15th, 2012 at 06:58 | #43

    Ok so I’ve had succesful connections between my phone and the arduino, but it’s not reliable. I’ve connected all the pins from 7-13 between both the host shield and arduino, along with reset, vin and gnd.

    To get it to work, i need to unplug the usb, replug it in, open serial monitor.
    I then get “SPP Bluetooth Library Started”
    and need to hit reset on the arduino a random amount of times before I finally get:

    SPP Bluetooth Library Started
    SPP Bluetooth Library Started
    SPP Bluetooth Library Started
    SPP Bluetooth Library Started
    SPP Bluetooth Library Started
    Bluetooth Dongle Initialized
    No response to HCI Reset
    HCI Reset complete
    Local Bluetooth Address: 00:1B:DC:0F:DB:2A
    The name is set to: Rawr Arduino2
    Wait For Incoming Connection Request

    I managed to get a connection working a couple times and managed to send strings, almost by accident, but my phone shows the old name for the arduino, (Rawr Arduino) and can’t pair with it now. Before it seemed arbitrary whether I get a connection or not. I’ve tried to plug it into ac power, but it doesn’t seem to make a difference.

    I’m completely baffled as to how to get this to work reliably.

  43. Timmh
    November 15th, 2012 at 07:12 | #44

    I’m sorry about the giant wall of text, but this is an ongoing process, I’m slowly making progress myself until I guess someone comments and saves the day.

    So now I found that if I unplug the dongle from the usb host shield while its on, and put it back in I get the correct name of the device “rawr arduino2” and get asked for a new pin, and it connects to my phone but it doesn’t pair properly:

    SPP Bluetooth Library Started
    Bluetooth Dongle Initialized
    No response to HCI Reset
    HCI Reset complete
    Local Bluetooth Address: 00:1B:DC:0F:DB:2A
    The name is set to: Rawr Arduino2
    Wait For Incoming Connection Request
    Incoming Connection Request
    Remote Name: GT-I9000
    Connected to Device: D4:88:90:27:E4:4A
    Wait For Incoming Connection Request

  44. Timmh
    November 15th, 2012 at 07:40 | #45

    Ok this is my last post till tomorrow. I recorded a session of me solely hitting reset on the arduino board and not moving the board at all otherwise or fiddling with it. I slowly made progress towards getting my device paired but still couldn’t make the connection. It finally stopped responding to reset button being hit and windows popped up telling me my usb failed.

    OSC did not start
    SPP Bluetooth Library Started
    SPP Bluetooth Library Started
    Bluetooth Dongle Initialized
    No response to HCI Reset
    HCI Reset complete
    Local Bluetooth Address: 00:1B:DC:0F:DB:2A
    The name is set to: Rawr Arduino3
    Wait For Incoming Connection Request
    SPP Bluetooth Library Started
    SPP Bluetooth Library Started
    Bluetooth Dongle Initialized
    No response to HCI Reset
    HCI Reset complete
    Local Bluetooth Address: 00:1B:DC:0F:DB:2A
    The name is set to: Rawr Arduino3
    Wait For Incoming Connection Request
    SPP Bluetooth Library Started
    Bluetooth Dongle Initialized
    SPP Bluetooth Library Started
    Bluetooth Dongle Initialized
    No response to HCI Reset
    HCI Reset complete
    Local Bluetooth Address: 00:1B:DC:0F:DB:2A
    The name is set to: Rawr Arduino3
    Wait For Incoming Connection Request
    SPP Bluetooth Library Started
    Bluetooth Dongle Initialized
    No response to HCI Reset
    HCI Reset complete
    Local Bluetooth Address: 00:1B:DC:0F:DB:2A
    The name is set to: Rawr Arduino3
    Wait For Incoming Connection Request
    SPP Bluetooth Library Started
    Bluetooth Dongle Initialized
    No response to HCI Reset
    HCI Reset complete
    Local Bluetooth Address: 00:1B:DC:0F:DB:2A
    The name is set to: Rawr Arduino3
    Wait For Incoming Connection Request
    Incoming Connection Request
    Remote Name: GT-I9000
    Connected to Device: D4:88:90:27:E4:4A
    SPP Bluetooth Library Started
    SPP Bluetooth Library Started
    SPP Bluetooth Library Started
    Bluetooth Dongle Initialized
    Bluetooth pin is set too: 0001
    No response to HCI Reset
    HCI Reset complete
    Local Bluetooth Address: 00:1B:DC:0F:DB:2A
    The name is set to: Rawr Arduino3
    Wait For Incoming Connection Request
    SPP Bluetooth Library Started
    getDevDescr
    BTD Init Failed, error code: 3
    SPP Bluetooth Library Started
    Bluetooth Dongle Initialized
    No response to HCI Reset
    HCI Reset complete
    Local Bluetooth Address: 00:1B:DC:0F:DB:2A
    The name is set to: Rawr Arduino3
    Wait For Incoming Connection Request
    Incoming Connection Request
    Remote Name: GT-I9000
    Connected to Device: D4:88:90:27:E4:4A
    Wait For Incoming Connection Request
    Bluetooth pin is set too: 0001
    SDP Incoming Connection Request
    SDP Configuration Request
    SDP Successfully Configured
    SPP Bluetooth Library Started
    Bluetooth Dongle Initialized
    No response to HCI Reset
    HCI Reset complete
    Local Bluetooth Address: 00:1B:DC:0F:DB:2A
    The name is set to: Rawr Arduino3
    Wait For Incoming Connection Request
    SPP Bluetooth Library Started
    Bluetooth Dongle Initialized
    No response to HCI Reset
    SPP Bluetooth Library Started
    Bluetooth Dongle Initialized
    No response to HCI Reset
    HCI Reset complete
    Local Bluetooth Address: 00:1B:DC:0F:DB:2A
    The name is set to: Rawr Arduino3
    Wait For Incoming Connection Request
    SPP Bluetooth Library Started
    SPP Bluetooth Library Started
    Bluetooth Dongle Initialized
    SPP Bluetooth Library Started
    Bluetooth Dongle Initialized
    No response to HCI Reset
    HCI Reset complete
    Local Bluetooth Address: 00:1B:DC:0F:DB:2A
    The name is set to: Rawr Arduino3
    Wait For Incoming Connection Request
    Incoming Connection Request
    Remote Name: GT-I9000
    Connected to Device: D4:88:90:27:E4:4A
    Wait For Incoming Connection Request
    Bluetooth pin is set too: 0001
    SDP Incoming Connection Request
    SDP Configuration Request
    SDP Successfully Configured
    Disconnected SDP Channel

    FAILED (stopped responding to reset) after this

    I thought it could be a wiring issue, but not touching the wires, why would it randomly work at different levels on resets? Pin 7 is a bit loose, unplugging it gave me that first OSC error.

    ANY HELP IS GREATLY APPRECIATED, once again, sorry for the wall of text.

  45. November 15th, 2012 at 12:09 | #46

    @Timmh
    Hi Timmh,
    What phone are you using? I would still try a different dongle – it still doesn’t look like it’s working properly.

    Try to use BlueTerm instead on you Android phone, as I can confirm that it’s working fine.

    But please be moderate with all the amount of post. You have to understand that I’m in a different timezone, so I was actually sleeping while you posted your comments…

  46. Soumya
    November 18th, 2012 at 21:31 | #47

    Lauszus,

    Is there some kind of trick required – to get this IOGEAR GBI521 dongle working?

    I am using: http://www.newegg.com/Product/Product.aspx?Item=N82E16833139027&Tpk=GBU521

    which is same as: http://www.microcenter.com/product/387794/Bluetooth_40_USB_Micro_Adapter

    UPC of the Neweggâ??s item also matches with Microcenterâ??s item.

    But when I use the above Example Sketch, I get the following error:

    SPP Bluetooth Library Started
    Unknown Device Connected â?? VID: 0A5C PID: 21E8
    BTD Init Failed, error code: 209

  47. November 18th, 2012 at 21:49 | #48

    @Soumya
    No unfortunately not. The dongle is not supported. See the wiki for a list of dongles that are confirmed to work: https://github.com/felis/USB_Host_Shield_2.0/wiki/PS3-Information#wiki-Hardware

  48. Soumya
    November 18th, 2012 at 22:03 | #49

    @Lauszus
    Thank you.

    I actually had gone thru the above list – and then decided to buy the 7th item listed in the list – then instead of buying from micro center site, I bought the item from Newegg.

    But, it came out to be not plug-n-play. Even on Mac also this dongle does not work.

    Probably I got a newer downsized version of the same item.

    Thank you again.

  49. November 18th, 2012 at 22:34 | #50

    @Soumya
    Okay. Just because it’s looks the same doesn’t meen it’s the same hardware.

    You’r welcome. Just write again if you need any help with the library.

  50. Paolo
    November 22nd, 2012 at 21:00 | #51

    I modify your sample to sending IO port values, it work for a while next I receive some errors and the sketch stop to send.

    Here the complete logging sequence:

    SPP Bluetooth Library Started
    Bluetooth Dongle Initialized
    No response to HCI Reset
    HCI Reset complete
    Local Bluetooth Address: 00:10:60:AA:75:19
    The name is set to: Arduino
    Wait For Incoming Connection Request
    Incoming Connection Request
    Remote Name: ANDROID BT
    Connected to Device: 5C:E2:F4:CC:D1:F0
    Received Key Request
    Wait For Incoming Connection Request
    Bluetooth pin is set too: 1234
    SDP Incoming Connection Request
    RFCOMM Incoming Connection Request
    SDP Configuration Request
    SDP Successfully Configured
    RFCOMM Configuration Request
    RFCOMM Successfully Configured
    Received SABM Command
    Received UIH Parameter Negotiation Command
    Received SABM Command
    Send UIH Modem Status Response
    Send UIH Modem Status Command
    Send UIH Command with credit
    Received UIH Command with credit
    RFCOMM Connection is now established – Automatic

    …here the script send correctly almost 20 string values, then….

    Error sending L2CAP message: 0x0D – Channel ID: 0 66
    Error sending L2CAP message: 0x0D – Channel ID: 0 66
    Error sending L2CAP message: 0x0D – Channel ID: 0 66
    Error sending L2CAP message: 0x0D – Channel ID: 0 66
    ……….

    have some idea how to resolve?

    Regards

    Paolo

  51. November 23rd, 2012 at 19:41 | #52

    @Paolo
    Can you post your code somewhere, then I will have a look at it 🙂

  52. Paolo
    November 23rd, 2012 at 22:53 | #53

    @Lauszus
    Many thanks for your reply.
    I tried it with some Android Terminal app (Bluetooth Viewer, SENA Bterm,BlueTerm ecc.) and with a my application developed with B4A also.
    I tried your demo with Wiimote and all work very fine!, so I think the hardware it’s working.

    The code is very simple:
    *
    Example sketch for the RFCOMM/SPP Bluetooth library – developed by Kristian Lauszus
    For more information visit my blog: http://blog.tkjelectronics.dk/ or
    send me an e-mail: kristianl@tkjelectronics.com
    */

    #include
    USB Usb;
    BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
    /* You can create the instance of the class in two ways */
    SPP SerialBT(&Btd); // This will set the name to the defaults: “Arduino” and the pin to “1234”
    //SPP SerialBT(&Btd, “Lauszus’s Arduino”,”0000″); // You can also set the name and pin like so

    boolean firstMessage = true;

    void setup() {
    Serial.begin(115200);

    if (Usb.Init() == -1) {
    Serial.print(F(“rnOSC did not start”));
    while(1); //halt
    }
    Serial.print(F(“rnSPP Bluetooth Library Started”));
    }
    void loop() {
    Usb.Task();

    if(SerialBT.connected) {

    if(firstMessage) {
    firstMessage = false;

    SerialBT.println(F(“Connected!”));
    SerialBT.flush();
    delay(500);
    }
    else
    {

    //*************************************************************
    //try to send only 1 byte
    //SerialBT.println(String(port0++));
    //delay (300); THAT LOSE CONNECTION AFTER 17 LINES
    //delay(30); THAT LOSE CONNECTION AFTER 17 LINES
    //delay(0); THAT REPORT “Error sending L2CAP message: 0x0D – Channel ID: 0 66” AFTER 60 LINES

    //*************************************************************
    //try to send port values
    //THAT STOP WITH ERROR MESSAGE “Error sending L2CAP message: 0x0D – Channel ID: 0 66” AFTER ALMOST 18 LINES
    SerialBT.println(“A”+String(analogRead(0))+”|”
    +”B”+String(analogRead(1))+”|”
    +”C”+String(analogRead(2))+”|”
    +”D”+String(analogRead(3))+”|”
    +”E”+String(analogRead(4))+”|”
    +”F”+String(analogRead(5))+”|”);
    delay(1000);

    //delay(200); //same behavior

    //SerialBT.flush(); //same behavior
    //************************************************************
    }
    }
    else
    firstMessage = true;
    }

    //Many thanks in advance
    //
    //Paolo

  53. November 24th, 2012 at 14:17 | #54

    @Paolo
    Try to split the string up like so:

    String string = â??Aâ?+String(analogRead(0))+â?|â?
    +�B�+String(analogRead(1))+�|�
    +�C�+String(analogRead(2))+�|�
    +�D�+String(analogRead(3))+�|�
    +�E�+String(analogRead(4))+�|�
    +�F�+String(analogRead(5))+�|�;
    SerialBT.println(string);

    A longer delay will actually just make things worse, as you have to read the endpoint once in a while, this is how I would recommend doing it instead: https://github.com/TKJElectronics/BalancingRobotArduino/blob/master/BalancingRobotArduino.ino#L107-L110.

  54. Paolo
    November 25th, 2012 at 00:57 | #55

    @Lauszus
    Thanks for reply but unfortunatly not resolve :(.
    Using your sample above I can send any char from the device to Arduino but not viceversa (I think it should), the last startup message received is:
    Disconnected SDP Channel
    this can help to understand?

    can I send you PM?

  55. Lemorlenny
    November 25th, 2012 at 12:41 | #56

    @Paolo
    I believe some software app interfered with my bluetooh communications, ’cause I was able to send perfectly for a while, until I reset the device, so I think the problems are inside my apparatus configurations.
    I call you if discover something.

    Regards

  56. November 25th, 2012 at 17:08 | #57

    @Lemorlenny
    Okay. No you can’t send me a PM here. Either use the comments or send me an email at kristianl@tkjelectronics.dk

  57. Mohammad Hefny
    November 27th, 2012 at 17:44 | #58

    Do you think this code can work on Arduino DUE directly….

    should I use SPP, BTD, Message and compile them with USB host for Arduino DUE ?

    I am new to arduino and need your advise.

  58. November 27th, 2012 at 18:23 | #59

    @Mohammad Hefny
    No it can’t be used with the Due directly as the usb host functionality is much different, but it shouldn’t be that difficult to port the different bluetooth libraries I created.

  59. acs
    December 28th, 2012 at 02:11 | #60

    using vztec vz-bt2279
    i get the following error :

    SPP Bluetooth Library Started
    Bluetooth Dongle Initialized
    HCI Reset complete
    Local Bluetooth Address: 00:15:83:0C:BF:EB
    The name is set to: Arduino
    Wait For Incoming Connection Request
    getConf
    BTD Init Failed, error code: 13

    it would be better if you can compatible models instead of pictures,
    based on your pictures i bought another dongle
    output : BTD Init Failed, error code: 13(immediately)

  60. December 28th, 2012 at 22:06 | #61

    @acs
    What do you mean?
    It’s impossible to tell if a dongle is supported or not based on a picture. Two dongles might look exactly the same, but one of them might not work. It all depends on the chip inside the dongle.
    But since they only cost 1$ you can just buy a couple of them.

    A list of dongles that are confirmed to work can be found here: https://github.com/felis/USB_Host_Shield_2.0/wiki/PS3-Information#wiki-Hardware

  61. Kapil Saraswat
    January 18th, 2013 at 21:20 | #62

    Hi! how arduino communicate with computer (or mobile phone) via bluetooth? Any example?

  62. January 18th, 2013 at 21:23 | #63

    @Kapil Saraswat
    There is a written description for a Android phone here: http://www.circuitsathome.com/mcu/using-usb-bluetooth-dongle-on-arduino-to-connect-to-android-phone

    To tell you how to connect to it with your computer I need to know what OS you are running? Like Windows, Linux, Mac etc.

  63. Kapil Saraswat
    January 18th, 2013 at 21:37 | #64

    How can I connect a pendrive through this shield?

  64. January 18th, 2013 at 21:38 | #65

    @Kapil Saraswat
    It’s currently not supported, but it is in development!

  65. Kapil Saraswat
    January 18th, 2013 at 21:41 | #66

    sorry to not mentioning os! I use windows os and I dint have android to test the code, thats why I requesting for the PC or Mobile connection. In nokia C1-02 it detect and then it disconnected. Thanks for your reply.

  66. January 18th, 2013 at 21:44 | #67

    @Kapil Saraswat
    Okay. Simply pair with the Arduino. It will then create a new COM-port.
    Now download PuTTY: http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html, now you are able to select the COM-port and press connect.

    I can’t say if your phone is supported or not, but I don’t think so.

  67. Kapil Saraswat
    January 18th, 2013 at 21:54 | #68

    @Lauszus
    One side I have one arduino board and another side I have computer with bluetooth. Now I want to see the response of arduino in PC linked via bluetooth. Is putty is for bluetooth communication? I think it’s for serial communication. I have lack of hardware so kindly tell me the possible way for testing (also in my place it is hard to find some electronic parts and the online shopping (for arduino) means I wait for least 10 days). Again thanks for your instant reply.

  68. January 18th, 2013 at 22:02 | #69

    @Kapil Saraswat
    SPP is serial communication! So yes you can use putty with Bluetooth devices too.
    If you want to see the debugging messages from you Arduino then you can use any serial terminal you like. You can use Putty as well or the build in serial monitor in the Arduino IDE.

  69. kapil saraswat
    January 30th, 2013 at 13:51 | #70

    Its working! thanx. But how to develop a thing that uses bi-directional communication? like, send the command to arduino and it send the value of analog signal (e.g. temperature)
    thanx

  70. January 30th, 2013 at 15:02 | #71

    @kapil saraswat
    You could simply send a speciel character to the Arduino and it then responds with the analog reading.
    If you want to write your own Android app, you can have a look at this app: https://github.com/TKJElectronics/BalanduinoAndroidApp I use to control my balancing robot.

  71. Tuomas
    February 18th, 2013 at 22:09 | #72

    Q: I have a cheap JY-MCU model that works is connected to the Arduino via the TX and RX ports. Will the code work for such a connection as well ?

    Q2: I just want two things from my remote: let’s say that when I send the value “1” via terminal, the wheels go forward, and for “2” the wheels go backwards. What is the part of the code that does that (or something similar) in this library ?

    Sorry for stupid questions but I’m a noob 🙂

  72. February 20th, 2013 at 14:31 | #73

    @Tuomas
    This code actually does what’s inside a Bluetooth module like that. The reason why I wrote it was because you don’t have to use a hardware UART and I were already using the Bluetooth dongle for communication with a Wii and a PS3 controller.

    I think you have misunderstood what this library is all about. It’s simply let you send data to and from the Arduino via Bluetooth.
    If you want your robot to do react on a certain character, then have a look at these lines for my balancing robot: https://github.com/TKJElectronics/BalancingRobotArduino/blob/master/BalancingRobotArduino.ino#L217-L281.

    but you can do something similar

  73. Frederik Juul
    February 20th, 2013 at 18:00 | #74

    Hi! Is there any chance that this library could be changed to inherit from the Stream Library? That would make a lot of my coding easier, and it seems as though it would be a thing that would be required in the long run, if this library is to continue to be the de-facto standard Arduino USB library.

  74. February 20th, 2013 at 18:57 | #75

    @Frederik Juul
    I havn’t looked in to it, but that would certainly be a good idea.
    Do you got any experience with it?

  75. February 20th, 2013 at 19:02 | #76

    @Frederik Juul
    Actually the main reason I havn’t used it is that you need to send the characters in one large string as ofter as possible or you would have to poll the endpoint in order to prevent the Bluetooth communication to work.

  76. xenonforlife
    March 26th, 2013 at 11:08 | #77

    Hi Lauszus,

    This is what I want to achieve. I have a separate circuit where I blink an Led with a particular sequence. This sequence represents encoded data. I want to be able to sense this pattern with a photo detector and decode the data from the led blink, and then I want to send this decoded data via bluetooth to my android device. I am a newbie to programming on a board, however I have programmed on Android. Do you think it is possible to achieve what I want with an Arduino. I have ordered the board, but they have still not delivered it. So I am still in the learning phase of my project, contemplating the problems I might run into. Do you think it is a better idea to collate and decode the entire data on the board itself and send it to the android in one go is a better idea? or should I send the information on the fly from the sensor to the android device and decode it in my Android code..please help! Sorry I have posted the same thing on the other forum as well

  77. March 27th, 2013 at 14:25 | #78

    @xenonforlife
    I think it would work better if you used a IR LED modulated at a specific frequency and then use an IR receiver to receive the data, by doing that you will filter out any noise. See this blog post: http://blog.tkjelectronics.dk/2012/03/attinyremote/ and this library: http://www.righto.com/2009/08/multi-protocol-infrared-remote-library.html.

    I would decode the data on the Arduino and then send the decoded command to the Android phone.

  78. mohmad
    March 27th, 2013 at 17:13 | #79

    hi all,my Bluetooth dongle worked fine two weeks ago but now i have the error btd initialization failed error 15,please help

  79. March 27th, 2013 at 17:36 | #80

    @mohmad
    Please post the full error messages, but I think it’s a hardware problem if it just suddenly stops working. Try a different dongle.

  80. mohmad
    March 27th, 2013 at 19:35 | #81

    HI i already replaced the dongle ,this is the error:- getDevDescr
    BTD Init Failed, error code: 15

  81. March 28th, 2013 at 18:30 | #82

    @mohmad
    Okay. I don’t think you can do anything about it and dongles are so cheap anyway.

  82. mohmad
    March 28th, 2013 at 19:05 | #83

    can you tell me whats wrong?

  83. March 28th, 2013 at 23:21 | #84

    @mohmad
    No, it could be a lot of different things.

  84. xenonforlife
    April 2nd, 2013 at 15:40 | #85

    @Lauszus Thank you so much for your reply. The thing is I want to be able to use visible light communication for my project and study the behavior of the same. I just received my board today and started playing with it right away. I am being able to send and receive signals between my Android phone and the Arduino board with the phone connected to the board with a usb cable. What I want to know would I need to change my Android side code if I want to use a bluetooth dongle instead? Will my FileInputStream object still be able to receive the data in the similar way? Since now(according to the SPP sample sketch) I will no longer be using adk.RcvData or adk.SndData, will my Android code remain the same as well or do I need to get a bluetooth socket and get an Input/Output Stream from that socket? I am in doubt because the bluetooth dongle is being inserted in the usb port of the board.

  85. xenonforlife
    April 2nd, 2013 at 15:54 | #86

    @Lauszus I just checked the code for Blueterm and it does use bluetooth sockets for communication. What I understand is I cannot seamlessly plug in/out my bluetooth dongle without affecting communication with the Arduino board. Please correct me if I am wrong.

  86. April 2nd, 2013 at 22:42 | #87

    @xenonforlife
    You should check out my Android app for my balancing robot (see Kickstarter: http://www.kickstarter.com/projects/tkjelectronics/balanduino-balancing-robot-kit).
    And here is the relevant java code: https://github.com/TKJElectronics/BalanduinoAndroidApp/blob/master/src/com/tkjelectronics/balanduino/BluetoothChatService.java.

    @xenonforlife
    Of course it stops working if you plug out the Bluetooth dongle from the USB Host Shield.

  87. xenonforlife
    April 3rd, 2013 at 06:55 | #88

    @Lauszus Ya ofcourse, but what I meant was something like in the IOIO, where I can plug out the BT dongle and plug in the usb cable and vice versa without any disruption, thats what I actually meant, sorry for the confusion.

  88. April 3rd, 2013 at 14:50 | #89

    @xenonforlife
    Yes that is possible. You just need to write code both for the Bluetooth connection and the ADK connection. It shouldn’t be that difficult.

  89. Nichlas
    April 4th, 2013 at 19:16 | #90

    Hey @Lauszus!
    I want to build a bluetooth connection between my android phone and my Arduino, using the USB shield with a Bluetooth dongle for great connection range. In the end i want to controle the arduino with my phone (like you RGB light project only with a greater range).
    – where do I look to find all the ansewers involving in that project ?? programming an app for my phone, the arduino code and so on?

  90. April 4th, 2013 at 19:20 | #91

    @Nichlas
    You should check out the app I use to control our Balancing robot: http://www.kickstarter.com/projects/tkjelectronics/balanduino-balancing-robot-kit. And here is the relevant java code from the app: https://github.com/TKJElectronics/BalanduinoAndroidApp/blob/master/src/com/tkjelectronics/balanduino/BluetoothChatService.java.

    The best advice I can give you is just start reading other peoples code and use google a lot! Whenever I need to implement something I simply google it, since I’m no expect at Android programming.

  91. the_ether
    May 16th, 2013 at 14:19 | #92

    Can Bluetooth be used with one host and several clients that can communicate to the host at the same time? I’d like to build a star network of Arduino devices that communicate wirelessly, using Bluetooth or some other system.

  92. May 16th, 2013 at 22:11 | #93

    @the_ether
    At the current time an Arduino can’t talk to another Arduino, but you can connect multiple devices to the Arduino – see: https://github.com/felis/USB_Host_Shield_2.0/blob/master/examples/Bluetooth/SPPMulti/SPPMulti.ino.

    If you want to Arduinos to talk to eachother, then take a look at the following code: https://github.com/felis/Arduino-Bluetooth.

  93. the_ether
    May 17th, 2013 at 00:53 | #94

    @Lauszus
    Thanks for the reply. I don’t understand how you can say that Arduinos can’t talk together but yet suggest I look at some code that allows them to talk together.

    But at any rate, since posting this I’ve discovered Xbee. Zigbee would seem better suited for a mesh network than Bluetooth. Thanks anyway.

  94. Ajith Padman
    August 30th, 2013 at 09:29 | #95

    I would like to know how can I use this library for raspberry Pi. Can I use the same directly there or it needs some adaptation?

  95. August 30th, 2013 at 17:21 | #96

    @the_ether
    What I mean is that they can’t talk together using the newest version of the code, but by using the old code I linked to.
    Yes I have also just used Xbee if I wanted two Arduino communicating, that’s why I have never prioritised getting SPP working between two Arduinos.

    @Ajith Padman
    You can’t use this for the Raspberry Pi as this is intended for AVR microcontrollers, but I’m pretty sure the Raspberry Pi already support it as it’s running Linux – just google it and I’m sure it will come up.

  96. JamesO
    November 13th, 2013 at 03:28 | #97

    Hello,
    This version of the example sketch works perfectly with my arduino uno R3 and the USB host shield!
    However, the most recent version (October 21, 2013) on the github site https://github.com/felis/USB_Host_Shield_2.0/blob/master/examples/Bluetooth/SPP/SPP.ino
    comes up with an error:

    sketch_nov12b.cpp: In function ?void setup()?:
    sketch_nov12b.cpp:24:11: error: no match for ?operator!? in ?!Serial?
    sketch_nov12b.cpp:24:11: note: candidate is: operator!(bool)

    It appears that the added line:
    while (!Serial);
    is the problem.

    I’m fine using the older version of the example sketch, but perhaps you should list both versions on the github site so people using the Uno R3 aren’t confused about the error.
    THANK YOU SO MUCH FOR THIS EXAMPLE!!

  97. November 13th, 2013 at 13:08 | #98

    @JamesO
    You need to update the Arduino IDE. That line is needed for Leonardo, Teensy and other boards with built-in USB CDC serial connection – see: http://arduino.cc/en/Serial/IfSerial.

    You will need to at least use Arduino 1.0.1, but I recommend getting the newest beta version 1.5.4.

  98. February 14th, 2014 at 17:25 | #99

    Anyone know if this works with the Mega 2650 ADK board (R3) ?

  99. February 14th, 2014 at 19:25 | #100

    @Joyceck
    Yes it works with that board as well. There is instructions in the readme for the library: https://github.com/felis/USB_Host_Shield_2.0#boards.

  100. younes
    February 18th, 2014 at 05:02 | #101

    I have a arduino adk and I want to know pliz
    I can work with its built its USB port?
    and I write my program the same way?

    et merci

  101. February 18th, 2014 at 17:02 | #102
  1. July 31st, 2012 at 01:03 | #1