简体   繁体   English

Android InputStream无法从Bluetooth HC-05接收整个消息

[英]Android InputStream cannot receive the whole message from Bluetooth HC-05

My Problem is to receive data from android app I send data from Arduino which is like that {166;122;33;5;n} and when I open a socket with Android it jus receive the first 9 or 10 bytes and show me the data like that 166;122;33. 我的问题是从Android应用程序接收数据,我从Arduino发送数据,就像{166; 122; 33; 5; n},当我用Android打开套接字时,它会收到前9或10个字节并向我显示166; 122; 33之类的数据。 want to know how to receive it all in InputStream. 想知道如何在InputStream中接收所有内容。 the function that responsible for that is beginListenForData() here it's. 这里负责的功能是beginListenForData()。


  void beginListenForData() {
    final Handler handler = new Handler();
    stopThread = false;
    Thread thread = new Thread(new Runnable() {
        public void run() {
            while (!Thread.currentThread().isInterrupted() && !stopThread) {
                try {
                    final int byteCount = inputStream.available();
                    if (byteCount > 0) {
                        byte[] rawBytes = new byte[byteCount];
                        inputStream.read(rawBytes);
                        final String str = new String(rawBytes);
                        handler.post(new Runnable() {
                            public void run() {
                                if (str.charAt(0) == '{') {
                                    try {
                                        bytes = inputStream.read(buffer, 0, 
                                                                 buffer.length);
                                        String readMessage1 = new String(buffer,   
                                                          0, bytes, "US-ASCII");
                                        Log.e("bytes ",  
                                              Integer.toString(bytes));
                                        Log.e("tag:", readMessage1);
                                        sendMessage(readMessage1);

                                    } catch (IOException e) {
                                        e.printStackTrace();
                                        Log.e("tag", "failed");
                                    }
                                }


                            }
                        });

                    }
                } catch (IOException ex) {
                    sendMessage("F");
                    stopThread = true;
                }
            }
        }
    });

    thread.start();
}
> Try something like:

      void beginListenForData() {
        final Handler handler = new Handler();
        stopThread = false;
        Thread thread = new Thread(new Runnable() {
            public void run() {
                while (!Thread.currentThread().isInterrupted() && !stopThread) {
         try {
                            byte[] buffer = new byte[1];
                            bytes = mmInStream.read(buffer, 0, 1);
                            String read = new String(buffer, 0, 1);
                            readMessage.append(read);
                            if (buffer[0] == '}') {
                                bytes = mmInStream.read(buffer, 0, 1);

                                    readMessage.append(read);
      String readMessage1 = new String(buffer,   
                                                              0, bytes, "US-ASCII");
                                            Log.e("bytes ",  
                                                  Integer.toString(bytes));

                                            sendMessage(readMessage);

                                        } catch (IOException e) {
                                            e.printStackTrace();
                                            Log.e("tag", "failed");
                                        }



                                }
                            });

                        }
                    } catch (IOException ex) {
                        sendMessage("F");
                        stopThread = true;
                    }
                }
            }
        });

        thread.start();
    }                               

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM