Creating Bluetooth Android App to Control Arduino Board

Droiduino Blog
The Startup
Published in
6 min readSep 17, 2020

--

This tutorial is about creating your custom Android app to connect with an Arduino board using Bluetooth. Consequently, some basic prior knowledge of Android programming is required to follow this tutorial.

But don’t worry, if you don’t have basic knowledge on Android programming but still want to create your own Bluetooth app, then you can take the Basic Android Programming for Arduino Makers that is available in Udemy. You will learn how to create a Bluetooth app that can talk with your Arduino board from scratch and without prior knowledge in Android programming required.

The codes presented in this tutorial is the minimum codes that enable an Android phone and Arduino board to send and receive messages (that can be translated into commands) with each other through Bluetooth.

Development environment

To manage some expectations in case the app doesn’t work like it’s supposed to be, this is the environment I use to develop this app:

  1. Samsung Galaxy S8, with Android version 9.
  2. Android Studio version 3.6.3 with compatible Gradle version.
  3. Minimum SDK Version: 19 (You need to select this when creating a new project using Android Studio).
  4. Mac OS 10.15.4 (Windows machines also works perfectly)

How this app works

This app will create a Bluetooth connection with a nearby Arduino board that has been connected with the HC05 Bluetooth module. It is created to be compatible with Arduino board from this tutorial. However, it is easy to modify the codes so that it can be used together with Arduino boards with different configurations.

Simple Bluetooth App

To test Bluetooth connection functionality, you can press the button on this App to control a built-in LED on the Arduino board. Once a predefined command message is received from Android, Arduino will transmit a return message to Android as a status message.

Creating Bluetooth Connection on Android

Before we dive into the coding part, I would like to describe the step by step flow to create a Bluetooth connection on Android. This is a summary of the more detailed documentation from Google.

  1. Initialize the default Bluetooth adapter (device) on your Android phone.
  2. Get the MAC Address from the remote device that you are connecting to. In this case, the MAC Address of HC05 Bluetooth module connected to Arduino board.
  3. Create a separate thread in your code to initiate a connection using the MAC Address that we previously obtained. This thread will manage what happens if a connection is successfully established or failed to be established. It also handles if we want to close the Bluetooth connection.
  4. Once a connection is successfully established, the thread will do callback for the codes that manage data exchange (transmit and receive between 2 devices). For this, we need to create another thread.
  5. This thread will read incoming data transmission and parse it if necessary (or you can parse it elsewhere on the code) and transmit the message or command that is generated by the Android app.

Now, the flow above needs to be translated into codes.

Creating Activities and Java Class

Create a new project with the empty activity template and select the appropriate name for your app. For this app we will create 2 activities and 2 Java classes :

  1. MainActivity. This is automatically created when you create a new project. This is where most of the interactions take place.
  2. SelectDeviceActivity. The UI where you select the Bluetooth device that you want to connect.
  3. DeviceListAdapter. A class to display a list of paired Bluetooth devices for you to connect. The list will be displayed in SelectDeviceActivity.
  4. DeviceInfoModel. A class that acts as a placeholder for the remote device information.

AndroidManifest.xml

Once you created all the activities and classes above, your AndroidManifest.xml file will look something like this :

Please note that you should add the Bluetooth permission so that you can access your phone’s Bluetooth device.

MainActivity Layout

MainActivity is the main UI where you can interact with the interfaces that will connect you to a remote Bluetooth device and control it.

Main Screen Layout

The XML code for the layout above is like this:

SelectDeviceActivity Layout

This activity will display a list of remote Bluetooth devices that are already paired with your phone. It is shown when the “Connect” button is clicked on the MainActivity. The layout XML code for this activity is as follows:

MainActivity Code

Now we continue with the code. Code in MainActivity is the one creating a Bluetooth connection to a remote device. You can just copy and paste the code below to your project. Some comments have been added to the code you can understand it better.

I make some minor style changes to the color resources.

SelectDeviceActivity, DeviceListAdapter and DeviceInfoModel

This activity works with DeviceListAdapter class and DeviceInfoModel class to display the list of paired devices.

SelectDeviceActivity.java

DeviceListAdapter.java

DeviceInfoModel.java

The whole Android project is also available on Github.

Once you build the whole project, you need to install the app to your actual device to be able to use the Bluetooth function.

Note: Due to periodic update to the app, the Github version might slightly different from the code in this post. But the core function remains the same.

Modifying the Arduino Code

We need to make some small modifications to the Arduino code from this tutorial. You can create a new Arduino sketch and copy-paste the code below.

Don’t forget to compile and upload the code to your Arduino board. You can also go to Github to get this code.

Connecting Android to Arduino…

Follow the steps below to connect your phone to the Arduino board

  1. Connect HC05 module to the Arduino board as described in the previous post then connect it to a power supply. The LED on HC05 module should be blinking fast.
  2. Activate Bluetooth on your phone and pair HC05 with your phone. The device name you’re looking for is “HC-05”. You don’t need your app yet for pairing with HC05.
  3. Now open your app and press the “Connect” button. A list of paired devices will be shown on screen. If you change the device name when you’re pairing with HC05, you actually only change the alias name. Your app only shows the device name, so select “HC-05”.
  4. Once HC05 is connected, there will be status on the toolbar and the LED button will be enabled. You know when HC05 is connected when the LED on HC05 blinks slowly.

…And Test the Data Exchange

Now, you can press the LED button and watch the built-in LED on Arduino turned on and off. Visually, you can also see the light bulb image on your phone change color to indicate LED status. The color changes when your phone receives the status message from Arduino, so you might notice some delay between the change on LED on Arduino with the light bulb image on your phone.

What’s next

You have now understood one of the methods to connect your Android phone to an Arduino board. Exciting new possibilities are now available for you. For example, you can:

  1. Improve the app so that it shows alert when Bluetooth function on your phone is not yet activated.
  2. Add more codes on Arduino so now your app can control 2 LEDs
  3. Connect a simple sensor on Arduino e.g. proximity sensor and read the values on your phone.
  4. And many more. If you can imagine it, you can create it.

If you find this tutorial is too difficult to follow or if you don’t have prior knowledge in Android programming, then you can always take the course Basic Android Programming For Arduino Makers in Udemy which will guide you to create your own Bluetooth app from scratch.

--

--

Droiduino Blog
The Startup

Droiduino is about sharing knowledge in the realm of Android app programming, Arduino project creation and using R for processing data.