Saturday, September 28, 2013

Connect Arduino With Bluetooth Module To Android Device


       I recently done a project with Arduino , on which i needed to communicate the Arduino with my Android phone via bluetooth interface, after referencing through the tutorials about the Bluetooth , i succeeded to implement the connectivity of  my phone and Arduino. So here am going to implement a simple example containing two buttons on the android to control the led connected in the led pin of Arduino.
   Here i used Arduino nano v3 micro-controller and HC-05 Bluetooth Module. The majority of the Arduino boards contain a LED connected to the pin 12.









    This is the circuit diagram for the connection between Arduino and Bluetooth module.




 Details about the Connections

  1. The RX pin of the Bluetooth Module is connected to TX pin of the Arduino.
  2. The TX pin of the Bluetooth Module is connected to RX pin of the Arduino.


      Here is the code for the Android mainActivity( MainActivity.java)


/*
 * Author : JITHINRAJ.P
 * Email  : jithinrajktd@gmail.com
 */


package com.example.bluetoothcontrole;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.UUID;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {
 private static final String TAG = "bluetooth1";
   private static final int REQUEST_CODE = 1234;
   private BluetoothAdapter btAdapter = null;
   private BluetoothSocket btSocket = null;
   private OutputStream outStream = null;
   ArrayList i ; 
    
   // SPP UUID service 
   private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
  
   // MAC-address of Bluetooth module 
   private static String address = "BC:77:37:E4:04:AC";

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  tryToConnect();
 }
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.activity_main, menu);
  return true;
 }
 
 public void tryToConnect()
 {
  
  btAdapter = BluetoothAdapter.getDefaultAdapter();
     
  checkBTState();//To check whwther the bluetooth is on,else turn on the bluetooth
    
   
     BluetoothDevice device = btAdapter.getRemoteDevice(address);
    
  try {
   btSocket = createBluetoothSocket(device);
  } catch (IOException e1) {
   errorExit("Fatal Error", "In onResume() and socket create failed: " + e1.getMessage() + ".");
  }
        
     btAdapter.cancelDiscovery();
     Log.d(TAG, "...Connecting...");
     try {
       btSocket.connect();
       Log.d(TAG, "...Connection ok...");
     } catch (IOException e) {
       try {
         btSocket.close();
       } catch (IOException e2) {
         errorExit("Fatal Error", "In onResume() and unable to close socket during connection failure" + e2.getMessage() + ".");
       }
     }
      
     Toast.makeText(getBaseContext(), "Connected", Toast.LENGTH_LONG).show();
     // Create a data stream so we can talk to server.
     Log.d(TAG, "...Create Socket...");
  
     try {
       outStream = btSocket.getOutputStream();
     } catch (IOException e) {
       errorExit("Fatal Error", "In onResume() and output stream creation failed:" + e.getMessage() + ".");
    }  
 } 
   private void sendData(String message) {
      byte[] msgBuffer = message.getBytes();   
      Log.d(TAG, "...Send data: " + message + "...");   
      try {
        outStream.write(msgBuffer);
      } catch (IOException e) {         
      }
    }
   
   private void checkBTState() {
      // Check for Bluetooth support and then check to make sure it is turned on
      // Emulator doesn't support Bluetooth and will return null
      if(btAdapter==null) { 
        errorExit("Fatal Error", "Bluetooth not support");
      } else {
        if (btAdapter.isEnabled()) {
          Log.d(TAG, "...Bluetooth ON...");
        } else {
          //Prompt user to turn on Bluetooth
          Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
          startActivityForResult(enableBtIntent, 1);
        }
      }
    }
   
   private BluetoothSocket createBluetoothSocket(BluetoothDevice device) throws IOException {
       if(Build.VERSION.SDK_INT >= 10){
           try {
               final Method  m = device.getClass().getMethod("createInsecureRfcommSocketToServiceRecord", new Class[] { UUID.class });
               return (BluetoothSocket) m.invoke(device, MY_UUID);
           } catch (Exception e) {
               Log.e(TAG, "Could not create Insecure RFComm Connection",e);
           }
       }
       return  device.createRfcommSocketToServiceRecord(MY_UUID);
   }
   private void errorExit(String title, String message){
      Toast.makeText(getBaseContext(), title + " - " + message, Toast.LENGTH_LONG).show();
      finish();
    }
   
  public void LedOn(View View)
  {
   sendData("1");
  }
  public void LedOff(View View)
  {
   sendData("0");
  }
}

Layout for the MainActivity(activity_main.xml)



    



The code for the arduino


char incomingByte;  // incoming data
int  LED = 12;      // LED pin
void setup() {
  Serial.begin(9600); // initialization
  pinMode(LED, OUTPUT);} 
void loop() {
  if (Serial.available() > 0) {  // if the data came
    incomingByte = Serial.read(); // read byte
    if(incomingByte == '0') {
       digitalWrite(LED, LOW);  // if 1, switch LED Off
    }
    if(incomingByte == '1') {
       digitalWrite(LED, HIGH); // if 0, switch LED on
    }
  }
}

1 comment: