我想问一个关于我在 Wifi 客户端通信中的代码的问题。我正在与树莓派作为服务器进行通信。 我的代码的体系结构是:
-
主要活动:我有处理程序类,我在OnCreat中启动了第一个线程(Thread1(,它负责建立wifi连接。
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 类:Thread1 和 Thread2 共享来自 Main Activity 的处理程序。Thread1 向 Raspberry Pi 发送命令,让它开始发送数据。Thread2 专用于读取从服务器接收的数据。
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(); } } }
现在的问题是: 我从Raspberry Pi正确接收了我的数据(每个包13字节(,确实:
Log.d("Data received", "valuewifi " + (0xFF & bytes[i]) );
正确打印我的值。然后,我创建要发送到 MainActivity 中的处理程序的消息。捆绑包包含(我已经验证(收到的输入流的相同值,但消息打印在 MainActivity 的处理程序中:
Log.d("Data received", "value " + (0xFF & bytes[i]) );
将每条消息的第一个字节值(我试图与 RPi 的每个通信获取 2 个包(替换为 66,这实际上是我发送的"B"的 ASCII 代码,用于启动从 Raspberry Pi 发送的数据。
请问您知道为什么会发生这种情况吗? 非常感谢您的帮助!:)
好吧,我发现在 Thread2 中如果我把
public byte[] bytes = new byte[13];
在运行{..}之前
in.readFully(bytes);
信息交换完美地发生。否则,我只会在主活动中获取从服务器收到的最后一个字节包。
关于为什么会发生这种情况的任何建议? 谢谢!