Transfer data to arduino via bluetooth

I am planning to transfer data between arduino and a mobile device. Right now, I can read data from arduino in mobile device, but can't send data to the arduino board. here is the code I am using for data transfer:

Android Code:

 void sendData() throws IOException {
 	    String msg = myTextbox.getText().toString();
 	    msg += "\n";
 	    mmOutputStream.write(msg.getBytes());
 	    //mmOutputStream.write('A');
 	    myLabel.setText("Data Sent"+msg.getBytes()); 	   }

Arduino Code:

  SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
          void loop() { 	 
          char aChar = bluetooth.read();
            Serial.print(aChar); 
          }

I would appreciate it if anyone could help me to solve this problem.

It would help if you posted the full code.

Software Serial Example:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX

void setup()  
{
  // Open serial communications and wait for port to open:
  Serial.begin(57600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  Serial.println("Goodnight moon!");

  // set the data rate for the SoftwareSerial port
  mySerial.begin(4800);
  mySerial.println("Hello, world?");
}

void loop() // run over and over
{
  if (mySerial.available())
    Serial.write(mySerial.read());
  if (Serial.available())
    mySerial.write(Serial.read());
}

Thanks for the reply. I tired to change the code in the LOOP, but it didn`t work. Should I also try with changing the baud rate?
here is my current code:

int bluetoothTx = 2;  // TX-O pin of bluetooth mate, Arduino D2
int bluetoothRx = 10;  // RX-I pin of bluetooth mate, Arduino D10

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup()
{
  Serial.begin(9600);  // Begin the serial monitor at 9600bps
  bluetooth.begin(115200);  // The Bluetooth Mate defaults to 115200bps
  bluetooth.print("$");  // Print three times individually
  bluetooth.print("$");
  bluetooth.print("$");  // Enter command mode
  delay(100);  // Short delay, wait for the Mate to send back CMD
  bluetooth.println("U,9600,N");  // Temporarily Change the baudrate to 9600, no parity
  bluetooth.begin(9600);  // Start bluetooth serial at 9600
}

void loop() 
{ 	 
 char aChar = bluetooth.read();
 Serial.print(aChar); 
}

I am using Arduino Pro Micro and Bluetooth RF Transceiver Module serial RS232 TTL HC-05.

Try the example I gave you.

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx); //It should be Rx, Tx.
How is it wired and what voltage are you supplying the BT module?

Thanks. I changed the Rx and Tx pin, but it didn't work (I was not getting any data in mobile device).

Here is the pin layout:

Bluetooth VCC- Arduino 3.3V
Bluetooth Tx - Arduino Digital pin 2
Bluetooth Rx - Arduino Digital pin 10
Bluetooth GND - Arduino Ground

Does you module have a connection indicator? Try a terminal program like RealTerm or Putty.

I think the connection is working nicely. I tired to read potentiometer values from arduino board to mobile device and it was working nicely. it's not sending data from the mobile to arduino board.

Did you make the android code yourself, if so, let me take a look at it. I'm wondering if its the UUID that is the issue,or if you even coded it correctly in the first place.

here is my code. Basically I found it online and modifying according to my need.

package com.example.readbluetooth;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.UUID;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity
{
	TextView myLabel, txtDebug;
	  EditText myTextbox;
	  BluetoothAdapter mBluetoothAdapter;
	  BluetoothSocket mmSocket;
	  BluetoothDevice mmDevice;
	  OutputStream mmOutputStream;
	  InputStream mmInputStream;
	  Thread workerThread;
	  byte[] readBuffer;
	  int readBufferPosition;
	  int counter;
	  volatile boolean stopWorker;
	  

	  @Override
	  public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.activity_main);

	    Button openButton = (Button)findViewById(R.id.open);
	    Button sendButton = (Button)findViewById(R.id.send);
	    Button closeButton = (Button)findViewById(R.id.close);
	    myLabel = (TextView)findViewById(R.id.label);
	    myTextbox = (EditText)findViewById(R.id.entry);
	    txtDebug = (TextView)findViewById(R.id.debug);

	    //Open Button
	    openButton.setOnClickListener(new View.OnClickListener() {
	      public void onClick(View v) {
	        try {
	          findBT();
	          openBT();
	        }
	        catch (IOException ex) { }
	      }
	    });

	    //Send Button
	    sendButton.setOnClickListener(new View.OnClickListener() {
	      public void onClick(View v) {
	        try {
	          sendData();
	        }
	        catch (IOException ex) {
	            showMessage("SEND FAILED");
	        }
	      }
	    });

	    //Close button
	    closeButton.setOnClickListener(new View.OnClickListener() {
	      public void onClick(View v) {
	        try {
	          closeBT();
	        }
	        catch (IOException ex) { }
	      }
	    });
	  }

	  void findBT() {
	    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
	    if(mBluetoothAdapter == null) {
	      myLabel.setText("No bluetooth adapter available");
	    }

	    if(!mBluetoothAdapter.isEnabled()) {
	      Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
	      startActivityForResult(enableBluetooth, 0);
	    }

	    Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
	    //mmDevice = mBluetoothAdapter.getRemoteDevice("00:06:66:66:24:09");	//Sparkfun
	    mmDevice = mBluetoothAdapter.getRemoteDevice("00:14:02:10:51:08");		//ebay
	    if (pairedDevices.contains(mmDevice))
	       {
	    	myLabel.setText("Bluetooth Device Found, address: " + mmDevice.getAddress() );
	           Log.d("ArduinoBT", "BT is paired");
	       }
	    
	    //myLabel.setText("Bluetooth Device Found");
	  }

	  void openBT() throws IOException {
	    UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); //Standard //SerialPortService ID
	    mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);    
	    mmSocket.connect();
	    mmOutputStream = mmSocket.getOutputStream();
	    mmInputStream = mmSocket.getInputStream();
	    beginListenForData();
	    myLabel.setText("Bluetooth Opened");
	  }

	  void beginListenForData() {
	    final Handler handler = new Handler(); 
	    final byte delimiter = 10; //This is the ASCII code for a newline character
	    stopWorker = false;
	    readBufferPosition = 0;
	    readBuffer = new byte[1024];
	    workerThread = new Thread(new Runnable() {
	      public void run() {
	         while(!Thread.currentThread().isInterrupted() && !stopWorker) {
	          try {
	            int bytesAvailable = mmInputStream.available();            
	            if(bytesAvailable > 0) {
	              byte[] packetBytes = new byte[bytesAvailable];
	              mmInputStream.read(packetBytes);
	              for(int i=0;i<bytesAvailable;i++) {
	                byte b = packetBytes[i];
	                if(b == delimiter) {
	                  byte[] encodedBytes = new byte[readBufferPosition];
	                  System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
	                  final String data = new String(encodedBytes, "US-ASCII");
	                  readBufferPosition = 0;
	                  
	                  final int[] rawData = new int[3];	
	                	  final String[] separated = data.split(":");
	                	  //Reading 3 analog input sent from Arduino (Potentiometer and Joystick XY)
	                	  if ((separated.length >= 3) && (data.contains(":")))
	                	  {
	                		  rawData[0] = Integer.parseInt(separated[0]);
	                		  rawData[1] = Integer.parseInt(separated[1]);
	                		  rawData[2] = Integer.parseInt(separated[2]);
	                	  }
	                	  
	                	  handler.post(new Runnable() {
			                    public void run() {
			                    		txtDebug.setText("Data 1: "+rawData[0] + "\nData 2: "+rawData[1] + "\nData 3: " + rawData[2]);
			                      
			                    }
			                  });
	                }
	                else {
	                  readBuffer[readBufferPosition++] = b;
	                }
	              }
	            }
	          } 
	          catch (IOException ex) {
	            stopWorker = true;
	          }
	         }
	      }
	    });

	    workerThread.start();
	  }


	  void sendData() throws IOException {
	    String msg = myTextbox.getText().toString();
	    msg += "\n";
	    mmOutputStream.write(msg.getBytes());
	    myLabel.setText("Data Sent"+msg.getBytes());
	  }

	  void closeBT() throws IOException {
	    stopWorker = true;
	    mmOutputStream.close();
	    mmInputStream.close();
	    mmSocket.close();
	    myLabel.setText("Bluetooth Closed");
	  }

	  private void showMessage(String theMsg) {
	        Toast msg = Toast.makeText(getBaseContext(),
	                theMsg, (Toast.LENGTH_LONG)/160);
	        msg.show();
	    }
	}

Basically I found it online and modifying according to my need.

That never usually works out correctly. Try the example program BluetoothChat and see if that allows you to connect to your BT module.

I changed the pin to:

int bluetoothTx = 8;  // TX-O pin of bluetooth mate, Arduino D8
int bluetoothRx = 10;  // RX-I pin of bluetooth mate, Arduino D10

Now it's working porperly.