Arduino To Android - Turning An LED On and Off
Arduino To Android - Turning An LED On and Off
This example shows you how to program an Android device to communicate with an Arduino over
bluetooth and turn on or turn off an LED.
The Android device connects via the bluetooth to the Arduino which is running in a loop waiting for
data from the bluetooth inteface. The data is read from the bluetooth interface using the software
serial library. If the Arduino reads a 1 it turns an LED on pin 13 on. If it finds a 0 then it turns the
LED off. If it reads anything else it doesn't change the state of the LED.
If you are using the Sparkfun Bluetooth Mate when you successfully connect to it from your Android
program the Red Stat LED will turn off and the Green Connect LED will turn on solid.
For this example I used the same setup as I did for my earlier Arduino to Android tutorial: Arduino to
Android Basic Bluetooth Connectivity with the addition of an LED connected to pin 13 on the
Arduino.
Download Zip Archive of Android Source Files
Arduino Sketch
#include <SoftwareSerial.h>
SoftwareSerial mySerial(6, 5);
int dataFromBT;
void setup() {
Serial.begin(57600);
Serial.println("LEDOnOff Starting...");
// The data rate for the SoftwareSerial port needs to
// match the data rate for your bluetooth board.
mySerial.begin(115200);
pinMode(13, OUTPUT);
}
void loop() {
if (mySerial.available())
dataFromBT = mySerial.read();
if (dataFromBT == '0') {
// Turn off LED
digitalWrite(13, LOW);
} else if (dataFromBT == '1') {
// Turn on LEFD
digitalWrite(13, HIGH);
}
}
LEDOnOFF.java
goes in <ProjectRoot>\src\com\example\ledonoff\
package com.example.ledonoff;
import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;
import com.example.ledonoff.R;
import
import
import
import
import
import
import
import
import
import
import
android.app.Activity;
android.bluetooth.BluetoothAdapter;
android.bluetooth.BluetoothDevice;
android.bluetooth.BluetoothSocket;
android.content.Intent;
android.os.Bundle;
android.util.Log;
android.view.View;
android.view.View.OnClickListener;
android.widget.Button;
android.widget.Toast;
});
btnOff.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
sendData("0");
Toast msg = Toast.makeText(getBaseContext(),
"You have clicked Off", Toast.LENGTH_SHORT);
msg.show();
}
});
@Override
public void onResume() {
super.onResume();
Log.d(TAG, "...In onResume - Attempting client connect...");
// Set up a pointer to the remote node using it's address.
BluetoothDevice device = btAdapter.getRemoteDevice(address);
// Two things are needed to make a connection:
//
A MAC address, which we got above.
//
A Service ID or UUID. In this case we are using the
//
UUID for SPP.
try {
btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
errorExit("Fatal Error", "In onResume() and socket create failed: "
+ e.getMessage() + ".");
}
// Discovery is resource intensive. Make sure it isn't going on
// when you attempt to connect and pass your message.
btAdapter.cancelDiscovery();
// Establish the connection. This will block until it connects.
Log.d(TAG, "...Connecting to Remote...");
try {
btSocket.connect();
Log.d(TAG, "...Connection established and data link opened...");
} catch (IOException e) {
try {
btSocket.close();
} catch (IOException e2) {
errorExit("Fatal Error", "In onResume() and unable to close
socket during connection failure" + e2.getMessage() + ".");
}
}
// Create a data stream so we can talk to server.
Log.d(TAG, "...Creating Socket...");
try {
outStream = btSocket.getOutputStream();
} catch (IOException e) {
strings.xml
goes in <ProjectRoot>\res\values\
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">LED On Off</string>
</resources>
main.xml
goes in <ProjectRoot>\res\layout\
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnOn"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Turn LED On" />
<Button
android:id="@+id/btnOff"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Turn LED Off" />
</LinearLayout>
AndroidManifest.xml
goes in <ProjectRoot>
/>
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
</manifest>
Screen Capture from Acer A100
Notes:
1.
As the SDK Emulator doesnt emulate bluetooth and probably wouldn't be able to make a
connection even if it did this example will not run in the emulator.
2.
This example is built upon the work of others. I post it here not as an example of original
work but rather as a complete working example for reference. If I didn't appropriately credit you for
your work please let me know and I will add you.