2017-07-19 49 views
0

我想問一個關於我的Wifi客戶端通信代碼的問題。我正在與Raspberry Pi通信作爲服務器。 我的代碼的架構是:Android wifi:處理器味精不起作用

  • 主要活動:我Handler類和我在那個需要建立WiFi連接的照顧OnCreat的第一個線程(線程1)推出。

    public class MainActivity extends AppCompatActivity { 
    public int serverPort = 40000; 
    public String serverIP = "10.177.86.212"; 
    public WiFiConnector wifiConnection; 
    
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    
    editTextWE = (EditText) findViewById(R.id.editText_WE); 
    
    wifiConnection = new WiFiConnector(serverIP, serverPort); 
    
    Handler mHandler = new MyHandler(); 
    
    WiFiConnector.Thread1 = new Thread(new WiFiConnector.Thread1(mHandler,true)); 
    WiFiConnector.Thread1.start(); 
    } 
    
    private class MyHandler extends Handler { 
    private byte[] bytes = null; 
    
    @Override 
    public void handleMessage(Message msg) { 
         bytes = msg.getData().getByteArray("KEY"); 
         if(bytes!= null){ 
         for (int i = 0; i < bytes.length; i++){ 
          Log.d("Data received", "value " + (0xFF & bytes[i])); 
         } 
    
         for (int i=0; i<bytes.length; i++) { 
          editTextWE.setText(editTextWE.getText()+ "Server says: " + bytes.length + " "+ (0xFF & bytes[i]) + "\n"); 
         } 
        } 
    } 
    } 
    } 
    
    • WifiConnector類:線程1和線程共享從主要活動來的處理程序。 Thread1向Raspberry Pi發送命令讓它開始發送數據。線程2專用於讀取從服務器接收的數據。

      public class WiFiConnector { 
      
      static String serverIP; 
      static int serverPort; 
      public static Thread Thread1 = null; 
      
      //Constructor 
      public WiFiConnector(String IP, int port) { 
      serverIP = IP; 
      serverPort = port; 
      } 
      
      public static class Thread1 extends Thread implements Runnable { 
      
      private Handler handler1; 
      boolean firsttime = false; 
      OutputStream out ; 
      
      public Thread1(Handler handler_1, boolean firsttime) { 
      this.handler1 = handler_1; 
      this.firsttime = firsttime; 
      } 
      
      public void run() { 
      Socket socket = null; 
      
      try { 
      
          //Writing to a Socket 
          InetAddress serverAddress = InetAddress.getByName(serverIP); 
          socket = new Socket(serverAddress, serverPort); 
      
          out = new DataOutputStream(socket.getOutputStream()); 
          if(firsttime){ 
          //I send "B" to Raspberry to let him start sending data 
          out.write("B".getBytes()); 
          this.fisrttime = false; 
          } 
      
          Thread2 comThread = new Thread2(socket, handler1); 
          new Thread(comThread).start(); 
      
      } catch (UnknownHostException e) { 
          e.printStackTrace(); 
      } catch (IOException e) { 
          e.printStackTrace(); 
      } 
      } 
      } 
      
      public static class Thread2 implements Runnable { 
      
      public Socket clientSocket; 
      private Handler handler_2; 
      public DataInputStream in; 
      public byte[] bytes = new byte[13]; 
      public Message msg; 
      
      public Thread2(Socket clientSocket, Handler handler2) { 
      this.clientSocket = clientSocket; 
      this.handler_2 = handler2; 
      } 
      
      public void run() { 
      
      while (!Thread.currentThread().isInterrupted()) { 
      
          try { 
      
           if (Looper.myLooper() == null) { 
            Looper.prepare(); 
           } 
      
           this.in = new DataInputStream(clientSocket.getInputStream()); 
           in.readFully(bytes); 
      
           if (in != null) { 
            for (int i = 0; i < bytes.length; i++){ 
             Log.d("Data received", "valuewifi " + (0xFF & bytes[i])); 
            } 
      
            msg = new Message(); 
            Bundle b = new Bundle(); 
            b.putByteArray("KEY", bytes); 
            msg.setData(b); 
            handler_2.sendMessage(msg); 
           } else { 
            Thread1 = new Thread(new Thread1(handler_2,false)); 
            Thread1.start(); 
            return; 
           } 
          } catch (IOException e) { 
           e.printStackTrace(); 
          } 
      } 
      Looper.loop(); 
      } 
      } 
      } 
      

現在的問題是:正確

Log.d("Data received", "valuewifi " + (0xFF & bytes[i])); 

打印我的值: 我從樹莓裨接收正確我的數據(包的13個字節每個),確實如此。然後我創建要發送到MainActivity中的處理程序的消息。所述包包括(我已驗證)的輸入流的相同值的接收,但是在MainActivity的處理程序的信息印刷:

Log.d("Data received", "value " + (0xFF & bytes[i])); 

替換每個消息的第一字節的值(我試圖獲得2將每個與RPi的通信打包),其中66是實際上是我發送的用於啓動從Raspberry Pi發送數據的「B」的ASCII碼。

請問你有什麼想法爲什麼這是發生? 非常感謝您的幫助提前!:)

回答

0

好吧,我發現,在線程2,如果我把

public byte[] bytes = new byte[13]; 

運行{..}內之前

in.readFully(bytes); 

交換的信息完美地發生。否則,我只能從MainActivity獲得從服務器接收到的最後一個字節包。

任何有關它爲什麼會發生的建議? 謝謝!