Since Kristian’s release of SPP service for USB Host 2.0 library I got many e-mails with questions about Bluetooth serial communications in general and using the SPP class in particular. While establishing a Bluetooth connection is quite easy when using PCs/smartphones/tablets, the Arduino implementation is less user-friendly (for a good reason) and somewhat confusing. This article is intended to be a short tutorial of Arduino USB Bluetooth connectivity using an Android device as a peer – in my opinion, the simplest one to set up and communicate.
To follow this tutorial, you will need the following:
- An Arduino board ( I sell them too )
- full-size USB Host Shield 2.0
- USB Host Shield 2.0 library downloaded and installed in your Arduino library directory
- An SPP example sketch
- Bluetooth dongle – see below for detailed discussion
- Android device – a phone or tablet. The accuracy of this tutorial has been checked on Nexus S phone and Nexus 7 tablet, both running Jelly Bean 4.1.1 OS.
- A Bluetooth terminal emulator app for Android – I use Blueterm
The hardware setup is shown on the title photo (click on it to make it larger). It is a good idea to use external power supply at least in the beginning – some Bluetooth dongles are not happy when powered together with Arduino board/USB Host shield from USB cable. On a side note, it is always a good idea to use external power supply while experimenting with USB devices, especially when using current Arduino boards with their weak regulators. Also, Bluetooth code requires plenty of resources. The example used in this article compiles in ~24K and even though it would work on standard Arduino, any meaningful project will likely require a bigger board, like a Mega.
Finding compatible Bluetooth dongle could be tricky. Many will work; a pile of dongles in the foreground represent compatible ones. Older dongles from Belkin, D-Link and Kingston are all good. Most Chinese dongles from eBay are likely good too. Among brand names, Asus dongles won’t work. Bluetooth dongle sold by Sparkfun is an example of incompatible Chinese product.
Here’s how the terminal output looks like when compatible dongle is used:
1 2 3 4 5 6 7
SPP Bluetooth Library Started Bluetooth Dongle Initialized No response to HCI Reset HCI Reset complete Local Bluetooth Address: 00:0C:76:46:1A:D6 The name is set to: Arduino Wait For Incoming Connection Request
Note line 3. You may or may not see this message. In any case, the output similar to the one above is an indication of a good dongle. Also note device address – if you have many active BT devices around you this address will help you find your Arduino in the pairing list.
Now let’s look at the dark side. The following terminal output is typical for an incompatible dongle:
SPP Bluetooth Library Started Unknown Device Connected - VID: 0CF3 PID: 3000 BTD Init Failed, error code: 209
Here is another one:
SPP Bluetooth Library Started getConf BTD Init Failed, error code: 13
You may also receive an output which is even less informative. Sometimes, only the first line is printed. Sometimes, some random garbage is printed. And sometimes, no output is given at all. All this mean your dongle is incompatible with library code and you need to find a new one. Here’s some closeups of my bad dongles.
A little ASUS dongle has downloadable firmware. When used on Windows machine, a dongle appears on the bus as a proprietary device, the driver detects it, loads the firmware and resets the dongle. After reset the dongle comes up as a typical Bluetooth. Due to the limited resources this process is difficult to implement on Arduino therefore this dongle is not supported.
Other two dongles are equipped with strange controllers which don’t like MAX3421E for some reason. I have several others packaged in similar cases. There is a lot of dongles looking like this on eBay – don’t buy them, they don’t work very well with a PC either.
It’s now time to move to the part 2 – connectivity. Assuming that a compatible dongle is attached and sketch is displaying ‘Wait For Incoming Connection Request’ line it’s time to switch attention to the Android device. The steps and screenshots (click on them to make them larger) are enumerated below:
- Go to ‘Settings’ and make sure Bluetooth is turned on as pictured. It is also a good idea to check if any remnants from previous pairing attempts are present – tap on Bluetooth and see if any Arduinos are listed as paired and if so, unpair them
- Open Blueterm. The opening screen will say ‘not connected’ in the upper right corner. Go to ‘Settings’ (device dependent, mine has an icon under the screen, second from the left) and tap on ‘Connect device’. The next screen appears.
- Tap on ‘Scan for devices’. If you have made pairing attempts before, you will see the list of paired devices. You can try to connect to the device from the list, however, I found it less reliable so I suggest to start with a clean list (see 1.)
- After a short time the next screen pops up. This is pairing request sent from Arduino. Tap on the form field
- Type a PIN and press ‘Done’. On the next (or is it previous?) screen tap ‘OK’
- This is the final screen with ‘Hello from Arduino’ line transmitted via Bluetooth. It is now possible to type in an Android or Arduino terminal window and see letters appear in another terminal window
The following listing shows Arduino terminal output during connection negotiation phase. A lot of useful info can be extracted from it. If you’re having difficulties while connecting, please copy this output from your terminal and post it in the comments.
Thank you for your time!
Incoming Connection Request Remote Name: Nexus S Connected to Device: 94:63:D1:BC:DD:B4 Wait For Incoming Connection Request SDP Incoming Connection Request SDP Configuration Request SDP Successfully Configured Disconnected SDP Channel Bluetooth pin is set too: 1234 RFCOMM Incoming Connection Request 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 SDP Incoming Connection Request SDP Configuration Request SDP Successfully Configured Disconnected SDP Channel HCI Disconnected from Device Wait For Incoming Connection Request
- Using USB Bluetooth dongle on Arduino to connect to Windows PC
- Exchanging data between USB devices and Android phone using Arduino and USB Host shield
- Bluetooth code for Arduino USB Host
- Bluetooth RFCOMM/SPP service support for USB Host 2.0 Library released!
- Android ADK terminal emulator for Arduino
- PS3 and Wiimote Game Controllers on the Arduino Host Shield: Part 3
- RC car controlled by Wii Remote on Arduino
- PS3 and Wiimote Game Controllers on the Arduino Host Shield: Part 1
- Interfacing Arduino to USB GPRS modem
- PS3 and Wiimote Game Controllers on the Arduino Host Shield: Part 2