简体   繁体   English

从蓝牙hc05到Android接收传感器值

[英]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.

相关问题 蓝牙无法从HC05接收数据。 我收到了这个垃圾数据 - Bluetooth receive data from HC05 desn't work. I receive this garbage data ���� 通过 HC-05 蓝牙从 Arduino 传感器接收字符串数据到 Android Studio Textview - Receive string data from Arduino sensor to Android Studio Textview via HC-05 Bluetooth Android InputStream无法从Bluetooth HC-05接收整个消息 - Android InputStream cannot receive the whole message from Bluetooth HC-05 HC 05 蓝牙模块无法连接安卓 - HC 05 Bluetooth Module Not Connecting with Android 带有HC-05的蓝牙串行作为Android服务 - Bluetooth Serial as Android Service with HC-05 Android 通过蓝牙从 HC-06 发布/接收数据 - Android Post/Receive data from HC-06 via Bluetooth 从蓝牙输入流中提取数据的问题,使用安卓平板电脑和 HC-05 无线电发射器 - Problems with extracting data from Bluetooth input stream, using android tablet and HC-05 radio transmitter 如何从 android 应用程序发送值到 Arduino 蓝牙 HC-05 - How to Send Value from android app to Arduino Bluetooth HC-05 如何使用蓝牙模块HC-05从Arduino-uno发送数据并在Android中读取? - How to send data from Arduino-uno using Bluetooth module HC-05 and read it in Android? Arduino HC-05写入串行监视器,但不写入蓝牙(Android) - Arduino HC-05 writes to serial monitor but not Bluetooth (Android)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM