<Solved>Can't read serial data from Arduino!!
Hi all .. I'm trying to connect my Samsung galaxy s 2 to Arduino via bloetooth. With the phone I can easily send data to the device, but I can not receive it! I tried everything, but nothing. But if I send them to the phone via the PC with a USB bluetooth, everything is ok! Can anyone help me? :BangHead::BangHead::BangHead:
Android code:
arduino code:
Hi all .. I'm trying to connect my Samsung galaxy s 2 to Arduino via bloetooth. With the phone I can easily send data to the device, but I can not receive it! I tried everything, but nothing. But if I send them to the phone via the PC with a USB bluetooth, everything is ok! Can anyone help me? :BangHead::BangHead::BangHead:
Android code:
B4X:
Sub Timer_buffer_Tick
If connected Then
lbl_connected.Visible = True
lbl_connected.Text = "Device connected..."
lbl_connected.TextColor = Colors.Green
If text_serial_in.Ready Then 'check if there is any data waiting to be read
'buffer_in_lenght = text_serial_in.Read(buffer_in, 0, buffer_in.Length )
'text_log.Add(DateTime.Time(DateTime.Now) & " --> " & "Received: " & converter.fromchars(buffer_in))
text_log.Add(DateTime.Time(DateTime.Now) & " --> " & "Received: " & text_serial_in.ReadLine )
End If
If (seek_pwm1_old <> seek_pwm1.Value) Then
text_serial_out.Write("p1" & seek_pwm1.Value)
text_serial_out.Flush
text_log.Add(DateTime.Time(DateTime.Now) & " --> " & "Sended pwm1 value: " & seek_pwm1.Value)
seek_pwm1_old = seek_pwm1.Value
Else If (seek_pwm2_old <> seek_pwm2.Value) Then
text_serial_out.Write("p2" & seek_pwm2.Value)
text_serial_out.Flush
text_log.Add(DateTime.Time(DateTime.Now) & " --> " & "Sended pwm2 value: " & seek_pwm2.Value)
seek_pwm2_old = seek_pwm2.Value
Else If (seek_pwm3_old <> seek_pwm3.Value) Then
text_serial_out.Write("p3" & seek_pwm3.Value)
text_serial_out.Flush
text_log.Add(DateTime.Time(DateTime.Now) & " --> " & "Sended pwm3 value: " & seek_pwm3.Value)
seek_pwm3_old = seek_pwm3.Value
End If
Else
lbl_connected.Visible = Not(lbl_connected.Visible)
lbl_connected.Text = "No device connected..."
lbl_connected.TextColor = Colors.Red
End If
End Sub
arduino code:
B4X:
Serial.println("Connected to Bloetooth.");
delay(500);
if (Serial.available() > 0) { //Se sono presenti dati nel buffer
command_type = Serial.read();
delay(2);
if (command_type == 's') { //se il comando è s indica lo stato della connessione
stato_connessione = Serial.read();
if (stato_connessione == 49) { //se lo stato è uguale a 1
delay(2);
Serial.write("Connected to Bloetooth.");
digitalWrite(led_connesso, HIGH);
}
else if (stato_connessione == 48) { //se lo stato è uguale a 0
delay(2);
Serial.print("Disconnected from Bloetooth.");
digitalWrite(led_connesso, LOW);
}
}
Last edited: