gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 15:14:00 24 4
gpt4 key购买 nike

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

我目前的设置是连接了 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com