2014-01-12 2 views
-1

он работает для кнопки «вперед», но не кнопки «Назад». использования pi4j для перемещения робота вперед и назад, но комментировать их, чтобы проверить первые командные работы, я чувствую, что-то делать с сервером более андроид приложения
сервера Java кодаАндроид-клиент и java tcp

   import java.io.BufferedReader; 
      import java.io.DataInputStream; 
      import java.io.DataOutputStream; 
      import java.io.IOException; 
      import java.io.InputStreamReader; 
      import java.net.ServerSocket; 
      import java.net.Socket; 
      import java.util.Scanner; 
      import com.pi4j.io.gpio.GpioController; 
      import com.pi4j.io.gpio.GpioFactory; 
      import com.pi4j.io.gpio.GpioPinDigitalOutput; 
      import com.pi4j.io.gpio.PinState; 
      import com.pi4j.io.gpio.RaspiPin; 
      import com.pi4j.io.gpio.GpioController; 
      import com.pi4j.io.gpio.GpioFactory; 
      import com.pi4j.io.gpio.GpioPinDigitalOutput; 
      import com.pi4j.io.gpio.PinPullResistance; 
      import com.pi4j.io.gpio.PinState; 
      import com.pi4j.io.gpio.RaspiPin; 


      public class Server { 
       String input="ggg"; 

      public static void main(String[] args)throws InterruptedException 
      { 
       ServerSocket serverSocket = null; 
       Socket socket = null; 
       DataInputStream dataInputStream = null; 
       DataOutputStream dataOutputStream = null; 

       try 
       { 
       serverSocket = new ServerSocket(80); 
       System.out.println("Listening :80"); 

       } 
       catch (IOException e) 
       { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
       } 

       while(true) 
       { 
       try 
       { 
       socket = serverSocket.accept(); 
       dataInputStream = new DataInputStream(socket.getInputStream()); 
       dataOutputStream = new DataOutputStream(socket.getOutputStream()); 

       System.out.println("ip: " + socket.getInetAddress()); 
       //System.out.println("message: " + dataInputStream.readUTF()); 

         if(dataInputStream.readUTF().contains("forward")) 
         { 
                   Runtime r = Runtime.getRuntime(); 
          Process p = null; 
          String commands="ls"; 
          //boolean Light=false; 


            // provision gpio pin #01 as an output pin and turn on 
            try 
            { 
             //Light=true; 
             p = r.exec(commands); 
             BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream())); 
             String str = ""; 

             while ((str = br.readLine()) != null) 
             { 




              // create gpio controller 
              System.out.println(str); 
             } 
             p.waitFor(); 
             br.close(); 
             //p.waitFor(); 
            } 
          catch (Exception e) 
            { 
            System.out.println("Exception "+e.getMessage()); 
            System.err.println("Exception "+e.getMessage()); 
            } 
              } 

         else if(dataInputStream.readUTF().contains("back")) 
         { 
            //Runtime rt=Runtime.getRuntime(); 
          //Process pr=rt.exec(cmd); 
          Runtime r = Runtime.getRuntime(); 
          Process p = null; 
          String commands="dir"; 



            try 
            { 

             p = r.exec(commands); 
             BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream())); 
             String str = ""; 

             while ((str = br.readLine()) != null) 
             { 





              System.out.println(str); 
             } 
             p.waitFor(); 
             br.close(); 
             //p.waitFor(); 
            } 
          catch (Exception e) 
            { 
            System.out.println("Exception "+e.getMessage()); 
            System.err.println("Exception "+e.getMessage()); 
            } 


       } 
       catch (IOException e) 
       { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
       } 
       finally 
       { 

       if(socket!= null) 
       { 
        try { 
         socket.close(); 
         } 
         catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
         } 
       } 

       if(dataInputStream!= null) 
       { 
        try 
        { 
         dataInputStream.close(); 
        } 
        catch (IOException e) 
        { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
       } 

       if(dataOutputStream!= null) 
       { 
        try 
        { 
         dataOutputStream.close(); 
        } 
        catch (IOException e) 
        { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
       } 
       } 
       } 
      } 
      } 

клиент андроид

 package com.example.client; 


     import java.io.DataInputStream; 
     import java.io.DataOutputStream; 
     import java.io.IOException; 
     import java.net.Socket; 
     import java.net.UnknownHostException; 

     import android.app.Activity; 
     import android.content.Context; 
     import android.os.AsyncTask; 
     import android.os.Bundle; 
     import android.view.View; 
     import android.view.View.OnClickListener; 
     import android.widget.Button; 
     import android.widget.EditText; 
     import android.widget.TextView; 

     public class controll extends Activity implements OnClickListener { 

     EditText textOut; 
     boolean flegmove=false; 
     //TextView textIn; 
     String comand=""; 

     /** Called when the activity is first created. */ 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.conroller); 

      textOut = (EditText)findViewById(R.id.textout); 
      Button buttonforward = (Button)findViewById(R.id.bforward); 
      buttonforward.setOnClickListener(this); 
      Button buttonback = (Button)findViewById(R.id.bback); 
      buttonback.setOnClickListener(this); 
      Button buttonleft = (Button)findViewById(R.id.bleft); 
      buttonleft.setOnClickListener(this); 
      Button buttonright = (Button)findViewById(R.id.bright); 
      buttonright.setOnClickListener(this); 






     } 
     public void onClick(View v) 
     { 
      switch(v.getId()) 
      { 
       case R.id.bforward: 
       { 

        comand="forward"; 
        flegmove=true; 
        new task1().execute(comand); 
        break; 

       } 
       case R.id.bback: 
       { 
        comand="back"; 
        flegmove=true; 
        new task1().execute(comand); 
        break; 

       } 
       case R.id.bright: 
       { 
        comand="right"; 
        flegmove=true; 
        new task1().execute(comand); 
        break; 

       } 
       case R.id.bleft: 
       { 
        comand="left"; 
        flegmove=true; 
        new task1().execute(comand); 
        break; 

       } 

     } 
      if(flegmove==true) 
      { 
       comand=comand.toString(); 
       System.out.println("comand"+comand); 
       //new task1().execute(comand); 
      } 
     } 


      private class task1 extends AsyncTask<String, Void,String> 
     { 

      Socket socket = null; 
      String answer=""; 
      DataOutputStream dataOutputStream = null; 
      DataInputStream dataInputStream = null; 
      protected String doInBackground(String...params) 
      { 

       try 
       { 
         socket = new Socket("192.168.1.12", 80); 
         dataOutputStream = new DataOutputStream(socket.getOutputStream()); 
         dataInputStream = new DataInputStream(socket.getInputStream()); 
        /
         dataOutputStream.writeUTF(comand); 
         System.out.println("comand"); 
         answer=dataInputStream.readUTF(); 
       } 
       catch (UnknownHostException e) 
       { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
       } catch (IOException e) 
       { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 

       } 


       return answer; 


      } 

      protected void onPostExecute(String result) { 

         if (dataOutputStream != null){ 
         try { 
         dataOutputStream.close(); 
         } catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
         } 
         } 

         if (dataInputStream != null){ 
         try { 
         dataInputStream.close(); 
         } catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
         } 
         } 



      } 

     } 


     } 

ответ

-1

пытаются использовать JSch SSH Connexion InstEd сокета Conexion