Friday, October 24, 2014

Building an Android App to Communicate with the HC-06 Bluetooth Module

In this video we will build an Android App to communicate with the low cost HC-06 Bluetooth module. The HC-06 is connected to Arduino Uno and the Android App we build turns on and off an LED connected to the Arduino. Below the video you will find the Arduino code and a link to download the Android App code (MIT Inventor 2 was used to build the Android App). Enjoy!



Link to download App Inventor 2 code (.aia file):
https://dl.dropboxusercontent.com/u/26591541/AndroidBTExample.aia

Arduino Code:
/*
  This sketch is part of a tutorial for connecting to and communicating with an HC-06 or an RN-42 bluetooth module using a custom Android App. 
  The bluetooth modules are connected to an Arduino and the Arduino is connected to an LED. The Android app is used to wirelessly turn on and
  off the LED using  bluetooth. 

  This code is in the public domain.
 */

// Pin 7 has a LED connected to it
int led = 7;

// the setup routine runs once when you press reset:
void setup() {
  
  Serial.begin(9600);
  // initialize the digital pin as an output and set it low initially
  pinMode(led, OUTPUT);
  digitalWrite(led, LOW);
}

// the loop routine runs over and over again forever:
void loop() {
  delay(30);
  String t; //create an empty string to store messages from Android
  while(Serial.available()) { //keep reading bytes while they are still more in the buffer
    t += (char)Serial.read(); //read byte, convert to char, and append it to string
  }
  
  if(t.length()) { //if string is not empty do the following
    if(t == "on") { //if the string is equal to "on" then turn LED on
      digitalWrite(led, HIGH); //Set digital pin to high to turn LED on
      Serial.write("LED is on"); //Tell the Android app that the LED was turned on
    }
    else if (t == "off") { 
      digitalWrite(led, LOW);  
      Serial.write("LED is off");
    } // turn the LED off by making the voltage LOW
  }
}


10 comments:

  1. I used your aia.
    I installed the application (apk) in my phone.
    But when I run the application Ι can not detect any Bluetooth Device.
    I used an application (bluetooth tracker) from Google Play and this application saw the arduino.
    My smartphone is a htc desire c.

    The app inventor applications work on all devices?
    I should do something for my own device?

    ReplyDelete
  2. Hello SIDIR, Did you first go into your Bluetooth settings on the Android device and make sure Bluetooth is enabled? If it is enabled did you pair with the HC06 first? You must pair with the Bluetooth device for it to show up in the connection menu of the app.

    ReplyDelete
  3. after i click the bluetooth button, then the apps not responding. why??

    ReplyDelete
    Replies
    1. Can you be more specific? The address selection and connection button at the top of the app?
      Be sure to first enable Bluetooth on your device and pair with the HC-06 module. This is done via your settings menu on the Android device.

      Delete
  4. Hi! I have the same problem that awwabin. When I connect my Smartphone with HC-06 and try to send a data or number, with Send.Text block of app inventor, my app crashes. I don't know why :S

    Can you help me?

    Thank you!

    ReplyDelete
    Replies
    1. So you are able to connect the phone to the HC06 with no issues? Does the app crash in its original form or did you alter it?

      Delete
    2. This comment has been removed by the author.

      Delete
  5. Thanks for answering!

    Yes, I can connect the phone with the device without issues, but, when I try to send anything, the app crashes.

    The HC-06 is the original, I haven't altered the device.

    Any idea?

    ReplyDelete
    Replies
    1. I mean did you alter the example app that I created? I need to understand what you mean by send data, are you talking about the data in the app that tells the LED to turn on or off? If you added code or blocks to the app inventor app then I need to know what that is or how you are sending the data to understand what is going wrong.

      Delete
    2. Sorry for not understand you. I haven't been able to open your example, because I need the .apk and I haven't found this file in .aia

      Could you send me the file .apk? This is my email: oliva.ferlo@gmail.com

      I tell you about my project which it's similar your app and I need help.

      Thank you and sorry.

      Delete