The meaning of the program is to get on Wi-Fi readings from sensors on the device. When sending commands, I get the same values ββ(previous), and through the console I get the correct value.
Tell me what's wrong? When I connect to the device netcat'om, then the data comes normally.
Activity: http://pastebin.com/ASpznVEq
Client: http://pastebin.com/VqjyjZ22
activity:
package com.pmeter.dmitrij999.photometer; //import android.support.v4.app.FragmentTransaction; import android.app.Activity; import android.app.FragmentTransaction; import android.app.Fragment; import android.os.AsyncTask; import android.support.v4.app.FragmentActivity; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.KeyEvent; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.FrameLayout; import android.widget.RadioGroup; import android.widget.TabHost; import android.widget.Toast; import com.jjoe64.graphview.GraphView; public class Screen extends Activity { RadioGroup scrView; Graph_Frag graph_frag; Console_Frag console_frag; public client tcp_net; FragmentTransaction fTrans; public static String measures[]; public static String reply; boolean measured = false; boolean connect = false; String ip; final String LOG_TAG = "myLogs"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_screen); graph_frag = new Graph_Frag(); console_frag = new Console_Frag(); scrView = (RadioGroup) findViewById(R.id.scr); scrView.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // TODO Auto-generated method stub fTrans = getFragmentManager().beginTransaction(); //FrameLayout frame = (FrameLayout) findViewById(R.id.screen_frag); switch (checkedId) { case -1: //Toast.makeText(getApplicationContext(), "No choice", Toast.LENGTH_SHORT).show(); break; case R.id.graphic: //fTrans.remove(console_frag); if (measured) fTrans.replace(R.id.screen_frag, graph_frag); else Toast.makeText(getApplicationContext(), "ΠΠ΅Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ ΠΏΠΎΠΊΠ°Π·Π°ΡΡ Π³ΡΠ°ΡΠΈΠΊ Π±Π΅Π· ΠΈΠ·ΠΌΠ΅ΡΠ΅Π½ΠΈΠΉ!", Toast.LENGTH_SHORT).show(); //Toast.makeText(getApplicationContext(), "Radio0", Toast.LENGTH_SHORT).show(); break; case R.id.cons: //Toast.makeText(getApplicationContext(), "Radio1", Toast.LENGTH_SHORT).show(); fTrans.replace(R.id.screen_frag, console_frag); break; default: break; } fTrans.commit(); } }); if (savedInstanceState == null) { connect = false; } else { connect = savedInstanceState.getBoolean("connect"); } Button con = (Button) findViewById(R.id.connect); final EditText ipaddress = (EditText) findViewById(R.id.ip); Button measure = (Button) findViewById(R.id.measure); // // connection(); measure.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (tcp_net == null || !connect || client.getError()) Toast.makeText(getApplicationContext(), R.string.conclosed, Toast.LENGTH_SHORT).show(); else { int i = 0; measures = new String[16]; for (i = 0;i<16;i++) measures[i] = "0"; for (i = 0;i<16;i++) { //reply = null; tcp_net.sendMessage("dat" + i + "\n\r\n"); Log.d("Send", "dat" + i + "\n\r"); if (reply != null) { measures[i] = reply; Log.d("Meas ", " " + i + " " + reply); //i++; } } measured = true; } } }); con.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ip = ipaddress.getText().toString(); connect = !connect; connection(); } }); Log.d(LOG_TAG, "onCreate"); } public static String getMeasure(int j) { if (measures != null) return measures[j]; else return null; } int toInt(String input) { int out = 0; int len = input.length(); for (int l=0;l<len;l++) { switch (input.indexOf(l)) { case '0': break; case '1': out=out+1*(10^(len-l-1)); break; case '2': out=out+2*(10^(len-l-1)); break; case '3': out=out+3*(10^(len-l-1)); break; case '4': out=out+4*(10^(len-l-1)); break; case '5': out=out+5*(10^(len-l-1)); break; case '6': out=out+6*(10^(len-l-1)); break; case '7': out=out+7*(10^(len-l-1)); break; case '8': out=out+8*(10^(len-l-1)); break; case '9': out=out+9*(10^(len-l-1)); break; default: break; } } return out; } protected void connection() { Button con = (Button) findViewById(R.id.connect); connectTask cn = new connectTask(); if (!connect) { con.setText(R.string.connect); cn.cancel(true); } else { cn.execute(""); con.setText(R.string.disconnect); if (tcp_net != null) { tcp_net.sendMessage("ip?\n\r"); } } if (client.getError()) { connect = false; con.setText(R.string.connect); cn.cancel(true); Toast.makeText(getApplicationContext(), R.string.con_error, Toast.LENGTH_SHORT).show(); } } protected void onDestroy() { super.onDestroy(); Log.d(LOG_TAG, "onDestroy"); } protected void onPause() { super.onPause(); Log.d(LOG_TAG, "onPause"); } protected void onRestart() { super.onRestart(); Log.d(LOG_TAG, "onRestart"); } protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); Log.d(LOG_TAG, "onRestoreInstanceState"); } protected void onResume() { super.onResume(); Log.d(LOG_TAG, "onResume "); } protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putBoolean("connect", connect); Log.d(LOG_TAG, "onSaveInstanceState"); } protected void onStart() { super.onStart(); Log.d(LOG_TAG, "onStart"); } protected void onStop() { super.onStop(); Log.d(LOG_TAG, "onStop"); } public static void putRes(String msg) { reply = msg; } public class connectTask extends AsyncTask<String,String,client> { @Override protected client doInBackground(String... message) { //we create a TCPClient object and tcp_net = new client(new client.OnMessageReceived() { @Override //here the messageReceived method is implemented public void messageReceived(String message) { //this method calls the onProgressUpdate publishProgress(message); } }); tcp_net.Set(ip); tcp_net.run(); return null; } @Override protected void onProgressUpdate(String... values) { super.onProgressUpdate(values); //reply = values[0]; //Toast.makeText(getApplicationContext(), reply, Toast.LENGTH_SHORT).show(); //in the arrayList we add the messaged received from server //arrayList.add(values[0]); // notify the adapter that the data set has changed. This means that new message received // from server was added to the list } @Override protected void onCancelled() { super.onCancelled(); //tvInfo.setText("Cancel"); //tcp_net.stopClient(); Toast.makeText(getApplicationContext(), R.string.conclosed, Toast.LENGTH_SHORT).show(); Log.d(LOG_TAG, "Cancel"); } } } client:
package com.pmeter.dmitrij999.photometer; import android.content.Context; import android.util.Log; import android.widget.Toast; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.ConnectException; import java.net.InetAddress; import java.net.Socket; /** * Created by dmitrij999 on 15.04.16. */ public class client { private String serverMessage; public static String SERVERIP = "192.168.1.100"; //your computer IP address public static int SERVERPORT = 80; private OnMessageReceived mMessageListener = null; private boolean mRun = false; public static boolean errors = false; PrintWriter out; BufferedReader in; /** * Constructor of the class. OnMessagedReceived listens for the messages received from server */ public client(OnMessageReceived listener) { mMessageListener = listener; } public void Set(String ip) { SERVERIP = ip; //Log.d("IP", SERVERIP); //SERVERPORT = port; } public static boolean getError() { return errors; } /** * Sends the message entered by client to the server * @param message text entered by client */ public void sendMessage(String message){ if (out != null && !out.checkError()) { out.println(message); out.flush(); } } public void stopClient(){ mRun = false; } public void run() { mRun = true; try { //here you must put your computer's IP address. InetAddress serverAddr = InetAddress.getByName(SERVERIP); Log.e("TCP Client", "C: Connecting..."); //mMessageListener.messageReceived("Connecting..."); // errors = false; //create a socket to make the connection with the server try { Socket socket = new Socket(serverAddr, SERVERPORT); try { //send the message to the server out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true); Log.e("TCP Client", "C: Sent."); //mMessageListener.messageReceived("Sent "); Log.e("TCP Client", "C: Done."); errors = false; //receive the message which the server sends back in = new BufferedReader(new InputStreamReader(socket.getInputStream())); //in this while the client listens for the messages sent by the server while (mRun) { //Thread.sleep(300); serverMessage = in.readLine(); if (serverMessage != null && mMessageListener != null) { //call the method messageReceived from MyActivity class mMessageListener.messageReceived(serverMessage); Log.e("RESPONSE FROM SERVER", "S: Received Message: '" + serverMessage + "'"); Screen.putRes(serverMessage); } serverMessage = null; } } catch (Exception e) { Log.e("TCP", "S: Error", e); mMessageListener.messageReceived("Error " + e); errors = true; } finally { //the socket must be closed. It is not possible to reconnect to this socket // after it is closed, which means a new socket instance has to be created. socket.close(); } } catch (ConnectException e) { Log.e("TCP", "C: Error", e); errors = true; Log.e("TCP", "Π‘Π»ΠΎΠ²ΠΈΠ»ΠΈ..."); } } catch (Exception e) { Log.e("TCP", "C: Error", e); errors = true; mMessageListener.messageReceived("Error " + e); } } //Declare the interface. The method messageReceived(String message) will must be implemented in the MyActivity //class at on asynckTask doInBackground public interface OnMessageReceived { void messageReceived(String message); } }