You are on page 1of 2

First, create a new project in Android Studio and add the following code to the

MainActivity.java file:

import androidx.appcompat.app.AppCompatActivity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;
public class MainActivity extends AppCompatActivity {
private static final String HC05_ADDRESS = "00:14:03:05:13:26"; // Replace with
your HC05 module's address
private static final UUID HC05_UUID = UUID.fromString("00001101-0000-1000-8000-
00805F9B34FB"); // HC05 module usually uses this UUID
private BluetoothSocket socket;
private OutputStream outputStream;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button0 = findViewById(R.id.button0);
button0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendInstruction("0");
}
});
Button button1 = findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendInstruction("1");
}
});
Button button2 = findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendInstruction("2");
}
});
// Add buttons for the other instructions (3-9) here
}
private void sendInstruction(String instruction) {
try {
if (socket == null) {
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice device = adapter.getRemoteDevice(HC05_ADDRESS);
socket = device.createRfcommSocketToServiceRecord(HC05_UUID);
socket.connect();
outputStream = socket.getOutputStream();
}
outputStream.write(instruction.getBytes());
Toast.makeText(this, "Instruction sent: " + instruction,
Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "Failed to send instruction: " + instruction,
Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
try {
if (socket != null) {
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

This code defines a MainActivity class that displays buttons for each instruction
and sends the corresponding instruction over Bluetooth when the button is tapped.
You will need to replace the HC05_ADDRESS variable with the Bluetooth address of
your HC05 module.
Next, create a new layout file called activity_main.xml and add the following code:

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<Button
android:id="@+id/button0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"/>
<!-- Add buttons for the other instructions (3-9) here -->
</LinearLayout>

You might also like