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() {
  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
    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. younes
    February 18th, 2014 at 05:02 | #1

    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

  2. February 18th, 2014 at 17:02 | #2
Comment pages
1 2 3 2785
  1. July 31st, 2012 at 01:03 | #1