There are multiple ways of how you can connect an Android device to an Arduino board. You can use wifi module, a usb cable or a bluetooth module. In this post, we will try to explore the connection between the two devices using bluetooth.
Bluetooth operates in the unlicensed band, technically that’s 2.4 GHz frequency. Unlicensed because you don’t need to get a license to use that frequency unlike frequencies being used in AM, FM and other microwave frequencies. Every android phone is equipped with a bluetooth device so you can connect or be discovered by other bluetooth devices as well. On the other hand, Arduino doesn’t provide out of the box bluetooth connectivity, instead, you need to buy a bluetooth shield or a module (below) to be able to connect and be discovered by other BT devices.
So, basically you need to get an Arduino board or any Arduino-like board. In my case, I bought a Seeduino ADK board because my previous projects requires me to use an ADK-compatible board. But in this example, you can just use any ordinary non-ADK Arduino board such as Arduino Diecimila, or any other variations of Arduino.
Our bluetooth module uses a standard pin arrangement which is compatible with an I/O Expansion Board that provides a docking space for our bluetooth module.
The DF Bluetooth V3 Module fits perfectly in the middle of the I/O Expansion Board as seen on the photo above. You can also see the wiring of the LEDs to the digital I/O pins of the Arduino board on the right most part of the photo.
The circuit is simple. You just need 6 pieces LEDs (3 red and 3 green), 6 pieces 330 ohms resistors and a bunch of connecting wires.
All LEDs are going to be connected in such a way that the cathode is connected to the resistor and the anode to one of the digital i/o pins. The resistor is then connected to the ground.
Each of the LEDs must be connected to digital I/O pins and in this example, we are going to connect them to pins 22, 24, 26, 28, 30 and 32. The breadboard diagram is shown below.
After we’ve built the hardware part, we can now open up our Arduino IDE and start to write some codes. First, let’s write the sketch. Basically, the idea is to be able to read from the bluetooth module’s serial stream and if there are any incoming data, read each character and store it to our array. Then use C’s builtin string comparison
strcmp method to check whether the incoming data corresponds to one of our pre-defined commands.
In this example, we use a 3 byte command to signify which pin to operate and if it is HIGH or LOW. So to turn the LED on that is connected to pin 22 (configured as p1), we need to send p11 and to turn it off, p10. You can define your own protocol here but do remember that you need to keep the size of the data being transferred from android to arduino small.
First, declare our pin variables and their corresponding I/O pin number in Arduino.
val will hold the command sent by android app.
In the setup method, we initialize the pins to OUTPUT mode so that we can set them to HIGH or LOW.
Serial.begin(9600) will initialize the serial connection to bluetooth module at 9600 baud rate.
In loop method, we check first if there are any available data coming from the bluetooth stream. If there is, we read the characters one byte at a time per loop and store it to our char array
val. Since we know that our commands will be of 3 bytes long, we can check if our counter
index is already 3 then we know that we’ve consumed all our data from the serial stream and we have the proper command to do string comparisons. The next if-else blocks just check if the input command matches any of our pre-defined commands:
Next, our Android application uses the Bluetooth API to discover, pair and connect to our Bluetooth module. In bluetooth connectivity, one has to be master and one has to be a slave. Master will be the one responsible to manage and keep the communication stream open.
Displaying Bluetooth Devices
For Bluetooth API documentation and tutorial, you can read this very-well written tutorial from the Android Developer site. I just would like to highlight some of the methods I use to be able to get the list of paired devices and unpaired and discoverable devices. One thing to take note is, before you can connect to a bluetooth device, you need to know their UUID. For connecting two smartphones, master device needs to generate its own UUID.
UUID.randomUUID() will generate the random UUID for you. But for our bluetooth module, everything is already in its firmware and we cannot just use any random uuid. I searched around and found that there’s a magic UUID used by devices like our BT module.
We will use this magic UUID later when we need to open a connection to the bluetooth device. For now, let’s explore how can we retrieve the list of nearby bluetooth devices.
This will immediately return all the bluetooth devices that you’ve paired before. Pairing normally requires you to input a security pin before you can connect and discover the services for a particular bluetooth device. For Bluetooth modules and other BT devices that doesn’t have display, normally the PIN is 1234 or 0000. Before we start our discovery, we need to register a broadcast receiver so that we can be informed that a bluetooth device was discovered which we can add to our list.
Here’s a trimmed down version of our broadcast receiver.
The bluetooth device object will be available as a parcelable extra in intent.
This will start discovery of all the bluetooth devices that’s within 30 meters ( I assume GNexus is using Class 2 BT chip) and we just need to display them in LsitView so that the user can select which one to connect to.
In the app, first screen displays already paired bluetooth devices and those bluetooth devices that are not yet paired and discoverable.
Connecting to Device
Once the user has selected a device, we can now try to connect to the bluetooth device and open a socket.
BTChannelThread is a thread object that simply retrieves an outputstream from the bluetooth socket and keeps it open until we disconnect from the bluetooth device.
After connecting to the device, we can now send data to our bluetooth module by using
OutputStream.write(bytes). In the app, I’ve used several radio buttons to turn on/off each LEDs. Each radio button corresponds to a particular command so that when I click on each one of them, it will send the command down the bluetooth socket’s outputstream and finally processed by our sketch.
Here’s a brief demo:
I hope you learn something from this post and as always, you can grab the source code of app and sketch from my github page