Bluetooth Communication between Arduino Nano and Android

manu kj
3 min readJul 1, 2019

--

This is a Tutorial which teaches how to communicate between Android App and Arduino Nano

1:Connection

2:Arduino code

3:Android Code

Connection

Arduino Code

char c;void setup() {
Serial.begin(9600);
}
void loop() {
while (!Serial.available() ) ;//wait until serial is available
c = Serial.read();
Serial.print(c);
}

Android Code

define a Bluetooth Class in Android Studio which

— Creates a Bluetooth Socket

— Sends data Through the Socket

Two things are needed to make a connection MAC address, of HC05 .
Service ID or UUID. where UUID = 00001101–0000–1000–8000–00805F9B34FB

NOTE change the MAC address in the below code to the MAC address of HC05

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.UUID;
public class BluetoothClass {
private BluetoothAdapter mbluetoothAdapter;
private BluetoothSocket mbtSocket = null;
private DataOutputStream moutStream = null;
private DataInputStream minputStream = null;
//Replace the MAC address of HC05
private static String maddress = "00:00:00:00:00:00";
private static final String TAG = "SocketBlueTooth";
private static final int REQUEST_ENABLE_BT = 1;
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
//Constructor
public BluetoothClass(BluetoothAdapter bluetoothAdapter) {
mbluetoothAdapter = bluetoothAdapter;
}
//Checking for the Bluetooth Adaptor and enabling it if not enabled
public void checkBTState(){
// Check for Bluetooth support and then check to make sure it is turned on
Log.d(TAG, "...Checking Bluetooth Adaptor...");
// if the Device doesn't support Bluetooth and will return null
if(mbluetoothAdapter==null) {
Log.d(TAG,"Fatal Error Bluetooth Not supported. Aborting.");
} else {
//if enabled else enable the bluetooth
if (mbluetoothAdapter.isEnabled()) {
Log.d(TAG, "...Bluetooth is enabled...");
} else {
mbluetoothAdapter.enable();
Log.d(TAG, "...Bluetooth is enabled...");
}
}
}
//Disconnect at last
public void disconnect()
{
try {
if(minputStream!= null && moutStream!=null && mbtSocket!=null) {
minputStream.close();
moutStream.close();
mbtSocket.close();
}
mbluetoothAdapter.disable();
} catch (IOException e) {
e.printStackTrace();
}
}


//Creating a Socket to establish a connection
public void createSocket() throws Exception{

// Set up a pointer to the remote node using it's address.
BluetoothDevice device = mbluetoothAdapter.getRemoteDevice(maddress);
try {
mbtSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
Log.d(TAG,"Fatal Error In onResume() and socket create failed: " + e.getMessage() + ".");
}
// Discovery is resource intensive. Make sure it isn't going on
// when you attempt to connect and pass your message.
mbluetoothAdapter.cancelDiscovery();
// Establish the connection. This will block until it connects.
Log.d(TAG, "...Connecting to Remote...");
try {
mbtSocket.connect();
Log.d(TAG, "...Connection established and data link opened...");
} catch (IOException e) {
Log.d(TAG,"Fatal Error In onResume() and unable to close socket during connection failure" + e.getMessage() + ".");
}
initializeIOStream();
}
//Initializing the Input and Output Stream
private void initializeIOStream() {
try
{
minputStream = new DataInputStream(mbtSocket.getInputStream());
moutStream = new DataOutputStream(mbtSocket.getOutputStream());
}catch (IOException e){}
}

//sending data to Arudino
public void sendData(String message) {
byte[] msgBuffer = message.getBytes();
Log.d(TAG, "...Sending data: " + message + "...");
try {
moutStream.write(msgBuffer);
}catch (IOException e)
{
String msg = "In onResume() and an exception occurred during write: " + e.getMessage();
msg = msg + ".\n\nCheck that the SPP UUID: " + MY_UUID.toString() + " exists on server.\n\n";
Log.d(TAG,msg);
}
}
}

Once the BluetoothClass is created, we first need to create the object of class and the call createSocket and sendData

for eg:

BlueetoothClass blueetoothClass = new BlueetoothClass(BluetoothAdapter.getDefaultAdapter());blueetoothClass.createSocket();
blueetoothClass.sendData("Send Data");

--

--