# Problems in receiving data of android to bluetooth

**URL:** <https://forum.arduino.cc/t/problems-in-receiving-data-of-android-to-bluetooth/862272>\
**Category:** General Guidance\
**Created:** [May 15, 2021, 11:17pm UTC](https://forum.arduino.cc/t/problems-in-receiving-data-of-android-to-bluetooth/862272 "2021-05-15T23:17:56Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmps98](https://avatars.discourse-cdn.com/v4/letter/j/ecc23a/32.png) [@jmps98](https://forum.arduino.cc/u/jmps98)\
**Post date:** [May 15, 2021, 11:17pm UTC](https://forum.arduino.cc/t/problems-in-receiving-data-of-android-to-bluetooth/862272/1 "2021-05-15T23:17:56Z")

</div>

I don´t know what is the problem with my code, the android app is fine, I tested the same app with other arduino code and it runs, turn on and off the lights at the pins 10 and 9.

When I active the buttons in the app and the app send the characters to bluetooth, the arduino do nothing.

#include \<SoftwareSerial.h\>

SoftwareSerial Bluetooth(3, 4); // RX TX

String dataIn =""; //variable para almacenar el comando enviado por el bluetooth

#define resistencia 9  
#define bomba 10

// variables para contar el tiempo entre procesos, modo automatico.  
unsigned long tiempo1=0;  
unsigned long tiempo2=0;

int segundosResis=0;  
int minutosResis=0;

int segundosBomba=0;  
int minutosBomba=0;

void setup() {  
// put your setup code here, to run once:  
Bluetooth.begin(38400);  
Bluetooth.setTimeout(1);

pinMode(resistencia, OUTPUT);  
pinMode(bomba, OUTPUT);

// INICIALIZADA DE LOS ESTADOS DE RESISTENCIA Y BOMBA  
digitalWrite(resistencia, LOW);  
digitalWrite(bomba, LOW);

}

void loop() {  
// put your main code here, to run repeatedly:

if (Bluetooth.available()\>0)  
{  
dataIn = Bluetooth.readString(); // Read the data as string

```
if (dataIn=="A") 

```

{  
digitalWrite(resistencia, HIGH); // ESTADO A, RESISTENCIA PRENDIDA  
}

```
if (dataIn=="B") 

```

{  
digitalWrite(bomba, HIGH); // ESTADO B, BOMBA PRENDIDA  
}

if (dataIn == "C")  
{  
digitalWrite(bomba, LOW); // ESTADO C, BOMBA Y RESISTENCIA APAGADA  
digitalWrite(resistencia, LOW);  
}

if (dataIn == "D")  
{  
digitalWrite(resistencia, LOW); // ESTADO D, RESISTENCIA APAGADA  
}

```
if (dataIn == "E") 

```

{  
digitalWrite(bomba, LOW); // ESTADO E, BOMBA APAGADA  
}

```
// Se toman los datos enviados por el bluetooth en las casillas de tiempos de bomba y de resistencia
 if(dataIn.startsWith("SR"))
 {
    String dataInS= dataIn.substring(2, dataIn.length());
    segundosResis= dataInS.toFloat();
    
  }

  if(dataIn.startsWith("MR"))
 {
    String dataInS= dataIn.substring(2, dataIn.length());
    minutosResis= dataInS.toFloat();
    
  }
 
 if(dataIn.startsWith("SB"))
 {
    String dataInS= dataIn.substring(2, dataIn.length());
    segundosBomba= dataInS.toFloat();
    
  }

  if(dataIn.startsWith("MB"))
 {
    String dataInS= dataIn.substring(2, dataIn.length());
    minutosBomba= dataInS.toFloat();
    
  }

  // se convierten los tiempos a segundos. 
  long tiempoResis= minutosResis*60+segundosResis;
  long tiempoBomba= minutosBomba*60+segundosBomba;

  long tiempoTotal=tiempoResis+tiempoBomba;

```

}  
}

---

<div class="post-metadata">

**Author:** ![groundFungus](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@groundFungus](https://forum.arduino.cc/u/groundFungus)\
**Post date:** [May 16, 2021, 12:07am UTC](https://forum.arduino.cc/t/problems-in-receiving-data-of-android-to-bluetooth/862272/2 "2021-05-16T00:07:29Z")

</div>

> [@jmps98](#):
>
> Bluetooth.begin(38400);

The default baud rate, in communication mode, for the HC05 is 9600, unless you changed it. 38400 is for AT mode.

---

<div class="post-metadata">

**Author:** ![cattledog](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/cattledog/32/217816_2.png) [@cattledog](https://forum.arduino.cc/u/cattledog)\
**Post date:** [May 16, 2021, 12:31am UTC](https://forum.arduino.cc/t/problems-in-receiving-data-of-android-to-bluetooth/862272/3 "2021-05-16T00:31:13Z")

</div>

```cpp
dataIn = Bluetooth.readString(); // Read the data as string
if (dataIn=="A") 

```

In your single letter String values are you certain that you are not sending them without any CR/LF appended? The comparison will not be true if there are terminators.

---

<div class="post-metadata">

**Author:** ![jmps98](https://avatars.discourse-cdn.com/v4/letter/j/ecc23a/32.png) [@jmps98](https://forum.arduino.cc/u/jmps98)\
**Post date:** [May 16, 2021, 12:32am UTC](https://forum.arduino.cc/t/problems-in-receiving-data-of-android-to-bluetooth/862272/4 "2021-05-16T00:32:21Z")

</div>

oh yes, I also tested in 9600 and not worked. I just put 38400 to see what happened

---

<div class="post-metadata">

**Author:** ![jmps98](https://avatars.discourse-cdn.com/v4/letter/j/ecc23a/32.png) [@jmps98](https://forum.arduino.cc/u/jmps98)\
**Post date:** [May 16, 2021, 12:37am UTC](https://forum.arduino.cc/t/problems-in-receiving-data-of-android-to-bluetooth/862272/5 "2021-05-16T00:37:14Z")

</div>

> [@cattledog](#):
>
> you certain that you are not sending them without any CR/LF appended? The comparison will not be true if there are

It only sends A.  
look. I will send you the code that works for me. The problem with this new code is that I can´t take the values that are in a slider. (SR,MR, SB,MB)

int estado = 0;  
#define resistencia 9  
#define bomba 10

// variables para contar el tiempo entre procesos, modo automatico.  
unsigned long tiempo1= 0;  
unsigned long tiempo2=0;

int segundosResis= 0;  
int minutosResis=0;

int segundosBomba=0;  
int minutosBomba=0;

void setup() {  
// put your setup code here, to run once:  
Serial.begin(9600);  
pinMode(resistencia, OUTPUT);  
pinMode(bomba, OUTPUT);

// INICIALIZADA DE LOS ESTADOS DE RESISTENCIA Y BOMBA  
digitalWrite(resistencia, LOW);  
digitalWrite(bomba, LOW);

}

void loop() {  
// put your main code here, to run repeatedly:

if (Serial.available() \> 0 )  
{

// dataIn = Serial.readString(); // Read the data as string

```
estado = Serial.read();

 switch( estado)
 {
  case 'A':
  digitalWrite(resistencia, HIGH); // ESTADO A, RESISTENCIA PRENDIDA
  break;

  case 'B':
  digitalWrite(bomba, HIGH); // ESTADO B, BOMBA PRENDIDA
  break;

  case 'C':
   digitalWrite(bomba, LOW); // ESTADO C, BOMBA Y RESISTENCIA APAGADA
 digitalWrite(resistencia, LOW);
 break;

 case 'D':
  digitalWrite(resistencia, LOW); // ESTADO D, RESISTENCIA APAGADA
  break;

  case 'E':
  digitalWrite(bomba, LOW); // ESTADO E, BOMBA APAGADA
  break;
  
 }

```

---

<div class="post-metadata">

**Author:** ![jmps98](https://avatars.discourse-cdn.com/v4/letter/j/ecc23a/32.png) [@jmps98](https://forum.arduino.cc/u/jmps98)\
**Post date:** [May 16, 2021, 12:37am UTC](https://forum.arduino.cc/t/problems-in-receiving-data-of-android-to-bluetooth/862272/6 "2021-05-16T00:37:58Z")

</div>

> [@jmps98](#):
>
> look. I will send you the code that works for me. The problem with this new code is that I can´t take the values that are in a slider. (SR,MR, SB,MB)

Why this new code works and the other one no?

---

<div class="post-metadata">

**Author:** ![drmpf](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/drmpf/32/1126438_2.png) [@drmpf](https://forum.arduino.cc/u/drmpf)\
**Post date:** [May 16, 2021, 12:46am UTC](https://forum.arduino.cc/t/problems-in-receiving-data-of-android-to-bluetooth/862272/7 "2021-05-16T00:46:40Z")

</div>

> [@cattledog](#):
>
> The comparison will not be true if there are terminators.

Try

```arduino
dataIn.trim() // remove terminators

```

Also try

```arduino
Serial.print("'"); Serial.print(dataIn); Serial.print("'"); // see what you are receiving

```

to see what you are receiving

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [September 13, 2021, 12:47am UTC](https://forum.arduino.cc/t/problems-in-receiving-data-of-android-to-bluetooth/862272/8 "2021-09-13T00:47:02Z")

</div>

Closed on September 13, 2021, 12:47am UTC

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.
