Android arduino communication

Now to the fun part!

In the previous post I explained how to interface and configure a Bluetooth Module with a Bus Pirate.

Here I will show how to toggle the Arduinos LED through an Android app!

Note that I’m using a Arduino Duemilanove.

You’ll need to connect the module to the Arduino. The +5V and GND pins are already there. The digital 0 and 1 pins are reserved for the PC/Arduino interface through the FTDI.
I used the digital pins 2 and 3 for the communication with the module.

The rxPin(2) is connected to the tx pin of the module.
The txPin(3) is connected to the rx pin of the module.

Arduino Code
Available here:

I could have implemented it without the use of
NewSoftSerial but didn’t. The delay when reading was added because of the baud rate configuration problem I was having, at 1200 bauds. At 9600 it is perhaps not needed. There is no need to actually return if the message was understood, I added it to experiment on possible information polling events.

Here is a short python code I wrote for my Ubuntu to test the communication.

Python test code
Available here:
Ubuntu will automatically ask if you want to pair with the device. It usually is called linvor and has the pin 1234 as default.

I based my code on the examples found here:
http://people.csail.mit.edu/albert/bluez-intro/c212.html(which also finds your device by name and gives you it’s mac address)

As for the Android code, you must add the permissions for bluetooth connections to the Manifest.

<uses-permission android:name=”android.permission.BLUETOOTH” />
<uses-permission android:name=”android.permission.BLUETOOTH_ADMIN” />

BLUETOOTH_ADMIN is only needed if you are to set your device visible or manage paired devices.

My main and only java class is available here:
(wasn’t going to get indented here in WordPress)

It should be noted that after creating a socket with

UUID mUUID = UUID.fromString(“00001101-0000-1000-8000-00805F9B34FB”);
BluetoothSocket mBTSock = device.createRfcommSocketToServiceRecord(mUUID);

which uses the default device UUID, you must still connect the socket.
It seems a socket cannot be reused after closing it. Probably would have to open a new socket.
Note that this implementation is far from being totally bug and idiot proof. I do the most of the error checks which probably would go wrong, but there are always more to add.

I used the DeviceListActivity from the BluetoothChat example available in the Android SDK. This class needs the res/layout/device_list.xml and res/layout/device_name.xml. I used it so the user can select a paired device from the list and it returns the Bluetooth address(mac) to the calling activity, so it knows to which device to connect.

Here is the whole project:

So, I hope this works for you. Will not run on Android Emulator(I think, since Bluetooth is not available) and was built/tested for android 2.1(SDK 7)

Sorry for the embedded images for the code. WordPress without decent plugins does not allow code to be displayed properly. And the android code was too long to paste together.


1 Response to “Android arduino communication”

  1. 1 Howard Patterson
    2012/02/11 at 17:09

    Thanks for posting this. I was trying to follow a post with similar hardware, (https://sites.google.com/site/r3s2v1/projects/candles), but it wasn’t clear how the mdfly device was connected to the arduino.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

The Author

Older Ramblings


Linux User Sig

%d bloggers like this: