Hello guys,
I want to do a project that needs communication between Android application and Arduino Ethernet board in both ways. I was successful to send HttpRequest from Android phone to Arduino board, but when I want to send back response as a result of the HttpRequest from Arduino board to the same Android phone, I am not able to receive anything. Actually, I am trying to handle a simple authentication page which send username and password to Arduino and Arduino send back “authentication OK” to android phone. BTW, if there is any better method to provides me this communication, I'd appreciate to offer with examples;)
Android Code:
public void onClick(View v) {
if (v == btn) {
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://192.168.1.113/signIn/?Pa1="
+ txtUserName.getText().toString() + "&Pa2="
+ txtPassword.getText().toString());
try {
httpclient.execute(httpget);
HttpResponse response = httpclient.execute(httpget);
this.request(response, txtResult);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
}
public void request(HttpResponse response, TextView tx) {
try {
InputStream in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder str = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
str.append(line + "\n");
}
in.close();
tx.setText(str);
} catch (Exception ex) {
tx.setText("Failed!");
}
}
Arduino Code:
if (strcmp(buffer, "signIn") == 0){
if(finder.getString("", "&", buffer1, sizeof(buffer1))){
Serial.println(buffer1);
if (strcmp(buffer1, "?Pa1=admin") == 0){
if(finder.getString("", " HTTP", buffer2, sizeof(buffer2))){
Serial.println(buffer2);
if (strcmp(buffer2, "Pa2=1234") == 0){
//this is the part that I try to send back response to android phone
client.println("Authenticate OK");
}
}
}
}
}
}