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

时间:2014-10-22 08:33:57

标签: 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!");
        }
    }           
}

感谢您的帮助!

1 个答案:

答案 0 :(得分:5)

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