I'm building an app in Android to communicate with Arduino via usb.
In Android, I create a button to send data to the Arduino. The Arduino should then respond to this message and send another message to be treated in the function of the same button. The problem is that when I click on the button the app stops.
My Arduino code:
void setup()
{
Serial.begin(9600);
}
void loop()
{
char c;
int i = 0;
if(Serial.available()){
c=Serial.read();
if (c == '4'){
Serial.write("X:100 Y:10");
}
}
}
Defining a Callback which triggers whenever data is read in Android:
UsbSerialInterface.UsbReadCallback mCallback = new UsbSerialInterface.UsbReadCallback() {
@Override
public void onReceivedData(byte[] arg0) {
String data = null;
try {
data = new String(arg0, "UTF-8");
data.concat("\n");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
};
The button function in Android is:
public void goBefore(View view) {
ssend = "M114"; // send message to update current position
serialPort.write(ssend.getBytes());
String[] current_Pos = data.split(" ");
Do you know what I'm doing wrong?