Trouble sending data from Android to Arduino via Bluetooth

I'm trying to set up a system where an android app connects to Arduino via Bluetooth and tells it to either turn on or off its LED. I've looked through a lot of pages and source code and saw many people did it as I did but somehow my code isn't working and I cannot determine why.

Here's the entirety of my Arduino code, really simple and short.

#include <SoftwareSerial.h> 

SoftwareSerial Blue(0,1); // rx tx 

int LED = 13; // Led connected
char data;

char state = 0;


void setup()
{
pinMode(LED, OUTPUT); 
digitalWrite(LED, LOW);
Serial.begin(9600);
Blue.begin(9600);

}

void loop()
{
  while(Blue.available()==0);
  if(Blue.available()>0){    // read from android via bluetooth
    data = Blue.read();
    Serial.println(data);
  } 



if (data == '1') // If data is 1, turn ON the LED
{

  digitalWrite(LED,HIGH);
  Serial.println("LED ON ");
  }

if( data == '2') // if data is 2, turn OFF the LED
  {
  digitalWrite(LED,LOW);
  Serial.println("LED OFF");
  }
}

And here's a snippet of my android code that sends data to Arduino to control LED

switchLight.setOnClickListener(new View.OnClickListener() {  // button that will switch LED on and off
            @Override
            public void onClick(View v) {
                Log.i("[BLUETOOTH]", "Attempting to send data");
                if (mmSocket.isConnected() && btt != null) { //if we have connection to the bluetoothmodule
                    if (!lightflag) {
                        try{
                            mmSocket.getOutputStream().write("1".toString().getBytes());
                            showToast("on");
                        }catch (IOException e) {
                            showToast("Error");
                            // TODO Auto-generated catch block
                            e.printStackTrace();

                        }


                        //btt.write(sendtxt.getBytes());
                        lightflag = true;

                    } else {
                        try{
                            mmSocket.getOutputStream().write("2".toString().getBytes());
                            showToast("off");
                        }catch (IOException e) {
                            showToast("Error");
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }


                        //btt.write(sendtxt.getBytes());
                        lightflag = false;

                    }
                } 
                else {
                    Toast.makeText(MainActivity.this, "Something went wrong", Toast.LENGTH_LONG).show();
                }
            }
        });

This is the part of the code that connects to Arduino Bluetooth module. Again, fairly simple stuff and its only purpose are to connect to the module.

BluetoothAdapter bta;                 //bluetooth stuff
BluetoothSocket mmSocket;             //bluetooth stuff
BluetoothDevice mmDevice;             //bluetooth stuff

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i("[BLUETOOTH]", "Creating listeners");
        final TextView response = findViewById(R.id.response);
        Button switchLight = findViewById(R.id.switchlight);
        Button connectBT = findViewById(R.id.connectBT);

        connectBT.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                BluetoothSocket tmp = null;
                mmDevice = bta.getRemoteDevice(MODULE_MAC);
                Log.i("[BLUETOOTH]", "Attempting to send data");

                try {
                    tmp = mmDevice.createRfcommSocketToServiceRecord(MY_UUID);
                    mmSocket = tmp;
                    mmSocket.connect();
                    Log.i("[BLUETOOTH]","Connected to: "+mmDevice.getName());
                    showToast("Connected to: " + mmDevice.getName());
                }catch(IOException e){
                    try {mmSocket.close();
                    }catch(IOException c){return;}
                }
            }
        });

When I connect my android to the Arduino and track the serial monitor on Arduino IDE, instead of reading either 1 or 2, it reads something that looks like this:

This is produced using the Serial. println function in my Arduino code and I'm pretty sure it should display 1 or 2 but as you can see it does not. I've tried multiple workarounds like declaring it as int or char etc. If you can pinpoint any issue I'd much appreciate it.

SoftwareSerial Blue(0,1); // rx tx

Why are you using the hardware Serial pins with SoftwareSerial ?

UKHeliBob:

SoftwareSerial Blue(0,1); // rx tx

Why are you using the hardware Serial pins with SoftwareSerial ?

I used pin 0 and 1 because it said RX and TX next to them on the board. Am I not supposed to use them to connect RX and TX ports? I can change it to something else like Blue(2,3) but it does not change my output display on the serial monitor.

I used pin 0 and 1 because it said RX and TX next to them on the board

Yes, but they are the Rx and Tx for the hardware Serial interface which will almost certainly need to use for debugging using the Serial monitor. Even in your program you are printing to hardware Serial

I can change it to something else like Blue(2,3) but it does not change my output display on the serial monitor.

Then there is another problem. What baud rate does the Bluetooth adaptor on the Arduino run at ? Which Arduino board are you using ?

There was someone else asking about this type of project .
Hmmmm. Are you twins?

Anyway, use pins 2 and 3 or anything else other than 0 and 1 especially if you are using Serial Monitor for debugging which you will want to do.

Download and install a Bluetooth terminal app on your smartphone and use that to send a short string such as "Hi" to the Arduino + HC-05. Display the received string in the Serial Monitor.

If you are not able to get this working, you aren't ready for anything else.

.

UKHeliBob:
What baud rate does the Bluetooth adaptor on the Arduino run at ? Which Arduino board are you using ?

I'm using arduino uno. I've tried different rats but 9600 is the only rate where it displays anything on the monitor. But that might be because the monitor's baud rate is set at 9600.

ieee488:
Download and install a Bluetooth terminal app on your smartphone and use that to send a short string such as "Hi" to the Arduino + HC-05. Display the received string in the Serial Monitor.

If you are not able to get this working, you aren't ready for anything else.

.

When I use that app to send data, the arduino can receive it but the monitor still displays that weird broken character. By the way, when I cast it to hex int it displays FFFFFFFF. So basically any data I send is displayed as 255 as int or broken character as char.

I'm doing some more digging and found mentions of using a voltage divider to connect the bluetooth module and the arduino. Currently I have no resistors in my circuit. The RX of arduino connects directly to TX and vice versa, and the GND is connected to Arduino's GND and VCC is to arduino's 5V. Is this not right? Am I supposed to put in a resistor between any of these?

voltage divider for HC-05 RX <---> Arduino TX

Some of the info in these blog posts I wrote some time ago may be useful as background/how-to info.