2014-10-22 43 views
1

我想知道是否有人可以幫我弄清楚是什麼導致我發送的數據變得腐敗。通過BLE發送連續數據流時出現問題

我的設置目前是一個連接了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的側只有r接收數據,然後在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!"); 
     } 
    }   
} 

謝謝任何​​幫助!

回答

5

我現在已經弄清楚是什麼導致了這個問題。看來BLE每個事務只支持最多20個字節。這些交易之間的時間根據您的使用情況而有所不同。我目前正在使用通知,這意味着我可以每7.5毫秒發送20個字節。我選擇了10毫秒是安全的。我現在需要考慮將數據包分成最多20個字節以確保沒有數據損壞。

+0

您還可以將連接間隔延遲設置爲外設的最小值。你如何設置10ms值? – sr09 2014-11-03 21:20:07

+0

我使用delay(10)發送數據後設置了延遲。我已經成功地將我的所有數據都轉換爲19/20字節,並且看起來很可靠。 – NextGenHen 2014-11-04 23:06:22

+0

你怎麼找到20字節的限制? – nkint 2014-12-03 16:26:00

相關問題