将蓝牙输入流写入文件会跳过数据

时间:2014-04-07 21:07:39

标签: android bluetooth inputstream printwriter

我正在尝试编写一个会记录arduino输出的应用程序由于手机或平板电脑上的文本文件。输出速率为1kHz。我的应用程序基于Blueserial代码(https://github.com/plastygrove/BlueSerial)。蓝牙连接与arduino蓝牙模块正确建立,命令被正确发送和接收,一切似乎工作得很好。但是,我保存数据的文件是丢失数据块,通常大约200ms(我的数据中包含毫秒时间戳),导致数据损坏。我一直试图找出问题的根源,我认为它可能与gc有关,但此时我不知所措。这是将我的数据写入文件的代码:

private class ReadInput implements Runnable {

    private boolean bStop = false;
    private Thread t;

    public ReadInput() {
        t = new Thread(this, "Input Thread");
        t.start();
    }

    public boolean isRunning() {
        return t.isAlive();
    }

    public void run() {
        InputStream inputStream;

        try {
            inputStream = mBTSocket.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(inputStream);
            while (!bStop) {
                byte[] buffer = new byte[250];
                int bytes = 0;
                if (bis.available() > 0) {
                    bytes = bis.read(buffer);
                    strInput = new String(buffer, 0, bytes);

                    sb.append(strInput);
                    int endOfLineIndex = sb.indexOf("\r\n");                            // determine the end-of-line
                    pw.print(strInput);                                                 // print buffer to the file buffer
                    pw.flush();                                                         // flush buffer and force write to media


                    if (endOfLineIndex > 0) {                                           // if end-of-line,
                        String sbprint = sb.substring(0, endOfLineIndex);               // extract string
                        sb.delete(0, sb.length());                                      // and clear the string
                        pw.print(strInput);                                             // write buffer to file buffer
                        pw.flush();                                                     // force writing to file
                        pw.close();                                                     // close print writer
                        try {
                            f.close();                                                  // close file output stream
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }   
                    sb.delete(0, sb.length()); strInput = "";                                           

                }
                //Thread.sleep(100);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void stop() {
        bStop = true;
    }

}

这是我的文件outputsteam和printwriter声明:

String strInput = null;
static PrintWriter pw = null;
static FileOutputStream f = null;
private StringBuilder sb = new StringBuilder();

我发送的数据格式如下:

24.330,-58,5,119,460\n
24.331,-86,25,-105,460\n
24.332,66,41,-145,460\n
24.333,90,-23,-85,4622,-7,119,460\n
24.524,6,-95,107,461\n
24.525,10,-7,-173,461\n
24.526,-22,33,103,461\n

在此示例中,您可以看到它跳过某些数据的位置。谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

似乎缺少代码或者您有一些额外的逻辑。 例如,StringBuilder似乎没有做任何事情, bStop永远不会被设置/清除。但是你总是打印出来的数据 通过strInput

线路处理的结束也会关闭,特别是:

String sbprint = sb.substring(0, endOfLineIndex);               // extract string
sb.delete(0, sb.length());       

您提取字符串,然后删除整个缓冲区sb.delete(0,sb.length())
同样在循环的底部。