Home » Android » java – Trouble sending data from Android to Arduino via Bluetooth-Exceptionshub

java – Trouble sending data from Android to Arduino via Bluetooth-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

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:

enter image description here

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.

How to&Answers: