SPP Android BT连接在几秒后退出(EEG读数)

时间:2016-07-22 22:29:08

标签: android bluetooth real-time

我创建了一个从bt发射器(Bluegiga)接收数据的应用程序。读数来自EEG信号。数据速率是1Khz(我必须承认它很高)。无论如何,我获取一些数据几秒钟然后蓝牙就像没有传入数据(这不是真的)。如果我尝试断开蓝牙使用该应用程序很清楚,没有与EEG板上的bt模块进行更多通信,因为它没有响应断开连接(它有一个指示何时连接的LED,它没有' t转/开或任何东西)。

如果我将数据速率降低到(假设为500hz),则应用程序可以正常运行,偶尔可以“退出”,但可以忍受。如果我减少它,应用程序可以正常工作。

诅咒,按照设计,我的应用必须以1Khz的数据速率工作,所以这就是问题所在。

我已经检查了一些其他帖子,试图点击某种sns,但没有完全匹配我的问题(无论如何我试图使用它们的信息,但显然没有成功)。

有时我收到此消息,“dm_pm_timer到期”,有时没有(在bt停止工作后)。

可悲的是,没有任何迹象,例外或消息可以告诉我发生了什么。

这是我的BT接收线程代码

    class BluetoothReadThread extends Thread {


    private final InputStream iStream;
    private final OutputStream mmOutputStream;

    private boolean continueReading = true;

    public BluetoothReadThread() {
        InputStream tmp = null;
        OutputStream tmp2 = null;

        try {
            tmp = btSocket.getInputStream();
            tmp2 = btSocket.getOutputStream();

        } catch (IOException e) {
        }
        iStream = tmp;
        mmOutputStream = tmp2;


    }

    @Override
    public void run() {

        int c;
        int waitCount = 0;
        while (continueReading) {
            try {


                if (iStream.available() > 0) {
                    waitCount = 0;
                    c = iStream.read();
                    readBuffer[readBufferPosition++] = c;


                    if (readBufferPosition == bitsExpected) {


                        if (bitsExpected == 22) {
                            ch1 = MultiplicationCombine(readBuffer[4], readBuffer[3]);
                            ch2 = MultiplicationCombine(readBuffer[6], readBuffer[5]);
                            ch3 = MultiplicationCombine(readBuffer[8], readBuffer[7]);
                            ch4 = MultiplicationCombine(readBuffer[10], readBuffer[9]);
                            ch5 = MultiplicationCombine(readBuffer[12], readBuffer[11]);
                            ch6 = MultiplicationCombine(readBuffer[14], readBuffer[13]);
                            ch7 = MultiplicationCombine(readBuffer[16], readBuffer[15]);
                            ch8 = MultiplicationCombine(readBuffer[18], readBuffer[17]);


                        } else {




                            ch1 = (int) filter_3((double)MultiplicationCombine(readBuffer[5], readBuffer[4], readBuffer[3]));
                            ch2 = (int) filter_4((double)MultiplicationCombine(readBuffer[8], readBuffer[7], readBuffer[6]));
                            ch3 = (int) filter_2((double)MultiplicationCombine(readBuffer[11], readBuffer[10], readBuffer[9]));
                            ch4 = (int) filter_2((double)MultiplicationCombine(readBuffer[14], readBuffer[13], readBuffer[12]));
                            ch5 = (int) filter_2((double)MultiplicationCombine(readBuffer[17], readBuffer[16], readBuffer[15]));
                            ch6 = (int) filter_2((double)MultiplicationCombine(readBuffer[20], readBuffer[19], readBuffer[18]));
                            ch7 = (int) filter_2((double)MultiplicationCombine(readBuffer[23], readBuffer[22], readBuffer[21]));
                            ch8 = (int) filter_2((double)MultiplicationCombine(readBuffer[26], readBuffer[25], readBuffer[24]));


                        }

                        Header_int = readBuffer[0];
                        PK_ID_int = readBuffer[1];
                        PK_Counter_int = readBuffer[2];


                        if (downsample++ == downsample_value) {
                            addEntry(ch1 / scaCh1, ch2 / scaCh2, ch3 / scaCh3, ch4 / scaCh4, ch5 / scaCh5, ch6 / scaCh6, ch7 / scaCh7, ch8 / scaCh8);
                            downsample = 0;
                        }
                        //ProgrNum,PacketType,Ch1,Ch2,Ch3,Ch4,Ch5,Ch6,Ch7,Ch8,MRK

                        if (write_open) {
                            osw.write(PK_Counter_int + "," + PK_ID_int + "," + ch1 + "," + ch2 + "," + ch3 + "," + ch4 + "," + ch5 + "," + ch6 + "," + ch7 + "," + ch8 + "," + bolOpenClose + "\n");
                            //osw.write(PK_Counter_int + "," + PK_ID_int + "," + ch1 + "," + ch2 + "," + ch3 + "," + ch4 + "," + ch5 + "," + ch6 + "," + ch7 + "," + ch8 + ","  + "\n");
                        }

                        System.out.println(PK_Counter_int + "," + PK_ID_int + "," + ch1 + "," + ch2 + "," + ch3 + "," + ch4 + "," + ch5 + "," + ch6 + "," + ch7 + "," + ch8 + ", AV=" + iStream.available() );


                        mmOutputStream.write(valueSTR.getBytes());

                        // if(downsample++==14) { safe_copy(readBuffer);  plot=true; downsample=0;}


                        readBufferPosition = 0;
                        try {
                            Thread.sleep(3);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }


                    }


                } 

            } catch (IOException e) {
                System.out.println(e + "\nError sending data + :" + e);
                // Bluetooth error! Stop reading.
                //this.stopAndSendIntent();
            }
        }
    }

    /*
            public void stopAndSendIntent() {

                this.cancel();

                Intent intent = new Intent();
                intent.setAction(BLUETOOTH_ACTION_DONE_READING);
                sendBroadcast(intent);
            }
    */
    public void cancel() {
        System.out.println("-----Cancelling readThread!!");
        try {
            iStream.close();
        } catch (IOException e) {
        } catch (NullPointerException e) {
        }
        ;

        continueReading = false;
    }

}

它的工作方式如下: 我读了一个收到的字符(c = iStream.read())。 然后我将这个字符复制到一个int数组,直到达到数据包的长度(它可以是22或28(bitsExpected))。

以下部分只是过滤和绘制信号。

我尝试了很多其他实现,但结果相同。

即使我删除了部分过滤和绘图(只是读取数据),问题仍然存在。

如果不是使用数组,我使用字符串,即使用append()(应该是相同的?)我设法得到一个工作连接(没有退出),但是,一旦我操作程序使用数组一切都是一样的。

我已经坚持了1个月了,所以我非常感谢任何评论,过去的经验或建议。

提前致谢。

1 个答案:

答案 0 :(得分:0)

我刚添加了这段代码

 if (PK_Counter_int % 100 == 0) mmOutputStream.write(startTring.getBytes());

到目前为止它工作正常。