Control an Arduino with Bluetooth

November 26, 2015 by Hariharan Mathavan

This article will show you how to set up two way communication between an Arduino and your Android Smartphone using Bluetooth.

Harness the power of Bluetooth to communicate with your Arduino!

This is part two of a series, which will focus on using Bluetooth to communicate with an Arduino. Bluetooth is one of the popular wireless communication technologies because of its low power consumption, low cost and a light stack but compensates on range. 


  • An Arduino
    Any model of the Arduino can be used, but all code and schematics in this article will be for the Uno.
  • An Android Smartphone with Bluetooth
    Check if your phone has Bluetooth by navigating to "Wireless and Networks" under settings.

  • HC-05 Bluetooth Module
    This module is the most popular Bluetooth module out there. The basic version is pretty inexpensive. The hassle of having to connect wires between the module and the Arduino can be avoided by using a Bluetooth Shield or a version in the Bee shape, the BTBee and a Bee shield.

  • Android Studio

  • USB cable for the Arduino

Setting Up the Hardware

To use the HC05 module, simply connect the VCC to the 5V output on the Arduino, GND to Ground, RX to TX pin of the Arduino, and vice versa. [Editor's note: The RX pin is not officially 5 V tolerant; it is designed for 3.3 V signals. The Arduino generates a 5 V signal, so you should use a voltage divider or logic-level translator to ensure that the Arduino signal does not damage the HC05.] If you are using the BTBee module with the shield, set the jumpers on the board so that the DOUT pin and D0 pins are shorted and DIN and D1 pins are shorted. This is done because the RX pin on the Arduino is Pin 0 and the TX pin is Pin 1. You are free to use any other pins as the RX and TX Pins, but you will have to use the SoftwareSerial Library of the Arduino to enable that. Do remember to remove the jumpers while uploading code to the Arduino if you have selected Pin 0 and 1 as Serial pins. 

Connecting the HC05 Module

Connecting the HC05 Module

Jumper connected to Pins 0 and 1 on the BTBee shield

Jumper connected to Pins 0 and 1 on the BTBee shield

If the module is being used for the first time, you'll want to change the name, passcode etc. To do this the module should be set to command mode. Connect the Key pin to any pin on the Arduino and set it to high to allow the module to be programmed. If you're using the BTBee, it's a little tricky. While the official wiki says that the Mode Button must be held to change it to Command Mode, and releasing it changes it back to Data Mode, it doesn't happen that way: instead, hold the Mode button, then quickly press and release the Reset button. You will notice that the Status LED blinks slower than usual.

To program the module, a set of commands known as AT commands are used. Here are some of them:

AT Check connection status.
AT+NAME ="ModuleName" Set a name for the device
AT+ADDR Check MAC Address
AT+UART Check Baudrate
AT+UART="9600" Sets Baudrate to 9600
AT+PSWD Check Default Passcode
AT+PSWD="1234" Sets Passcode to 1234

All the set commands return "OK" when they are executed successfully.

Here's some code to change the name. The jumpers are connected to 4 and 5 because the response from the module will be printed on the Serial Monitor. The complete code is available at the end of this article in the "download code" button. 

//If youre not using a BTBee connect set the pin connected to the KEY pin high
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(4,5); 
void setup() {
  String setName = String("AT+NAME=MyBTBee\r\n"); //Setting name as 'MyBTBee'
  BTSerial.print("AT\r\n"); //Check Status
  while (BTSerial.available()) {
  BTSerial.print(setName); //Send Command to change the name
  while (BTSerial.available()) {
void loop() {}

Programming the Arduino

No extra library is used to connect to the Bluetooth module because the RX and TX pins of the Arduino are shorted with those of the module. All data--outgoing and incoming--will have to go through the module. Interfacing the module is that easy.
To see how this works, let us connect a DHT-11 Temperature Sensor to the Arduino. When the letter "t" is received, the temperature, humidity, and heat index will be transmitted back. To use the DHT-11, the DHT library by Adafruit is used.

Using of DHT-11

The Shield and the connected DHT Sensor

The Shield and the connected DHT Sensor

Below is the code used to read data from the DHT sensor, process it, and send it via Bluetooth.

#include "DHT.h"
#define DHTPIN 2     
#define DHTTYPE DHT11  
void setup() {

void loop()
{  char c; 
   c =;  
void readSensor() {
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
  float hic = dht.computeHeatIndex(t, h, false);
  Serial.print("Humidity: ");
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(" *C ");
  Serial.print("Heat index: ");
  Serial.print(" *C ");

The Android App

Take a look at the previous article for a quick intro about Android apps and also the layout for this app.

Android apps layout

The program flow will be as illustrated above.

Editing AndroidManifest.xml

Since the extra hardware this app will be using is the onboard Bluetooth adapter, it will have to be mentioned in the Manifest.

uses-permission android:name="android.permission.BLUETOOTH" />

Connecting to the Device

First check for the presence of a Bluetooth Adapter.

        BluetoothAdapter bluetoothAdapter=BluetoothAdapter.getDefaultAdapter();

        if (bluetoothAdapter == null) {

            Toast.makeText(getApplicationContext(),"Device doesnt Support Bluetooth",Toast.LENGTH_SHORT).show();



If it is present, check if it's enabled. If it isn't enabled, ask the user permission to enable it.



            Intent enableAdapter = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

            startActivityForResult(enableAdapter, 0);


The device must be paired before the app can use it. Check the section below for details on how to pair the BT Module and the Phone. Now that the adapter is enabled, check for paired/bonded devices. Using the AT Commands from the section above, give an appropriate name to the device. Also find out its MAC Address. Here, the MAC addresses are being compared for a match because names are liable to be changed often.

Set bondedDevices = bluetoothAdapter.getBondedDevices();

if(bondedDevices.isEmpty()) {

     Toast.makeText(getApplicationContext(),"Please Pair the Device first",Toast.LENGTH_SHORT).show();

} else {

     for (BluetoothDevice iterator : bondedDevices) {

          if(iterator.getAddress().equals(DEVICE_ADDRESS)) //Replace with iterator.getName() if comparing Device names.


             device=iterator; //device is an object of type BluetoothDevice            



          } } } 

After getting the BluetoothDevice, a socket has to be created to handle the outgoing connection. Here a RFCOMM socket is used. RFCOMM--also known as Serial Port Profile--is essentially a Bluetooth protocol to emulate an RS232 cable.

socket = device.createRfcommSocketToServiceRecord(PORT_UUID); socket.connect();

Then get the input and output streams of the socket.



Reading Incoming Data

Since data can be received at any point of time, running a thread to listen for data would be best. First, the input stream is queried for available data. Then, the bytes are converted to human readable UTF-8 format and the text is send to a handler to post onto the UI. This is done because the UI can't be updated from background threads.

        int byteCount = inputStream.available();
        if(byteCount > 0)
            byte[] rawBytes = new byte[byteCount];
            final String string=new String(rawBytes,"UTF-8");
   Runnable() {
                    public void run()
<code>                        textView.append(string);

Transmitting Data

To send data, pass the String to the OutputStream.


Download the Source Code for the App


Testing the App

First off, pair the phone and the module by scanning for new devices in the Bluetooth tab of the System Settings. The name which was set earlier should appear. Put in the set passcode (Default is either 0000 or 1234).

Bluetooth Pairing Request

After you power on the Arduino, you may notice that the BTBee's status LED blinks periodically. Now open the app and tap Begin. The status LED must go off and the Conn LED will glow. This shows that a connection has been established.

The BTBee's status LED blinks periodically

When "t" is sent to the Arduino, it replies with the Temperature, Humidity, and Heat Index.


That was a simple exhibition of data transfer using Bluetooth. Note that you can also use an Arduino Bluetooth serial terminal app if you wish to not use the app provided here. Bluetooth can be used to control the automation of many everyday things, like switching a lightbulb on, which can be done by using a Relay with the Arduino. This concludes this two-part series on interfacing an Arduino with an Android phone. The first part on using a Serial Cable can be found here.

Give this project a try for yourself! Get the BOM.

  • B
    briantee March 07, 2016

    When i try to compile app its gradle says:

    Error:(1, 0) Plugin with id ‘’ not found.
    Open File

    Like. Reply
    • Đạt Tiến Nguyễn October 29, 2016
      @briantee: just upgrade to appcombat, gradle to lastest version. It's work fine buildscript { repositories { jcenter() // or mavenCentral() } dependencies { classpath '' } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile '' }
      Like. Reply
  • F
    fredy67 March 24, 2016

    hi, I had this same error,,add the following code to the top of your gradle build, that must solve the problem

    buildscript {
    repositories {
    jcenter() // or mavenCentral()

      dependencies {
    classpath ''

    Like. Reply
  • Rimish Bansod April 05, 2016

    Can we use simple board instead of arduino board with same bluetooth module and android app ?
    Here in android app, we have to enter mac id of Bluetooth module manually in code.
    Instead of entering manually, how to select bluetooth module from list of available device and then fetch its mac id and then connect

    Like. Reply
    • S
      SensorMatty30 July 10, 2017
      I need the same info, but i think we should to add the "standard code" (from others tutorials) for make a list of available devices in this codelist...
      Like. Reply
    • S
      SensorMatty30 July 10, 2017
      I need the same info, but i think we should to add the "standard code" (from others tutorials) for make a list of available devices in this codelist...
      Like. Reply
    • S
      SensorMatty30 July 10, 2017
      I need the same info, but i think we should to add the "standard code" (from others tutorials) for make a list of available devices in this codelist...
      Like. Reply
    • S
      SensorMatty30 July 10, 2017
      I need the same info, but i think we should to add the "standard code" (from others tutorials) for make a list of available devices in this codelist...
      Like. Reply
  • _
    _Rich August 18, 2016

    I tried this recently and have the USB OTG (part 1) working fine - I needed to remove the check for the Arduino vendorID (a bit unsecure, but hey it wasn’t accepting the listed vendorID or any variation thereof).

    When I came to use the Bluetooth version, the app would do nothing when the begin button was pressed. I strongly suspect it is because the permissions have changed in Android 6. Here is a potential answer on Stackoverflow that provides some more details:

    Like. Reply
  • B
    Bioluminobay August 24, 2016

    Hi Hariharan,
    If you were to program an app that interacts with the arduino, what language would you use?

    Like. Reply
  • T
    thomasss January 02, 2017

    Is there someone who has done this with wifi? I’m trying to connect an android app (with Xamarin in Visual Studio)  with my arduino using a wifi module (ESP8266). But I don’t know how to start.

    Like. Reply
  • F
    ffefwf February 09, 2017

    Excuse me
    I already have a connection to Bluetooth.
    Why did I press BEGIN without any response?
    Thank You

    Like. Reply
  • Kristián Seman 1 February 13, 2017

    This line is causing my a problem

    byteCount = inputStream.available();

    Error is that i try to invoke virtual method a than close on null
    (attempt-to-invoke-virtual-method-void-java-io-bufferedreader-close-on-a-null) can you help me ? thank you

    Like. Reply
  • Bryan C. January 04, 2018

    I don’t see a voltage divider on your hookup diagram for the receiver input. The bluetooth module wants 3.3volts on that pin. The spec sheets call for using a divider or you could burn out your module when sending 5v direct from arduino.

    Like. Reply
    • RK37 January 08, 2018
      I agree, the Rx pin is designed for 3.3 V and is not officially 5 V tolerant. Perhaps this is one of those situations where you can do the wrong thing and still have a functional device, at least temporarily. I don't think that the original author is available to modify the article, so I will add a note in the "Setting Up the Hardware" section.
      Like. Reply
  • Efe ofluoğlu 2 January 31, 2018

    how can I change this so it is just one button to send a “on” and “off” data (possibly boolean true and false) instead of strings?

    Like. Reply
  • Tamoghno Das March 29, 2018

    Nothing happens after I send the ‘t’. It is unable to fetch the sensor readings.

    Like. Reply
  • Tamoghno Das March 29, 2018

    Please help

    Like. Reply
  • Salim Orn June 12, 2018

    I need Help please
    I open the app and push the Button Begin. Then the app wants me to turn on Bluetooth. I push OK and it turns on, my Phone connects with my Bluetoothmodule. But nothing happens. I cant push the Buttons SEND and STOP.
    There is no Text what tells me that my Device is Connected. I would be happy if someone can help me.

    Like. Reply
    • Rich Ung September 30, 2018
      I have the same issue, mine only have the begin and clear buttons highlighted, the send and stop buttons are grayed out. When I touch begin button, it has absolute no response. The bluetooth on my phone is enabled and paired, Can somebody pleas tell me what the problem is? I use a work HC-05.
      Like. Reply