Monday, May 14, 2012

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.println("LEDOnOff Starting...");

  // The data rate for the SoftwareSerial port needs to 
  // match the data rate for your bluetooth board.
  pinMode(13, OUTPUT);   

void loop() {
  if (mySerial.available())
    dataFromBT =;

  if (dataFromBT == '0') {
    // Turn off LED
    digitalWrite(13, LOW);
  } else if (dataFromBT == '1') {
    // Turn on LEFD
    digitalWrite(13, HIGH);
goes in <ProjectRoot>\src\com\example\ledonoff\
package com.example.ledonoff;

import java.util.UUID;

import com.example.ledonoff.R;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class LEDOnOff extends Activity {
  private static final String TAG = "LEDOnOff";
  Button btnOn, btnOff;
  private static final int REQUEST_ENABLE_BT = 1;
  private BluetoothAdapter btAdapter = null;
  private BluetoothSocket btSocket = null;
  private OutputStream outStream = null;
  // Well known SPP UUID
  private static final UUID MY_UUID =

  // Insert your server's MAC address
  private static String address = "00:00:00:00:00:00";
  /** Called when the activity is first created. */
  public void onCreate(Bundle savedInstanceState) {

    Log.d(TAG, "In onCreate()");


    btnOn = (Button) findViewById(;
    btnOff = (Button) findViewById(;
    btAdapter = BluetoothAdapter.getDefaultAdapter();

    btnOn.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
        Toast msg = Toast.makeText(getBaseContext(),
            "You have clicked On", Toast.LENGTH_SHORT);;

    btnOff.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
        Toast msg = Toast.makeText(getBaseContext(),
            "You have clicked Off", Toast.LENGTH_SHORT);;
  public void 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.
    // Establish the connection.  This will block until it connects.
    Log.d(TAG, "...Connecting to Remote...");
    try {
      Log.d(TAG, "...Connection established and data link opened...");
    } catch (IOException e) {
      try {
      } 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) {
      errorExit("Fatal Error", "In onResume() and output stream creation failed:" + e.getMessage() + ".");

  public void onPause() {

    Log.d(TAG, "...In onPause()...");

    if (outStream != null) {
      try {
      } catch (IOException e) {
        errorExit("Fatal Error", "In onPause() and failed to flush output stream: " + e.getMessage() + ".");

    try     {
    } catch (IOException e2) {
      errorExit("Fatal Error", "In onPause() and failed to close socket." + e2.getMessage() + ".");
  private void checkBTState() {
    // Check for Bluetooth support and then check to make sure it is turned on

    // Emulator doesn't support Bluetooth and will return null
    if(btAdapter==null) { 
      errorExit("Fatal Error", "Bluetooth Not supported. Aborting.");
    } else {
      if (btAdapter.isEnabled()) {
        Log.d(TAG, "...Bluetooth is enabled...");
      } else {
        //Prompt user to turn on Bluetooth
        Intent enableBtIntent = new Intent(btAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);

  private void errorExit(String title, String message){
    Toast msg = Toast.makeText(getBaseContext(),
        title + " - " + message, Toast.LENGTH_SHORT);;

  private void sendData(String message) {
    byte[] msgBuffer = message.getBytes();

    Log.d(TAG, "...Sending data: " + message + "...");

    try {
    } catch (IOException e) {
      String msg = "In onResume() and an exception occurred during write: " + e.getMessage();
      if (address.equals("00:00:00:00:00:00")) 
        msg = msg + ".\n\nUpdate your server address from 00:00:00:00:00:00 to the correct address on line 37 in the java code";
      msg = msg +  ".\n\nCheck that the SPP UUID: " + MY_UUID.toString() + " exists on server.\n\n";
      errorExit("Fatal Error", msg);       
goes in <ProjectRoot>\res\values\
<?xml version="1.0" encoding="utf-8"?>

    <string name="app_name">LED On Off</string>

goes in <ProjectRoot>\res\layout\
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:orientation="vertical" >

        android:layout_height="wrap_content" />

        android:text="Turn LED On" />

        android:text="Turn LED Off" />


goes in <ProjectRoot>

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="15" />

        android:label="@string/app_name" >
            android:label="@string/app_name" >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />


Screen Capture from Acer A100


  1. As the SDK Emulator doesn’t 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.


C.A Majdi said...

it cannot start the app after done compiling. can u post up fully source code in a zip file?

digitalhack said...

Just to double check on line 36 in did you replace "00:00:00:00:00:00" with the MAC address of your bluetooth device?

C.A Majdi said...

Also same, i replace the mac address using my Bluetooth Mac address. I got error, but i used CTRL+SHIFT+O to auto fix, and ignore error check for this file only. The error is :

it cannot compile if i fix all the problem error. or i got wrong step?

digitalhack said...

I am sorry but the error message you have posted doesn't make sense to me.

I found two problems with the posted java source. On the first line the "p" in package was missing and on the last line there was a "}" missing. You might want to try re-cutting and pasting the java source code into your project.

I also have updated the post to include a link to a zip archive that should have all the source files you need.

Sorry about the cut/paste errors. Hopefully that was the problem or the archive will fix your problem. Let me know.

master.crome register said...

Yay! Thanks it work now ;D, i wounder can we replace button with togglebutton?

Anonymous said...

i copy paste the codes and modify some of it (eg:package name, button name) to mine. there's no error but when i upload the codes, i cant run the app at all~

Krzysztof WesoĊ‚owski said...

Thanks a lot for sharing. It helped me a lot - yesterday I started adventure with Android and today, thanks to you, I can have my Arduino managed from under Android.

Best regards!

Anonymous said...

This app works very good for my arduino mega with silver bluetooth module! Thanks a lot!

Anonymous said...

hi my problem is that when i start the app first time works fine my bluetooth hc-06 on my ARDUINO stop flashing and stays on.when i start app on second time it does not stop flashing it is not connected.when i press on i got message fatal error inOnResume() write check if your UUID exist on the server.But when i disconnect my Bluetooth on my phone and connect it works again

Antoan Miroslavov said...

What software did you use to compile the source code?Android Studio returns an error during compilation.

lalit kumar said...

Dear Digitalhack,
thanks for sharing "Arduino to Android – Turning an LED On and Off" application. I have tried the application and it compiled without any error but unfortunately when i run the app on emulator or on my Tablet it gives a message "Unfortunately application stopped" followed by the notification for turning Bluetooth on, then clicking on yes it turns bluetooth ON. what should i do. I am working on Eclipse with android 4.4.2 (API 19)

digitalhack said...

I believe the problem you are having is that the emulator doesn't support bluetooth peripherals.


lalit kumar said...

Dear Digitalhack,
with the reference of my earlier comment, if i modify my project files according to your code then it does not work I could not understand what is the problem.Now, when I just copy and paste your files into my project then it is working. but when i press any button application get crashed with a message "Fatal Error - In onResume() and an exception occurred during write socket closed. check that the SPP UUID 00001101-0000..... exists on the server". Please note that there is no any receiver till now like Arduino etc. kindly suggest is it normal.

junaid mushtaq said...

When, I open the application without turning on bluetooth an error display "application error unfortunately LEDOnOff has been stopped" and when I open this application after bluetooth turning on its work correctly. Please guide me, this code is OK or something is missing!

lalit kumar said...

Dear Sir,
please clear following points.
1. In file we need to replace MAC address of which device. Android Tablet on which this application is installed Or Bluetooth module (HC-05).
2. How this android App will recognize/connect to Bluetooth module.

lalit kumar said...

Please advise why the application crashed on pressing turn LED On button with error "Fatal Error - In onResume() and an exception occurred during write socket closed. check that the SPP UUID 00001101-0000..... exists on the server". I have replaced MAC address with Android tablet Bluetooth address.