从系列Arduino到Android的不完整和奇怪的单词

从系列Arduino到Android的不完整和奇怪的单词,android,arduino,Android,Arduino,我使用教程从Arduino接收代码 我创建了一个id为txtArduino的文本视图,并使用了.append,但为什么在Arduino发送时会收到不完整的文本 例如: LED亮起,并且:w-。iin:13 还有类似的奇怪文字 我如何解决这个问题 PS:如果需要,我可以提供更多的代码 安卓标头 Arduino代码: 我明白了,如果我只发送一个“命令”,它就会起作用。我该怎么做才能在串行和Android上接收更多命令 PS:我使用。分析 查看您的Android代码: String sbprint =

我使用教程从Arduino接收代码

我创建了一个id为txtArduino的文本视图,并使用了
.append
,但为什么在Arduino发送时会收到不完整的文本

例如:

LED亮起,并且:w-。iin:13

还有类似的奇怪文字

我如何解决这个问题

PS:如果需要,我可以提供更多的代码

安卓标头 Arduino代码: 我明白了,如果我只发送一个“命令”,它就会起作用。我该怎么做才能在串行和Android上接收更多命令

PS:我使用。

分析 查看您的Android代码:

String sbprint = sb.substring(0, endOfLineIndex); // Extract string
sb.delete(0, sb.length());                        // And clear
txtArduino.append(sbprint + "\n");                // Update TextView
您将接收到的消息保留到(第一个)换行符,然后删除整个消息。我认为,人们无法确定在收到的任何给定“消息”中会包含多少数据

例如,当Arduino执行以下操作时:

Serial.print("Pin 13: LED OFF\r\n");
// [...]
Serial.print("Command:W-C\r\n");
这将发送到BT模块,如下所示:

Serial.print("Pin 13: LED OFF\r\nCommand:W-C\r\n");
然后“某人”(BT模块?Android操作系统?…)可能会决定将其放入两个“消息”(或“数据包”)中,例如

Message1: "Pin 13: LED OFF\r\nComm"
Message2: "and:W-C\r\n"
在这种情况下,您将只看到

"Pin 13: LED OFF"

显示,因为您删除了第一条消息的
“Comm”
部分

暗示 尝试替换你的
sb.delete(0,sb.length())sb.delete(0,endOfLineIndex+“\r\n”.length()),这样您就不会删除整个字符串,而只删除实际显示的部分,其余部分将在下一条消息中完成

然后还应该添加一个循环,以确保当消息中有多个换行符时,代码不会失败。为此,只需更换:

int endOfLineIndex = sb.indexOf("\r\n"); // Determine the end-of-line
if (endOfLineIndex > 0) {                // If end-of-line,

不要在Arduino端发送print(“/r/n”),只需放置一个空的System.println()

像这样:

void taskTransmitData(void)
{
  // start the xml
  Serial.print("HE");
  Serial.print(pMyMemory->currentHeading);
  Serial.print("/HE");

---- 

  Serial.print("CS");
  Serial.print(currentState);
  Serial.print("/CS");

  Serial.println();

}
在android方面,这应该是可行的:

bytes = mmInStream.read(buffer);
byte[] readBuf = (byte[]) buffer;
String strIncom = new String(readBuf, 0, bytes); // create string from bytes array
sb.append(strIncom);      // append string
int endOfLineIndex = sb.indexOf("\r\n");  // determine the end-of-line
if (endOfLineIndex > 0) {  
    // add the current string to eol to a local string
    String sbprint = sb.substring(0, endOfLineIndex);

    // get the start and end indexes of the heading
    int startHeading = sb.indexOf("HE");
    int endHeading = sb.indexOf("/HE");

    // set the heading
    Henry.this.setCurrentHeading(sb.substring((startHeading + 2), endHeading));

    ......

    // get the start and end indexes of the current state
    int startCS = sb.indexOf("CS");
    int endCS = sb.indexOf("/CS");

    // set the current state
    Henry.this.currentState = sb.substring((startCS + 2), endCS);

    // output what we have
    //System.out.println("recv: " + sbprint);
    sb.delete(0, sb.length());   
}

1.您确定BT模块使用的波特率正确吗?是否设置为以9600bps通信?2.你的Arduino是如何计时的?石英?频率是多少?模块设置为9600bps。这个想法是,如果我使用谷歌play的蓝牙终端,它可以正常工作,没有任何错误或东西,我就可以得到我在串行上发送的东西。我按照你的建议做了,我仍然会得到奇怪的字符串,如果我发送199,有时我会得到999
int endOfLineIndex = sb.indexOf("\r\n"); // Determine the end-of-line
if (endOfLineIndex > 0) {                // If end-of-line,
int endOfLineIndex; 
while ( (endOfLineIndex = sb.indexOf("\r\n")) > 0) { // While there's at least one complete line in buffer,
void taskTransmitData(void)
{
  // start the xml
  Serial.print("HE");
  Serial.print(pMyMemory->currentHeading);
  Serial.print("/HE");

---- 

  Serial.print("CS");
  Serial.print(currentState);
  Serial.print("/CS");

  Serial.println();

}
bytes = mmInStream.read(buffer);
byte[] readBuf = (byte[]) buffer;
String strIncom = new String(readBuf, 0, bytes); // create string from bytes array
sb.append(strIncom);      // append string
int endOfLineIndex = sb.indexOf("\r\n");  // determine the end-of-line
if (endOfLineIndex > 0) {  
    // add the current string to eol to a local string
    String sbprint = sb.substring(0, endOfLineIndex);

    // get the start and end indexes of the heading
    int startHeading = sb.indexOf("HE");
    int endHeading = sb.indexOf("/HE");

    // set the heading
    Henry.this.setCurrentHeading(sb.substring((startHeading + 2), endHeading));

    ......

    // get the start and end indexes of the current state
    int startCS = sb.indexOf("CS");
    int endCS = sb.indexOf("/CS");

    // set the current state
    Henry.this.currentState = sb.substring((startCS + 2), endCS);

    // output what we have
    //System.out.println("recv: " + sbprint);
    sb.delete(0, sb.length());   
}