android - 通过 BLE 发送连续数据流时出现的问题

标签 android arduino bluetooth-lowenergy

我想知道是否有人可以帮助我找出导致我发送的数据损坏的原因。

我目前的设置是连接了 HM-10 蓝牙模块的 Arduino pro mini(我也尝试过 HM-11 模块)和一个用于接收蓝牙数据的 Android 应用程序。

模块设置:http://letsmakerobots.com/node/38009

如果我以足够大的间隔发送数据,那么数据就很好,但如果我连续发送数据,我会看到消息混淆和丢失。为了测试这一点,我从 Arduino 向 Android 应用程序发送“$0.1,0.2,0.3,0.4,0.5”,有时数据流似乎发送正常,但其他时候它真的很困惑。请看下面的图表来证明这一点:

好案例:

enter image description here

坏情况:

enter image description here

Arduino 代码:

String inputString = ""; //Hold the incoming data.
boolean stringComplete = false; //Determines if the string is complete.
boolean realtime = false;

void setup()
{
  Serial.begin(9600);
  delay(500);
  Serial.print("AT+START");
  delay(500);
}

void loop()
{
  if(stringComplete)
  {
    if(inputString.equals("rStart"))
    {
      Serial.println("$startACK");
      realtime = true;
   }
    else if(inputString.equals("stop"))
    {
      Serial.println("$stopACK");
      realtime = false;
    }
    else{
      Serial.print(inputString);
    }

    inputString = "";
    stringComplete = false;
  }


  if(realtime)
  {
    Serial.println("$0.1,0.2,0.3,0.4,0.5,0.6");
   delay(10); 
  }
}

void serialEvent() {
  while (Serial.available())
  {
    // get the new byte:
    char inChar = (char)Serial.read(); 

    if (inChar == '\n')
    {
      stringComplete = true;
    }
    else
    {
      inputString += inChar;
    }
  }
}

Android端只是接收数据,然后在IntentService中解析:

@Override
protected void onHandleIntent(Intent intent) {
    //Incoming command.
    String rawData =  intent.getStringExtra(DataProcessingIntentService.REQUEST);

    //Append our new data to our data helper.
    Log.i(this.getClass().getName(), "Previous Raw: (" + DataProcessingHelper.getInstance().getData() + ")");
    DataProcessingHelper.getInstance().appendData(rawData);
    Log.i(this.getClass().getName(), "New Raw: (" + DataProcessingHelper.getInstance().getData() + ")");

    commandStartIndex = DataProcessingHelper.getInstance().getData().indexOf("$");
    commandEndIndex = DataProcessingHelper.getInstance().getData().indexOf("\n");

    //Set this as the data starting point.
    if(commandStartIndex != -1){
        DataProcessingHelper.getInstance().offsetData(commandStartIndex);
    }

    //Ensure that a command has been found and that the end index is after the starting index.
    if(commandStartIndex != -1 && commandEndIndex > commandStartIndex){
        //Remove the command structure from the command.
        command = DataProcessingHelper.getInstance().getData().substring(commandStartIndex+1, commandEndIndex-1); //Remove the \r\n end command.
        DataProcessingHelper.getInstance().offsetData(commandEndIndex+1);

        if(command.length() > 1){
            //Split the data out of the comand.
            splitData = command.split(","); 

            Log.i(this.getClass().getName(), "Broadcasting the processed data. (" + command + ")");
            //Broadcast data.
            Intent broadcastIntent = new Intent();
            broadcastIntent.setAction(DataProcessingIntentService.RESPONSE);
            broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
            broadcastIntent.putExtra(DataProcessingIntentService.RESPONSE, splitData);
            sendBroadcast(broadcastIntent);
        }else{
            Log.e(this.getClass().getName(), "Command is less than 1 character long!");
        }
    }           
}

感谢您的帮助!

最佳答案

我现在已经弄清楚是什么导致了这个问题。看来 BLE 每次交易最多只支持 20 个字节。这些事务之间的时间因您使用的内容而异。我目前正在使用通知,这意味着我最多可以每 7.5 毫秒发送 20 个字节。为了安全起见,我选择了 10 毫秒。我现在需要研究将数据包分解成最多 20 个字节以确保没有数据损坏。

关于android - 通过 BLE 发送连续数据流时出现的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26503293/

相关文章:

android - 如何在android studio中编辑jar的源代码?

Android 无法完成 session : -26 new target SDK 22 doesn't support runtime permissions but the old target SDK 23 does

c++ - Arduino 上的可变参数函数

Android:使用 IntentService 获取快速数据

android - react native -当我使用react-native-ble-manager调用扫描功能时,应用程序崩溃

android - 使用 react-native-ble-plx 避免重复的 BLE 设备

Android打开失败: EACCES (Permission denied) in implementing PDF Reader library

用于 Gradle 'sourceCompatibility' 和 'targetCompatibility' 的 Java 版本

c++ - 在不同的机器上编译会产生不同的 Arduino ESP8266 项目的 hex 文件

c++ - 在PC平台上使用Arduino C++库