[英]Receive sensor values from bluetooth hc05 to Android
I'm sending sensor values from Arduino via an HC-05 Bluetooth module to Android. 我正在通过HC-05蓝牙模块从Arduino发送传感器值到Android。 I'm receiving the values but they are not showing properly in my textviews.
我正在接收值,但它们在我的textviews中无法正确显示。
I'm using 2 sensors and their values are like this (example first sensor = 120, 97, 110 etc) 我正在使用2个传感器,它们的值是这样的(示例第一个传感器= 120、97、110等)
Sensor2 values are like this (97, 96, 100 etc) ie values are changing. Sensor2的值是这样的(97、96、100等),即值正在变化。
Now my Android code is: 现在我的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 = " ";
}
}
When I receive 2-digit data on sensor 1 like this (76) and 2-digit data also on sensor 2 (97) it shows the data properly. 当我像这样(76)在传感器1上接收到2位数据,并且在传感器2(97)上也接收到2位数据时,它会正确显示该数据。 But if values are greater than 3 digits the values don't show up properly and some digits of the first textview appear on the second textview.
但是,如果值大于3位数,则该值将无法正确显示,并且第一个textview的某些数字将出现在第二个textview上。 I think the problem is getting the data between the indices.
我认为问题在于获取索引之间的数据。 How can I manage this?
我该如何处理? Please help me.
请帮我。
Sorry for my bad English, I'm not a native speaker. 对不起,我的英语不好,我不是母语人士。
It looks like your current substring statements will only get 2 digits worth of information per sensor since the function excludes final index. 看起来您当前的子字符串语句每个传感器将仅获得2位数字的信息,因为该函数不包含最终索引。 You are currently only getting chars at 2,3 and then 5,6.
您目前仅在2,3,然后在5,6处获得字符。 Consider sending an individual string for each sensor value and then using a certain character to denote the end of the value so that the length of the value doesn't matter.
考虑为每个传感器值发送一个单独的字符串,然后使用某个字符表示该值的结尾,因此该值的长度无关紧要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.