为什么我无法使用蓝牙从Arduino到Android接收完整的字符串?

时间:2017-01-05 01:04:52

标签: android bluetooth arduino

我通过蓝牙将字符串从arduino发送到Android,当字符串到达​​应用程序时,我创建子字符串以获取我需要的数据。但是,字符串不完整,它们被剪切,因此我无法得到我需要的子字符串。例如:从Arduino我发送这种风格的字符串:

#ecg{21304(2.41~

当我在arduino串行监视器上发送几个字符串时,如下所示:

#ecg{0(137~
#ecg{1(136~
#ecg{3(162~
#ecg{5(221~
#ecg{6(282~
#ecg{8(296~
#ecg{10(259~
#ecg{12(208~
#ecg{13(166~
#ecg{15(141~
#ecg{17(132~
#ecg{19(144~
#ecg{20(194~
#ecg{22(262~
#ecg{24(296~
#ecg{26(267~
#ecg{27(217~
#ecg{29(173~
#ecg{31(143~

但是当他们到达Android时,就会发生这样的事情:

I/System.out: ReadMessage: #ecg{6147 end string
I/System.out: ReadMessage: 0(335~
I/System.out: #ecg{61472(335~
I/System.out: #ecg{61474(273~
I/System.out: #ecg{61476(230~
I/System.out: #ecg{61478(252~
I/System.out: #ecg{61480(307~
I/System.out: #ecg{61483(307~
I/System.out: #ecg{61485(279~
I/System.out: #ecg{61487(306~
I/System.out: #ecg{61488(347~
I/System.out: #ecg{61490(313~
I/System.out: #ecg{61492(249~
I/System.out: #ecg{61494(231~
I/System.out: #ecg{61496(277~
I/System.out: #ecg{61498(320~
I/System.out: #ecg{61500 end string
I/System.out: ReadMessage: (290~
I/System.out: #ecg{61502(284~
I/System.out: #ecg{61504(328~
I/System.out: #ecg{61506(343~
I/System.out: #ecg{61507(283~
I/System.out: #ecg{61509(237~
I/System.out: #ecg{61511(246~
I/System.out: #ecg{61513(303~
I/System.out: #ecg{61515(311~
I/System.out: #ecg{61517(280~
I/System.out: #ecg{61519(301~
I/System.out: #ecg{61521(346~
I/System.out: #ecg{61523(321~
I/System.out: #ecg{61524(258~
I/System.out: #ecg{61527(230~
I/System.out: #ecg{61529( end string
I/System.out: ReadMessage: 269~
I/System.out: #ecg{61531(318~

请注意,从大多数情况下,它们从一开始就没有到达。

我想得到这样的字符串:

I/System.out: ReadMessage: #ecg{61502(284~ end string
I/System.out: ReadMessage: #ecg{61502(284~ end string
I/System.out: ReadMessage: #ecg{61504(328~ end string
I/System.out: ReadMessage: #ecg{61506(343~ end string
I/System.out: ReadMessage: #ecg{61507(283~ end string

因此我无法创建我需要的子串。你知道为什么Strings被切断而且不完整,因为我从arduino发送它们?有什么办法可以解决吗?

可能是不完整的字符串接收是我在Arduino代码(115200)中使用的速度传输? 我测试了在另一个通过蓝牙将数据发送到同一个应用程序的arduino加载程序,这些到达得很好,区别在于它将数据发送到9600

这是我发送的Arduino代码:

    #include <eHealth.h>

  String CadenaUno="";
  String CadenaDos="";
  String CadenaTres="";
  String CadenaFinal="";

// The setup routine runs once when you press reset:
void setup() {
  Serial.flush();
  Serial.begin(115200);
 // Serial.begin(9600); //No muestra nada en android studio a esta velocidad
}
// The loop routine runs over and over again forever:
void loop() {
        long currentMillis = millis();
        float floatecgValue = eHealth.getECG();
        int valueECG = floatecgValue*100;
                    CadenaUno="#ecg{";
                    CadenaDos="(";
                    CadenaTres="~";
                    CadenaFinal=CadenaUno+currentMillis+CadenaDos+valueECG+CadenaTres;                    
                    Serial.println(CadenaFinal);    
                   delay(1); 
}

这是我通过蓝牙验证从arduino到达的数据的部分

        while (mState == STATE_CONNECTED) {
            try {
                StringBuilder recDataString = new StringBuilder();
                String paquete = "";

                String outputText="";
                byte[] buffer = new byte[256];
                bytes = mmInStream.read(buffer);
                mDataParse.Add(buffer, bytes);
                String readMessage = new String(buffer, 0, bytes);
                System.out.println("ReadMessage: "+readMessage+" end string");

请指南。

1 个答案:

答案 0 :(得分:0)

试试这个:

String prev = null;
while (mState == STATE_CONNECTED) {
    try {
        StringBuilder recDataString = new StringBuilder();
        String paquete = "";

        String outputText = "";
        byte[] buffer = new byte[256];
        bytes = mmInStream.read(buffer);
        mDataParse.Add(buffer, bytes);
        String readMessage = new String(buffer, 0, bytes);
        //Split by new line symbol
        String[] msgs = readMesage.split("\n");
        int msgsCount = msgs.length;
        //Check is we have a prev not completed message?
        if (msgsCount > 0) {
            if (prev != null)
                msgs[0] = prev + msgs[0]; //add it to first message

            //Check is last message completed?
            if (readMessage.endsWith("\n"))
                prev = null;
            else {
                prev = msgs[msgsCount - 1]; //Set last message to prev and remove it from output
                msgsCount--;
            }

            for (int i = 0; i < msgsCount; i++) {
                System.out.println("ReadMessage: " + msgs[i] + " end string");
            }
        }
    }