蓝牙Socket通信,客户端接收的消息顺序打乱了为什么?

例如:服务器端发送:123456

客户端会收到:34556或者123455

什么原因,求指导!

// 读取数据
    private class readThread extends Thread {

        public void run() {
            BufferedInputStream inStream = null;
            byte[] buffer = new byte[1024];
            while (true) {
                try {
                    inStream = new BufferedInputStream(socket.getInputStream());
                    inStream.read(buffer);
                    String str = new String(buffer);
                    Message msg = new Message();
                    msg.obj = str;
                    msg.what = 1;
                    LinkDetectedHandler.sendMessage(msg);
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                    try {
                        inStream.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    break;
                }
            }
        }
    }

推荐图书

  • Android 2高级编程(第2版)
  • Google Android SDK开发范例大全(第2版)
  • 疯狂Android讲义


1个回答

 private class readThread extends Thread { 

        public void run() { 
            BufferedInputStream inStream = null; 
            inStream = new BufferedInputStream(socket.getInputStream()); 
            byte[] buffer = new byte[1024]; 
            while (true) { 
                try {                    
                    inStream.read(buffer); 
                    String str = new String(buffer); 
                    Message msg = new Message(); 
                    msg.obj = str; 
                    msg.what = 1; 
                    LinkDetectedHandler.sendMessage(msg); 
                } catch (IOException e1) { 
                    // TODO Auto-generated catch block 
                    e1.printStackTrace(); 
                    try { 
                        inStream.close(); 
                    } catch (IOException e) { 
                        // TODO Auto-generated catch block 
                        e.printStackTrace(); 
                    } 
                    break; 
                } 
            } 
        } 
    }