How to Connect an AVR Microcontroller to an Android Tablet Using the HC-05 Bluetooth Module.
At the end of the previous article I wrote that I had managed to create an interface for Android, and in this article I'll try to describe the process. Why "try"? Because I have almost no experience creating Android apps, and in truth there's little I can explain clearly. Okay, enough excuses, time to get down to business.
First, download and install Android Studio; there is plenty of material online on how to do this. Once the development environment is installed, create a new project following the screenshots.
Once the project is created, the first thing to do is modify the AndroidManifest by adding two lines requesting permissions.
Next, in activity_my we create several buttons, give them ids and assign the methods that will be called when they are pressed.
Here's what it looks like on the screen.
The buttons can be named however you like.
The most interesting part is left: learning to establish a Bluetooth connection and handle button presses.
At the beginning of the previous article I wrote that when I bought the bluetooth module I didn't yet know what to use it for, but I knew for sure that such a thing would come in handy. A little time passed and a use was found... In short, I have a project in which an MCU controls stepper motors, and all this stuff is on the balcony. In winter the balcony is cold and no fun, so it's more convenient to debug it from a warm room, and that's what I used this module for.
The Atmega16 code for receiving characters from the tablet was taken from the previous article.
I'll leave the Android Studio project in the archive Bluetooth_Interface_Example.rar.
First, download and install Android Studio; there is plenty of material online on how to do this. Once the development environment is installed, create a new project following the screenshots.
Once the project is created, the first thing to do is modify the AndroidManifest by adding two lines requesting permissions.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="domain.my.bluetooth_interface_example" >
//request the permissions the app needs to work
//BLUETOOTH_ADMIN is needed when you need to change some adapter properties
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MyActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Next, in activity_my we create several buttons, give them ids and assign the methods that will be called when they are pressed.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MyActivity">
<Button
android:layout_width="165dp"
android:layout_height="wrap_content"
android:text="increase_diametr"
android:id="@+id/increase_diametr_bt"
android:onclick="onclick"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp" />
<Button
android:layout_width="160dp"
android:layout_height="wrap_content"
android:text="back"
android:id="@+id/back_bt"
android:onclick="onclick"
android:layout_below="@+id/increase_diametr_bt"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="33dp"
android:layout_marginTop="97dp" />
<Button
android:layout_width="160dp"
android:layout_height="wrap_content"
android:text="forward"
android:id="@+id/forward_bt"
android:onclick="onclick"
android:layout_alignTop="@+id/back_bt"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="43dp" />
<Button
android:layout_width="160dp"
android:layout_height="wrap_content"
android:text="reduce_diametr"
android:id="@+id/reduce_diametr_bt"
android:onclick="onclick"
android:layout_below="@+id/back_bt"
android:layout_alignRight="@+id/increase_diametr_bt"
android:layout_alignEnd="@+id/increase_diametr_bt"
android:layout_marginTop="91dp" />
<Button
android:layout_width="165dp"
android:layout_height="wrap_content"
android:text="Connect"
android:id="@+id/connect_bt"
android:onclick="connect"
android:layout_below="@+id/reduce_diametr_bt"
android:layout_alignLeft="@+id/back_bt"
android:layout_alignStart="@+id/back_bt"
android:layout_marginTop="103dp"
android:background="#ff59ffd1"
android:layout_alignParentStart="false" />
</RelativeLayout>
Here's what it looks like on the screen.
The buttons can be named however you like.
The most interesting part is left: learning to establish a Bluetooth connection and handle button presses.
package domain.my.bluetooth_interface_example;
import java.io.OutputStream;
import java.lang.reflect.Method;
import android.util.Log;
import android.content.Intent;
import android.view.View;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.os.Bundle;
import android.widget.Toast;
public class MyActivity extends Activity {
BluetoothSocket clientSocket;
//this method is called when the app is created or restarted
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
}
public void connect(View v)
{
String enableBT = BluetoothAdapter.ACTION_REQUEST_ENABLE;
startActivityForResult(new Intent(enableBT), 0);
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
boolean connection = true;
try
{
// 10:14:05:26:01:66 - the module address; you can see it in the connection settings when pairing with a PC
BluetoothDevice device = bluetoothAdapter.getRemoteDevice("10:14:05:26:01:66");
//initiate the connection with the device
Method m = device.getClass().getMethod(
"createRfcommSocket", new Class[]{int.class});
clientSocket = (BluetoothSocket) m.invoke(device, 1);
clientSocket.connect();
}
catch (Exception e)
{
//write errors to the log
Log.d("BLUETOOTH", e.getMessage());
//show a message that the connection failed
Toast.makeText(getApplicationContext(), "failed_to_connect", Toast.LENGTH_LONG).show();
connection = false;
}
if (connection) {
//show a message about the successful connection
Toast.makeText(getApplicationContext(), "CONNECTED", Toast.LENGTH_LONG).show();
}
}
public void onclick(View v) {
byte value = 0;
//try to send data
try
{
//get the output stream for data transfer
OutputStream outStream = clientSocket.getOutputStream();
//check which button was pressed
switch (v.getId())
{
case R.id.forward_bt: value = 's'; break;
case R.id.back_bt: value = 'w'; break;
case R.id.increase_diametr_bt: value = 'r'; break;
case R.id.reduce_diametr_bt: value = 'f'; break;
}
//write data to the output stream
outStream.write(value);
}
catch (Exception e)
{
//if there are errors, write them to the log
Log.d("BLUETOOTH", e.getMessage());
//and show a message that the connection is not established
Toast.makeText(getApplicationContext(), "not_connected", Toast.LENGTH_LONG).show();
}
}
}
At the beginning of the previous article I wrote that when I bought the bluetooth module I didn't yet know what to use it for, but I knew for sure that such a thing would come in handy. A little time passed and a use was found... In short, I have a project in which an MCU controls stepper motors, and all this stuff is on the balcony. In winter the balcony is cold and no fun, so it's more convenient to debug it from a warm room, and that's what I used this module for.
The Atmega16 code for receiving characters from the tablet was taken from the previous article.
I'll leave the Android Studio project in the archive Bluetooth_Interface_Example.rar.




