Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 蓝牙输入字符串中缺少字符_Java_Android_Arduino_Bluetooth - Fatal编程技术网

Java 蓝牙输入字符串中缺少字符

Java 蓝牙输入字符串中缺少字符,java,android,arduino,bluetooth,Java,Android,Arduino,Bluetooth,我目前正试图在一个简单的Android应用程序中创建一个水位读数作为进度条。目前,我使用带有HC-05的Arduino Mega 2560来传输水位传感器的读数。为了简化事情,arduino代码只是从0到1000上下循环,如下所示 void setup() { // put your setup code here, to run once: Serial.begin(9600); Serial.println("Test for Water

我目前正试图在一个简单的Android应用程序中创建一个水位读数作为进度条。目前,我使用带有HC-05的Arduino Mega 2560来传输水位传感器的读数。为了简化事情,arduino代码只是从0到1000上下循环,如下所示

    void setup() {
      // put your setup code here, to run once:
      Serial.begin(9600);
      Serial.println("Test for Water Sensor");
      Serial1.begin(9600);
    
    }
    
    void loop() {
      // put your main code here, to run repeatedly:
    
      for (int i = 0; i <= 1000; i++)
      {
        Serial1.println(i);
        Serial.println(i);
        delay(100);
      }
      for (int i = 1000; i >= 0; i--)
      {
        Serial1.println(i);
        Serial.println(i);
        delay(100);
      }
    }
我遇到的问题是,它经常(可能每秒1-2次)不读取第一个数字。我可以在串行监视器上看到所有数字都在那里,但在android应用程序上,它有时会错过第一个数字(例如:443、444、45、446、447等)

是什么导致了这个问题,我对蓝牙非常陌生,所以请帮助我!如果需要,我们非常乐意发送更多部分的代码

编辑:添加用于读取输入流的代码。一开始可能很重要

public void run() {
            byte[] buffer = new byte[1024];  // buffer store for the stream
            int bytes; // bytes returned from read()
            // Keep listening to the InputStream until an exception occurs
            while (true) {
                try {
                    // Read from the InputStream
                    bytes = mmInStream.available();
                    if(bytes != 0) {
                        SystemClock.sleep(100); //pause and wait for rest of data. Adjust this depending on your sending speed.
                        bytes = mmInStream.available(); // how many bytes are ready to be read?
                        bytes = mmInStream.read(buffer, 0, bytes); // record how many bytes we actually read
                        mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
                                .sendToTarget(); // Send the obtained bytes to the UI activity
                    }
                } catch (IOException e) {
                    e.printStackTrace();

                    break;
                }
            }
        }

你有没有在Arduino的早期将数据发送给我们?可能会在蓝牙连接之前发送一些数据。可以尝试在Arduino中发送数据之前延迟更多时间。我发布了整个Arduino代码,我可以尝试将延迟放在Serial.println()之前,看看这是否有帮助。编辑:如果问题包含必要的代码部分,那么您将得到更多的回答。目前Java部分不完整,例如不清楚
msg.obj
来自何处。我代码中的每个msg实例都在我发布的Java代码块中。我会把代码添加到你读的信息的来源,这可能就是你想要的。我刚刚有一个想法,我可能会存储最后10个传入的数字,然后让它删除任何偏离平均值太远的数字,然后平均出其余的数字。例:543555854847555555758754053567580。平均值是457.8,去掉47,53,因为超出平均值太远,新的平均值是559,然后将其用于水位。我不需要这些数据是100%准确的,所以如果这是+-5%,我会侥幸逃脱。这样行吗?
public void run() {
            byte[] buffer = new byte[1024];  // buffer store for the stream
            int bytes; // bytes returned from read()
            // Keep listening to the InputStream until an exception occurs
            while (true) {
                try {
                    // Read from the InputStream
                    bytes = mmInStream.available();
                    if(bytes != 0) {
                        SystemClock.sleep(100); //pause and wait for rest of data. Adjust this depending on your sending speed.
                        bytes = mmInStream.available(); // how many bytes are ready to be read?
                        bytes = mmInStream.read(buffer, 0, bytes); // record how many bytes we actually read
                        mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
                                .sendToTarget(); // Send the obtained bytes to the UI activity
                    }
                } catch (IOException e) {
                    e.printStackTrace();

                    break;
                }
            }
        }