1

我使用教程Data transfer between Android and Arduino via Bluetooth从 Arduino 接收代码。

我用 id txtArduino 制作了一个文本视图,我使用了.append,但是为什么当 Arduino 发送时我收到不完整的文本?

例如:

LED 亮起,并且:w- 。输入:13

还有类似这样的奇怪文字。

我该如何解决这个问题?

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

安卓标头

h = new Handler() {
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case RECIEVE_MESSAGE:                                     // If receive massage
                byte[] readBuf = (byte[]) msg.obj;
                String strIncom = new String(readBuf, 0, msg.arg1);   // Create string from bytes array
                sb.append(strIncom);                                  // Append string
                int endOfLineIndex = sb.indexOf("\r\n");              // Determine the end-of-line
                if (endOfLineIndex > 0) {                             // If end-of-line,
                    String sbprint = sb.substring(0, endOfLineIndex); // Extract string
                    sb.delete(0, sb.length());                        // And clear
                    txtArduino.append(sbprint + "\n");                // Update TextView

                    //toggleDOOR.setEnabled(true);
                }
                Log.d(TAG, "...Mesaj:"+ sb.toString() +  " Byte:" + msg.arg1 + "...");
                break;
            }
    };
};

Arduino代码:

int cnt = 0;
int pinLed = 13;

String myKey = "0000001";

//Pin Geam Masina 2 si 3
int pinGeamInchis = 2;
int pinGeamDeschis = 3;

char incomingByte;
void setup(){
    Serial.begin(9600);

    //INITIALIZARE
    pinMode(pinLed,OUTPUT);
    pinMode(pinGeamInchis,OUTPUT);
    pinMode(pinGeamDeschis,OUTPUT);

    digitalWrite(pinLed,LOW);

    Serial.print("Modul ON( KEY:");
    Serial.print(myKey);
    Serial.print(" )\r\n");
}

void loop(){
    if(Serial.available()> 0){
        incomingByte = Serial.read();
        if(incomingByte == '0'){
            //Opreste LED
            digitalWrite(pinLed,LOW);
            Serial.print("Pin 13: LED OFF\r\n");

            //Inchide Geam
            digitalWrite(pinGeamInchis,HIGH);
            Serial.print("Command:W-C\r\n");
            delay(500);
            digitalWrite(pinGeamInchis,LOW);
            Serial.print("Pin 2:Window Down\r\n");
        }
        if(incomingByte == '1'){
            //Aprinde LED
            digitalWrite(pinLed,HIGH);
            Serial.print("LED ON\r\n");

            //Deschide Geam
            digitalWrite(pinGeamDeschis,HIGH);
            Serial.print("Command:W-O\r\n");
            delay(500);
            digitalWrite(pinGeamDeschis,LOW);
            Serial.print("Pin 3:Window Up\r\n");
        }
    }
}

我明白了,如果我只发送一个“命令”,它就可以工作。我该怎么做才能在串行和 Android 上接收更多命令?

PS:我使用Arduino Nano

4

2 回答 2

0

无需在 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());   
}
于 2013-04-27T16:54:16.023 回答
0

分析

查看您的 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 OS?...?)可能会决定将其放入两个“消息”(或“数据包”)中,例如

Message1: "Pin 13: LED OFF\r\nComm"
Message2: "and:W-C\r\n"

在这种情况下,您只会看到

"Pin 13: LED OFF"

"and:W-C"

显示,因为您删除"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,

int endOfLineIndex; 
while ( (endOfLineIndex = sb.indexOf("\r\n")) > 0) { // While there's at least one complete line in buffer,
于 2013-04-19T18:17:47.600 回答