2017-09-01 135 views
1

我通过HC-05蓝牙模块向Arduino发送传感器值。我正在接收这些值,但他们在我的文章中没有正确显示。从蓝牙hc05接收传感器值到Android

我使用2个传感器,它们的值是这样的(例如第一传感器= 120,97,110等)

传感器2的值是这样的(97,96,100等) 即值改变。现在

我的Android代码是:

if (msg.what == handlerState) { 
    //if message is what we want 
    String readMessage = (String) msg.obj; 
    // msg.arg1 = bytes from connect thread 
    recDataString.append(readMessage); 
    //keep appending to string until ~ 
    int endOfLineIndex = recDataString.indexOf("~"); 
    // determine the end-of-line 
    if (endOfLineIndex > 0) { 
    // make sure there data before ~ 
    String dataInPrint = recDataString.substring(0, endOfLineIndex); 
    // extract string 
    if (recDataString.charAt(0) == '#') { 
     //if it starts with # we know it is what we are looking for 
     String sensor0 = recDataString.substring(1, 3); 
     //get sensor value from string between indices 1-5 
     String sensor1 = recDataString.substring(4, 6); 
     //same again... 
     heart_rate.setText(sensor0); 
     // update the textviews with sensor values 
     temperature.setText(sensor1); 
    } 
    recDataString.delete(0, recDataString.length()); 
    //clear all string data 
    dataInPrint = " "; 
    } 
} 

当我接收在传感器1上2位数字数据等也对传感器2(97)该(76)和2位数字数据它正确地示出了数据。但是,如果值大于3位,则值无法正确显示,并且第一个textview的某些数字出现在第二个textview中。我认为问题是获取索引之间的数据。我怎样才能管理这个?请帮帮我。

对不起,我不是母语的人。

+0

仍然没有反应 –

回答

0

看起来你的当前子串语句只会得到每个传感器2位数的信息,因为函数排除了最终的索引。你目前只能得到2,3和5,6的字符。考虑为每个传感器值发送一个单独的字符串,然后使用某个字符表示该值的结尾,以便该值的长度无关紧要。