【问题标题】:Problems when sending a continuous stream of data over BLE通过 BLE 发送连续数据流时的问题
【发布时间】:2014-10-22 08:33:57
【问题描述】:

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

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

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

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

好案例:

坏情况:

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

感谢您的帮助!

【问题讨论】:

    标签: android arduino bluetooth-lowenergy


    【解决方案1】:

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

    【讨论】:

    • 您还可以将连接间隔延迟设置为外围设备上的最小值。 10ms的值是怎么设置的?
    • 我使用延迟(10)发送数据后设置延迟。我现在已经设法将所有数据放入 19/20 字节中,而且看起来很可靠。
    • 你是怎么找到 20 字节的限制的?
    • 多个来源,这里有一个:github.com/tigoe/BLEDocs/wiki/Introduction-to-Bluetooth-LE"一个特征值最长可达20字节"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-24
    • 2010-12-10
    • 2021-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多