HC-05和x2B;Android(错误的回声/数据)

HC-05和x2B;Android(错误的回声/数据),android,bluetooth,arduino,Android,Bluetooth,Arduino,所以我现在面临着一个问题。任何建议都可以。 首先,我使用我的代码从arduino接收数据,然后我使用bluetoothChat并更改uuid,我可以配对,一切都很好,但是如果我从arduino向android发送整个字符串,我只能得到该字符串的一部分。 如果我使用谷歌play的蓝牙终端,一切都正常,在描述中,它说它是由蓝牙聊天样本制作的 代码Arduino #include <SoftwareSerial.h> SoftwareSerial mySerial(10, 9); //R

所以我现在面临着一个问题。任何建议都可以。 首先,我使用我的代码从arduino接收数据,然后我使用bluetoothChat并更改uuid,我可以配对,一切都很好,但是如果我从arduino向android发送整个字符串,我只能得到该字符串的一部分。 如果我使用谷歌play的蓝牙终端,一切都正常,在描述中,它说它是由蓝牙聊天样本制作的

代码Arduino

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 9); //RX,TX

 long int i = 0;

void setup(){
  mySerial.begin(9600);

}

void loop(){
  mySerial.print("This is a message n. ");
  mySerial.println(i);
  i++;
  delay(100);
}
处理程序:

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

                Log.d("Arduino", "Mesaj:"+ sbprint.toString());



            }
            Log.d("Arduino", "...Mesaj:"+ sb.toString() +  " Byte:" + msg.arg1 + "...");
            break;
        }
    };
};
InputStream的侦听器

  public void run() {
        byte[] buffer = new byte[256];  // buffer store for the stream
        int bytes; // bytes returned from read()

        // Keep listening to the InputStream until an exception occurs
        while (true) {
            try {
                // Read from the InputStream
                bytes = mmInStream.read(buffer);        // Get number of bytes and message in "buffer"
                h.obtainMessage(RECIEVE_MESSAGE, bytes, -1, buffer).sendToTarget();     // Send to message queue Handler


            } catch (IOException e) {
                break;
            }
        }

    }

注意,您使用的是串行端口的软件仿真,因此计时不如硬件UART好

这可能是以下两个可能问题中的一个或两个:

1) 起始位和停止位未正确计时,导致字节背对背。设置字符串时会发生这种情况,而不是一次按一个键。 解决方案是将每个键隔开

2) 波特率与公差不匹配。HC05和Arduino上的波特率减慢或加快都能更好地匹配定时


我还建议确保您的库是SoftwareSerial,并声明它是NewSoftSerial。它解决了许多问题。它是在Arduino IDE 1.0+核心库中实现的,所以如果你有最新的IDE,你应该有它。

我有Arduino 1.0.5,所以我应该有新的SoftwareSerial,你能给我更多的细节如何做第一个解决方案吗,我会在我的第一篇文章中移植我接收和提取字节的处理程序。
  public void run() {
        byte[] buffer = new byte[256];  // buffer store for the stream
        int bytes; // bytes returned from read()

        // Keep listening to the InputStream until an exception occurs
        while (true) {
            try {
                // Read from the InputStream
                bytes = mmInStream.read(buffer);        // Get number of bytes and message in "buffer"
                h.obtainMessage(RECIEVE_MESSAGE, bytes, -1, buffer).sendToTarget();     // Send to message queue Handler


            } catch (IOException e) {
                break;
            }
        }

    }